Skip to content

Commit a973711

Browse files
authored
improve: dependent resource can be re-initialized (#2026)
1 parent 574eeed commit a973711

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public void setConfigurationService(ConfigurationService configurationService) {
365365
super.setConfigurationService(configurationService);
366366

367367
cache.addIndexers(indexerBuffer);
368-
indexerBuffer = null;
368+
indexerBuffer = new HashMap<>();
369369
}
370370

371371
public void addIndexers(Map<String, Function<R, List<String>>> indexers) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.javaoperatorsdk.operator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import io.fabric8.kubernetes.client.KubernetesClient;
6+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
7+
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
8+
import io.javaoperatorsdk.operator.sample.dependentreinitialization.ConfigMapDependentResource;
9+
import io.javaoperatorsdk.operator.sample.dependentreinitialization.DependentReInitializationCustomResource;
10+
import io.javaoperatorsdk.operator.sample.dependentreinitialization.DependentReInitializationReconciler;
11+
12+
class DependentReInitializationIT {
13+
14+
/**
15+
* In case dependent resource is managed by CDI (like in Quarkus) can be handy that the instance
16+
* is reused in tests.
17+
*/
18+
@Test
19+
void dependentCanDeReInitialized() {
20+
var client = new KubernetesClientBuilder().build();
21+
LocallyRunOperatorExtension.applyCrd(DependentReInitializationCustomResource.class, client);
22+
23+
var dependent = new ConfigMapDependentResource();
24+
25+
startEndStopOperator(client, dependent);
26+
startEndStopOperator(client, dependent);
27+
}
28+
29+
private static void startEndStopOperator(KubernetesClient client,
30+
ConfigMapDependentResource dependent) {
31+
Operator o1 = new Operator(o -> o
32+
.withCloseClientOnStop(false)
33+
.withKubernetesClient(client));
34+
o1.register(new DependentReInitializationReconciler(dependent, client));
35+
o1.start();
36+
o1.stop();
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.javaoperatorsdk.operator.sample.dependentreinitialization;
2+
3+
import java.util.Map;
4+
5+
import io.fabric8.kubernetes.api.model.ConfigMap;
6+
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
7+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
8+
import io.javaoperatorsdk.operator.api.reconciler.Context;
9+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
10+
11+
public class ConfigMapDependentResource
12+
extends CRUDKubernetesDependentResource<ConfigMap, DependentReInitializationCustomResource> {
13+
14+
public ConfigMapDependentResource() {
15+
super(ConfigMap.class);
16+
}
17+
18+
@Override
19+
protected ConfigMap desired(DependentReInitializationCustomResource primary,
20+
Context<DependentReInitializationCustomResource> context) {
21+
return new ConfigMapBuilder()
22+
.withMetadata(new ObjectMetaBuilder()
23+
.withName(primary.getMetadata().getName())
24+
.withNamespace(primary.getMetadata().getNamespace())
25+
.build())
26+
.withData(Map.of("key", "val"))
27+
.build();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.javaoperatorsdk.operator.sample.dependentreinitialization;
2+
3+
import io.fabric8.kubernetes.api.model.Namespaced;
4+
import io.fabric8.kubernetes.client.CustomResource;
5+
import io.fabric8.kubernetes.model.annotation.Group;
6+
import io.fabric8.kubernetes.model.annotation.Version;
7+
8+
@Group("sample.javaoperatorsdk")
9+
@Version("v1")
10+
public class DependentReInitializationCustomResource
11+
extends CustomResource<Void, Void>
12+
implements Namespaced {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.javaoperatorsdk.operator.sample.dependentreinitialization;
2+
3+
import java.util.Map;
4+
5+
import io.fabric8.kubernetes.client.KubernetesClient;
6+
import io.javaoperatorsdk.operator.api.reconciler.*;
7+
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
8+
9+
@ControllerConfiguration
10+
public class DependentReInitializationReconciler
11+
implements Reconciler<DependentReInitializationCustomResource>,
12+
EventSourceInitializer<DependentReInitializationCustomResource> {
13+
14+
private final ConfigMapDependentResource configMapDependentResource;
15+
16+
public DependentReInitializationReconciler(ConfigMapDependentResource dependentResource,
17+
KubernetesClient client) {
18+
this.configMapDependentResource = dependentResource;
19+
this.configMapDependentResource.setKubernetesClient(client);
20+
}
21+
22+
@Override
23+
public UpdateControl<DependentReInitializationCustomResource> reconcile(
24+
DependentReInitializationCustomResource resource,
25+
Context<DependentReInitializationCustomResource> context) throws Exception {
26+
configMapDependentResource.reconcile(resource, context);
27+
return UpdateControl.noUpdate();
28+
}
29+
30+
@Override
31+
public Map<String, EventSource> prepareEventSources(
32+
EventSourceContext<DependentReInitializationCustomResource> context) {
33+
return EventSourceInitializer.nameEventSourcesFromDependentResource(context,
34+
configMapDependentResource);
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)