Skip to content

Commit d6428a3

Browse files
metacosmcsviri
authored andcommitted
refactor: DependentResourceSpec shouldn't expose setters (#1316)
1 parent bedcc25 commit d6428a3

File tree

4 files changed

+38
-64
lines changed

4 files changed

+38
-64
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AnnotationControllerConfiguration.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ public List<DependentResourceSpec> getDependentResources() {
237237
throw new IllegalArgumentException(
238238
"A DependentResource named: " + name + " already exists: " + spec);
239239
}
240-
spec = new DependentResourceSpec(dependentType, config, name);
241-
spec.setDependsOn(Set.of(dependent.dependsOn()));
242-
addConditions(spec, dependent);
240+
spec = new DependentResourceSpec(dependentType, config, name,
241+
Set.of(dependent.dependsOn()),
242+
instantiateConditionIfNotVoid(dependent.readyPostcondition()),
243+
instantiateConditionIfNotVoid(dependent.reconcilePrecondition()),
244+
instantiateConditionIfNotVoid(dependent.deletePostcondition()));
243245
specsMap.put(name, spec);
244246
}
245247

@@ -248,28 +250,18 @@ public List<DependentResourceSpec> getDependentResources() {
248250
return specs;
249251
}
250252

251-
@SuppressWarnings("unchecked")
252-
private void addConditions(DependentResourceSpec spec, Dependent dependent) {
253-
if (dependent.deletePostcondition() != VoidCondition.class) {
254-
spec.setDeletePostCondition(instantiateCondition(dependent.deletePostcondition()));
255-
}
256-
if (dependent.readyPostcondition() != VoidCondition.class) {
257-
spec.setReadyPostcondition(instantiateCondition(dependent.readyPostcondition()));
258-
}
259-
if (dependent.reconcilePrecondition() != VoidCondition.class) {
260-
spec.setReconcilePrecondition(instantiateCondition(dependent.reconcilePrecondition()));
261-
}
262-
}
263-
264-
private Condition<?, ?> instantiateCondition(Class<? extends Condition> condition) {
265-
try {
266-
return condition.getDeclaredConstructor().newInstance();
267-
} catch (InstantiationException
268-
| IllegalAccessException
269-
| InvocationTargetException
270-
| NoSuchMethodException e) {
271-
throw new OperatorException(e);
253+
private Condition<?, ?> instantiateConditionIfNotVoid(Class<? extends Condition> condition) {
254+
if (condition != VoidCondition.class) {
255+
try {
256+
return condition.getDeclaredConstructor().newInstance();
257+
} catch (InstantiationException
258+
| IllegalAccessException
259+
| InvocationTargetException
260+
| NoSuchMethodException e) {
261+
throw new OperatorException(e);
262+
}
272263
}
264+
return null;
273265
}
274266

275267
private String getName(Dependent dependent, Class<? extends DependentResource> dependentType) {
@@ -280,7 +272,7 @@ private String getName(Dependent dependent, Class<? extends DependentResource> d
280272
return name;
281273
}
282274

283-
@SuppressWarnings("rawtypes")
275+
@SuppressWarnings({"rawtypes", "unchecked"})
284276
private Object createKubernetesResourceConfig(Class<? extends DependentResource> dependentType) {
285277

286278
Object config;

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverrider.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,9 @@ public ControllerConfiguration<R> build() {
211211
@SuppressWarnings({"rawtypes", "unchecked"})
212212
private DependentResourceSpec<?, ?> updateSpec(String name, DependentResourceSpec spec,
213213
KubernetesDependentResourceConfig c) {
214-
var res = new DependentResourceSpec(spec.getDependentResourceClass(),
215-
c.setNamespaces(namespaces), name);
216-
res.setReadyPostcondition(spec.getReadyCondition());
217-
res.setReconcilePrecondition(spec.getReconcileCondition());
218-
res.setDeletePostCondition(spec.getDeletePostCondition());
219-
res.setDependsOn(spec.getDependsOn());
220-
return res;
214+
return new DependentResourceSpec(spec.getDependentResourceClass(),
215+
c.setNamespaces(namespaces), name, spec.getDependsOn(), spec.getReadyCondition(),
216+
spec.getReconcileCondition(), spec.getDeletePostCondition());
221217
}
222218

223219
public static <R extends HasMetadata> ControllerConfigurationOverrider<R> override(

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/dependent/DependentResourceSpec.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.javaoperatorsdk.operator.api.config.dependent;
22

3-
import java.util.HashSet;
3+
import java.util.Collections;
44
import java.util.Objects;
55
import java.util.Optional;
66
import java.util.Set;
@@ -16,19 +16,30 @@ public class DependentResourceSpec<T extends DependentResource<?, ?>, C> {
1616

1717
private final String name;
1818

19-
private Set<String> dependsOn;
19+
private final Set<String> dependsOn;
2020

21-
private Condition<?, ?> readyCondition;
21+
private final Condition<?, ?> readyCondition;
2222

23-
private Condition<?, ?> reconcileCondition;
23+
private final Condition<?, ?> reconcileCondition;
2424

25-
private Condition<?, ?> deletePostCondition;
25+
private final Condition<?, ?> deletePostCondition;
2626

2727
public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
2828
String name) {
29+
this(dependentResourceClass, dependentResourceConfig, name, Collections.emptySet(), null, null,
30+
null);
31+
}
32+
33+
public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
34+
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
35+
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition) {
2936
this.dependentResourceClass = dependentResourceClass;
3037
this.dependentResourceConfig = dependentResourceConfig;
3138
this.name = name;
39+
this.dependsOn = dependsOn;
40+
this.readyCondition = readyCondition;
41+
this.reconcileCondition = reconcileCondition;
42+
this.deletePostCondition = deletePostCondition;
3243
}
3344

3445
public Class<T> getDependentResourceClass() {
@@ -68,44 +79,21 @@ public int hashCode() {
6879
}
6980

7081
public Set<String> getDependsOn() {
71-
if (dependsOn == null) {
72-
dependsOn = new HashSet<>(0);
73-
}
7482
return dependsOn;
7583
}
7684

77-
public DependentResourceSpec<T, C> setDependsOn(Set<String> dependsOn) {
78-
this.dependsOn = dependsOn;
79-
return this;
80-
}
81-
8285
@SuppressWarnings("rawtypes")
8386
public Condition getReadyCondition() {
8487
return readyCondition;
8588
}
8689

87-
public DependentResourceSpec<T, C> setReadyPostcondition(Condition<?, ?> readyCondition) {
88-
this.readyCondition = readyCondition;
89-
return this;
90-
}
91-
9290
@SuppressWarnings("rawtypes")
9391
public Condition getReconcileCondition() {
9492
return reconcileCondition;
9593
}
9694

97-
public DependentResourceSpec<T, C> setReconcilePrecondition(Condition<?, ?> reconcileCondition) {
98-
this.reconcileCondition = reconcileCondition;
99-
return this;
100-
}
101-
10295
@SuppressWarnings("rawtypes")
10396
public Condition getDeletePostCondition() {
10497
return deletePostCondition;
10598
}
106-
107-
public DependentResourceSpec<T, C> setDeletePostCondition(Condition<?, ?> deletePostCondition) {
108-
this.deletePostCondition = deletePostCondition;
109-
return this;
110-
}
11199
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/ManagedWorkflowTestUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ public class ManagedWorkflowTestUtils {
1010

1111
@SuppressWarnings("unchecked")
1212
public static DependentResourceSpec createDRS(String name, String... dependOns) {
13-
final var spec = new DependentResourceSpec(EmptyTestDependentResource.class,
14-
null, name);
15-
spec.setDependsOn(Set.of(dependOns));
16-
return spec;
13+
return new DependentResourceSpec(EmptyTestDependentResource.class,
14+
null, name, Set.of(dependOns), null, null, null);
1715
}
1816

1917
}

0 commit comments

Comments
 (0)