Skip to content

Commit 7db5c33

Browse files
authored
fix: make sure a DependentResourceSpec is always created complete (#1391)
1 parent 7010638 commit 7db5c33

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,7 @@ public List<DependentResourceSpec> getDependentResources() {
277277

278278
private Condition<?, ?> instantiateConditionIfNotVoid(Class<? extends Condition> condition) {
279279
if (condition != VoidCondition.class) {
280-
try {
281-
return condition.getDeclaredConstructor().newInstance();
282-
} catch (InstantiationException
283-
| IllegalAccessException
284-
| InvocationTargetException
285-
| NoSuchMethodException e) {
286-
throw new OperatorException(e);
287-
}
280+
return instantiateAndConfigureIfNeeded(condition, Condition.class);
288281
}
289282
return null;
290283
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ public ControllerConfigurationOverrider<R> replacingNamedDependentResourceConfig
172172

173173
private void replaceConfig(String name, Object newConfig, DependentResourceSpec<?, ?> current) {
174174
namedDependentResourceSpecs.put(name,
175-
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name));
175+
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name,
176+
current.getDependsOn(), current.getReadyCondition(), current.getReconcileCondition(),
177+
current.getDeletePostCondition()));
176178
}
177179

178180
@SuppressWarnings("unchecked")

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

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

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

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

27-
public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
28-
String name) {
29-
this(dependentResourceClass, dependentResourceConfig, name, Collections.emptySet(), null, null,
30-
null);
31-
}
32-
3326
public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
3427
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
3528
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition) {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
1818
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
1919
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
20+
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
2021

2122
import static org.junit.jupiter.api.Assertions.assertEquals;
2223
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -91,6 +92,7 @@ public Optional<Object> getSecondaryResource(ConfigMap primary) {
9192
}
9293
}
9394

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

196+
@SuppressWarnings("unchecked")
194197
@Test
195198
void dependentShouldWatchAllNamespacesIfParentDoesAsWell() {
196199
var configuration = createConfiguration(new WatchAllNamespacesReconciler());
@@ -211,6 +214,7 @@ void dependentShouldWatchAllNamespacesIfParentDoesAsWell() {
211214
assertEquals(Set.of(newNS), config.namespaces());
212215
}
213216

217+
@SuppressWarnings("unchecked")
214218
@Test
215219
void shouldBePossibleToForceDependentToWatchAllNamespaces() {
216220
var configuration = createConfiguration(new DependentWatchesAllNSReconciler());
@@ -272,6 +276,7 @@ void alreadyOverriddenDependentNamespacesShouldNotBePropagated() {
272276
assertEquals(Set.of(OverriddenNSDependent.DEP_NS), config.namespaces());
273277
}
274278

279+
@SuppressWarnings({"rawtypes", "unchecked"})
275280
@Test
276281
void replaceNamedDependentResourceConfigShouldWork() {
277282
var configuration = createConfiguration(new OneDepReconciler());
@@ -284,7 +289,7 @@ void replaceNamedDependentResourceConfigShouldWork() {
284289

285290
var dependentSpec = dependents.stream()
286291
.filter(dr -> dr.getName().equals(dependentResourceName))
287-
.findFirst().get();
292+
.findFirst().orElseThrow();
288293
assertEquals(ReadOnlyDependent.class, dependentSpec.getDependentResourceClass());
289294
var maybeConfig = dependentSpec.getDependentResourceConfiguration();
290295
assertTrue(maybeConfig.isPresent());
@@ -306,12 +311,14 @@ void replaceNamedDependentResourceConfigShouldWork() {
306311
.build();
307312
dependents = overridden.getDependentResources();
308313
dependentSpec = dependents.stream().filter(dr -> dr.getName().equals(dependentResourceName))
309-
.findFirst().get();
314+
.findFirst().orElseThrow();
310315
config = (KubernetesDependentResourceConfig) dependentSpec.getDependentResourceConfiguration()
311316
.orElseThrow();
312317
assertEquals(1, config.namespaces().size());
313318
assertEquals(labelSelector, config.labelSelector());
314319
assertEquals(Set.of(overriddenNS), config.namespaces());
320+
// check that we still have the proper workflow configuration
321+
assertTrue(dependentSpec.getReadyCondition() instanceof TestCondition);
315322
}
316323

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

342+
private static class TestCondition implements Condition<ConfigMap, ConfigMap> {
343+
344+
@Override
345+
public boolean isMet(DependentResource<ConfigMap, ConfigMap> dependentResource,
346+
ConfigMap primary, Context<ConfigMap> context) {
347+
return true;
348+
}
349+
}
350+
335351
@ControllerConfiguration(namespaces = OneDepReconciler.CONFIGURED_NS,
336-
dependents = @Dependent(type = ReadOnlyDependent.class))
352+
dependents = @Dependent(type = ReadOnlyDependent.class,
353+
readyPostcondition = TestCondition.class))
337354
private static class OneDepReconciler implements Reconciler<ConfigMap> {
338355

339356
private static final String CONFIGURED_NS = "foo";

0 commit comments

Comments
 (0)