Skip to content

fix: make sure a DependentResourceSpec is always created complete #1391

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
Aug 9, 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 @@ -277,14 +277,7 @@ public List<DependentResourceSpec> getDependentResources() {

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 instantiateAndConfigureIfNeeded(condition, Condition.class);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ public ControllerConfigurationOverrider<R> replacingNamedDependentResourceConfig

private void replaceConfig(String name, Object newConfig, DependentResourceSpec<?, ?> current) {
namedDependentResourceSpecs.put(name,
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name));
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name,
current.getDependsOn(), current.getReadyCondition(), current.getReconcileCondition(),
current.getDeletePostCondition()));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.javaoperatorsdk.operator.api.config.dependent;

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand All @@ -24,12 +23,6 @@ public class DependentResourceSpec<T extends DependentResource<?, ?>, C> {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -91,6 +92,7 @@ public Optional<Object> getSecondaryResource(ConfigMap primary) {
}
}

@SuppressWarnings("rawtypes")
private KubernetesDependentResourceConfig extractFirstDependentKubernetesResourceConfig(
io.javaoperatorsdk.operator.api.config.ControllerConfiguration<?> configuration) {
return (KubernetesDependentResourceConfig) extractDependentKubernetesResourceConfig(
Expand Down Expand Up @@ -191,6 +193,7 @@ void configuredDependentShouldNotChangeOnParentOverrideEvenWhenInitialConfigIsSa
assertEquals(Set.of(OverriddenNSDependent.DEP_NS), config.namespaces());
}

@SuppressWarnings("unchecked")
@Test
void dependentShouldWatchAllNamespacesIfParentDoesAsWell() {
var configuration = createConfiguration(new WatchAllNamespacesReconciler());
Expand All @@ -211,6 +214,7 @@ void dependentShouldWatchAllNamespacesIfParentDoesAsWell() {
assertEquals(Set.of(newNS), config.namespaces());
}

@SuppressWarnings("unchecked")
@Test
void shouldBePossibleToForceDependentToWatchAllNamespaces() {
var configuration = createConfiguration(new DependentWatchesAllNSReconciler());
Expand Down Expand Up @@ -272,6 +276,7 @@ void alreadyOverriddenDependentNamespacesShouldNotBePropagated() {
assertEquals(Set.of(OverriddenNSDependent.DEP_NS), config.namespaces());
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Test
void replaceNamedDependentResourceConfigShouldWork() {
var configuration = createConfiguration(new OneDepReconciler());
Expand All @@ -284,7 +289,7 @@ void replaceNamedDependentResourceConfigShouldWork() {

var dependentSpec = dependents.stream()
.filter(dr -> dr.getName().equals(dependentResourceName))
.findFirst().get();
.findFirst().orElseThrow();
assertEquals(ReadOnlyDependent.class, dependentSpec.getDependentResourceClass());
var maybeConfig = dependentSpec.getDependentResourceConfiguration();
assertTrue(maybeConfig.isPresent());
Expand All @@ -306,12 +311,14 @@ void replaceNamedDependentResourceConfigShouldWork() {
.build();
dependents = overridden.getDependentResources();
dependentSpec = dependents.stream().filter(dr -> dr.getName().equals(dependentResourceName))
.findFirst().get();
.findFirst().orElseThrow();
config = (KubernetesDependentResourceConfig) dependentSpec.getDependentResourceConfiguration()
.orElseThrow();
assertEquals(1, config.namespaces().size());
assertEquals(labelSelector, config.labelSelector());
assertEquals(Set.of(overriddenNS), config.namespaces());
// check that we still have the proper workflow configuration
assertTrue(dependentSpec.getReadyCondition() instanceof TestCondition);
}

@ControllerConfiguration(dependents = @Dependent(type = ReadOnlyDependent.class))
Expand All @@ -332,8 +339,18 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
}
}

private static class TestCondition implements Condition<ConfigMap, ConfigMap> {

@Override
public boolean isMet(DependentResource<ConfigMap, ConfigMap> dependentResource,
ConfigMap primary, Context<ConfigMap> context) {
return true;
}
}

@ControllerConfiguration(namespaces = OneDepReconciler.CONFIGURED_NS,
dependents = @Dependent(type = ReadOnlyDependent.class))
dependents = @Dependent(type = ReadOnlyDependent.class,
readyPostcondition = TestCondition.class))
private static class OneDepReconciler implements Reconciler<ConfigMap> {

private static final String CONFIGURED_NS = "foo";
Expand Down