Skip to content

refactor: DependentResourceSpec shouldn't expose setters #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ public List<DependentResourceSpec> getDependentResources() {
throw new IllegalArgumentException(
"A DependentResource named: " + name + " already exists: " + spec);
}
spec = new DependentResourceSpec(dependentType, config, name);
spec.setDependsOn(Set.of(dependent.dependsOn()));
addConditions(spec, dependent);
spec = new DependentResourceSpec(dependentType, config, name,
Set.of(dependent.dependsOn()),
instantiateConditionIfNotVoid(dependent.readyPostcondition()),
instantiateConditionIfNotVoid(dependent.reconcilePrecondition()),
instantiateConditionIfNotVoid(dependent.deletePostcondition()));
specsMap.put(name, spec);
}

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

@SuppressWarnings("unchecked")
private void addConditions(DependentResourceSpec spec, Dependent dependent) {
if (dependent.deletePostcondition() != VoidCondition.class) {
spec.setDeletePostCondition(instantiateCondition(dependent.deletePostcondition()));
}
if (dependent.readyPostcondition() != VoidCondition.class) {
spec.setReadyPostcondition(instantiateCondition(dependent.readyPostcondition()));
}
if (dependent.reconcilePrecondition() != VoidCondition.class) {
spec.setReconcilePrecondition(instantiateCondition(dependent.reconcilePrecondition()));
}
}

private Condition<?, ?> instantiateCondition(Class<? extends Condition> condition) {
try {
return condition.getDeclaredConstructor().newInstance();
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException e) {
throw new OperatorException(e);
private Condition<?, ?> instantiateConditionIfNotVoid(Class<? extends Condition> condition) {
if (condition != VoidCondition.class) {
try {
return condition.getDeclaredConstructor().newInstance();
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException e) {
throw new OperatorException(e);
}
}
return null;
}

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

@SuppressWarnings("rawtypes")
@SuppressWarnings({"rawtypes", "unchecked"})
private Object createKubernetesResourceConfig(Class<? extends DependentResource> dependentType) {

Object config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,9 @@ public ControllerConfiguration<R> build() {
@SuppressWarnings({"rawtypes", "unchecked"})
private DependentResourceSpec<?, ?> updateSpec(String name, DependentResourceSpec spec,
KubernetesDependentResourceConfig c) {
var res = new DependentResourceSpec(spec.getDependentResourceClass(),
c.setNamespaces(namespaces), name);
res.setReadyPostcondition(spec.getReadyCondition());
res.setReconcilePrecondition(spec.getReconcileCondition());
res.setDeletePostCondition(spec.getDeletePostCondition());
res.setDependsOn(spec.getDependsOn());
return res;
return new DependentResourceSpec(spec.getDependentResourceClass(),
c.setNamespaces(namespaces), name, spec.getDependsOn(), spec.getReadyCondition(),
spec.getReconcileCondition(), spec.getDeletePostCondition());
}

public static <R extends HasMetadata> ControllerConfigurationOverrider<R> override(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.javaoperatorsdk.operator.api.config.dependent;

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

private final String name;

private Set<String> dependsOn;
private final Set<String> dependsOn;

private Condition<?, ?> readyCondition;
private final Condition<?, ?> readyCondition;

private Condition<?, ?> reconcileCondition;
private final Condition<?, ?> reconcileCondition;

private Condition<?, ?> deletePostCondition;
private final Condition<?, ?> deletePostCondition;

public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
String name) {
this(dependentResourceClass, dependentResourceConfig, name, Collections.emptySet(), null, null,
null);
}

public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition) {
this.dependentResourceClass = dependentResourceClass;
this.dependentResourceConfig = dependentResourceConfig;
this.name = name;
this.dependsOn = dependsOn;
this.readyCondition = readyCondition;
this.reconcileCondition = reconcileCondition;
this.deletePostCondition = deletePostCondition;
}

public Class<T> getDependentResourceClass() {
Expand Down Expand Up @@ -68,44 +79,21 @@ public int hashCode() {
}

public Set<String> getDependsOn() {
if (dependsOn == null) {
dependsOn = new HashSet<>(0);
}
return dependsOn;
}

public DependentResourceSpec<T, C> setDependsOn(Set<String> dependsOn) {
this.dependsOn = dependsOn;
return this;
}

@SuppressWarnings("rawtypes")
public Condition getReadyCondition() {
return readyCondition;
}

public DependentResourceSpec<T, C> setReadyPostcondition(Condition<?, ?> readyCondition) {
this.readyCondition = readyCondition;
return this;
}

@SuppressWarnings("rawtypes")
public Condition getReconcileCondition() {
return reconcileCondition;
}

public DependentResourceSpec<T, C> setReconcilePrecondition(Condition<?, ?> reconcileCondition) {
this.reconcileCondition = reconcileCondition;
return this;
}

@SuppressWarnings("rawtypes")
public Condition getDeletePostCondition() {
return deletePostCondition;
}

public DependentResourceSpec<T, C> setDeletePostCondition(Condition<?, ?> deletePostCondition) {
this.deletePostCondition = deletePostCondition;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public class ManagedWorkflowTestUtils {

@SuppressWarnings("unchecked")
public static DependentResourceSpec createDRS(String name, String... dependOns) {
final var spec = new DependentResourceSpec(EmptyTestDependentResource.class,
null, name);
spec.setDependsOn(Set.of(dependOns));
return spec;
return new DependentResourceSpec(EmptyTestDependentResource.class,
null, name, Set.of(dependOns), null, null, null);
}

}