Skip to content

Commit cadae26

Browse files
committed
feat: use a builder to create the informer's configuration
1 parent 20b97b7 commit cadae26

File tree

4 files changed

+126
-43
lines changed

4 files changed

+126
-43
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/KubernetesDependentResourceController.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public KubernetesDependentResourceController(DependentResource<R, P> delegate,
3232
? (AssociatedSecondaryResourceIdentifier<P>) delegate
3333
: configuration.getAssociatedResourceIdentifier();
3434

35-
this.configuration = new InformerConfiguration<>(configuration.getConfigurationService(),
36-
configuration.getLabelSelector(), configuration.getResourceClass(), associatedPrimaries,
37-
associatedSecondary, configuration.isSkipUpdateEventPropagationIfNoChange(),
38-
configuration.getNamespaces());
35+
this.configuration = InformerConfiguration.from(configuration)
36+
.withPrimaryResourcesRetriever(associatedPrimaries)
37+
.withAssociatedSecondaryResourceIdentifier(associatedSecondary)
38+
.build();
3939
this.owned = owned;
4040
}
4141

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

+87-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.fabric8.kubernetes.api.model.HasMetadata;
88
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
99
import io.javaoperatorsdk.operator.api.config.DefaultResourceConfiguration;
10+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
1011
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1112
import io.javaoperatorsdk.operator.processing.event.source.AssociatedSecondaryResourceIdentifier;
1213
import io.javaoperatorsdk.operator.processing.event.source.PrimaryResourcesRetriever;
@@ -18,23 +19,7 @@ public class InformerConfiguration<R extends HasMetadata, P extends HasMetadata>
1819
private final AssociatedSecondaryResourceIdentifier<P> associatedWith;
1920
private final boolean skipUpdateEventPropagationIfNoChange;
2021

21-
public InformerConfiguration(ConfigurationService service, String labelSelector,
22-
Class<R> resourceClass, String... namespaces) {
23-
this(service, labelSelector, resourceClass, Mappers.fromOwnerReference(), null, true,
24-
namespaces);
25-
}
26-
27-
public InformerConfiguration(ConfigurationService service, String labelSelector,
28-
Class<R> resourceClass,
29-
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
30-
AssociatedSecondaryResourceIdentifier<P> associatedWith,
31-
boolean skipUpdateEventPropagationIfNoChange, String... namespaces) {
32-
this(service, labelSelector, resourceClass, secondaryToPrimaryResourcesIdSet, associatedWith,
33-
skipUpdateEventPropagationIfNoChange,
34-
namespaces != null ? Set.of(namespaces) : Collections.emptySet());
35-
}
36-
37-
public InformerConfiguration(ConfigurationService service, String labelSelector,
22+
private InformerConfiguration(ConfigurationService service, String labelSelector,
3823
Class<R> resourceClass,
3924
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
4025
AssociatedSecondaryResourceIdentifier<P> associatedWith,
@@ -59,4 +44,89 @@ public AssociatedSecondaryResourceIdentifier<P> getAssociatedResourceIdentifier(
5944
public boolean isSkipUpdateEventPropagationIfNoChange() {
6045
return skipUpdateEventPropagationIfNoChange;
6146
}
47+
48+
public static class InformerConfigurationBuilder<R extends HasMetadata, P extends HasMetadata> {
49+
50+
private PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet;
51+
private AssociatedSecondaryResourceIdentifier<P> associatedWith;
52+
private boolean skipUpdateEventPropagationIfNoChange = true;
53+
private Set<String> namespaces;
54+
private String labelSelector;
55+
private final Class<R> resourceClass;
56+
private final ConfigurationService configurationService;
57+
58+
private InformerConfigurationBuilder(Class<R> resourceClass,
59+
ConfigurationService configurationService) {
60+
this.resourceClass = resourceClass;
61+
this.configurationService = configurationService;
62+
}
63+
64+
public InformerConfigurationBuilder<R, P> withPrimaryResourcesRetriever(
65+
PrimaryResourcesRetriever<R> primaryResourcesRetriever) {
66+
this.secondaryToPrimaryResourcesIdSet = primaryResourcesRetriever;
67+
return this;
68+
}
69+
70+
public InformerConfigurationBuilder<R, P> withAssociatedSecondaryResourceIdentifier(
71+
AssociatedSecondaryResourceIdentifier<P> associatedWith) {
72+
this.associatedWith = associatedWith;
73+
return this;
74+
}
75+
76+
public InformerConfigurationBuilder<R, P> withoutSkippingEventPropagationIfUnchanged() {
77+
this.skipUpdateEventPropagationIfNoChange = false;
78+
return this;
79+
}
80+
81+
public InformerConfigurationBuilder<R, P> skippingEventPropagationIfUnchanged(
82+
boolean skipIfUnchanged) {
83+
this.skipUpdateEventPropagationIfNoChange = skipIfUnchanged;
84+
return this;
85+
}
86+
87+
public InformerConfigurationBuilder<R, P> withNamespaces(String... namespaces) {
88+
this.namespaces = namespaces != null ? Set.of(namespaces) : Collections.emptySet();
89+
return this;
90+
}
91+
92+
public InformerConfigurationBuilder<R, P> withNamespaces(Set<String> namespaces) {
93+
this.namespaces = namespaces != null ? namespaces : Collections.emptySet();
94+
return this;
95+
}
96+
97+
98+
public InformerConfigurationBuilder<R, P> withLabelSelector(String labelSelector) {
99+
this.labelSelector = labelSelector;
100+
return this;
101+
}
102+
103+
public InformerConfiguration<R, P> build() {
104+
return new InformerConfiguration<>(configurationService, labelSelector, resourceClass,
105+
secondaryToPrimaryResourcesIdSet, associatedWith, skipUpdateEventPropagationIfNoChange,
106+
namespaces);
107+
}
108+
}
109+
110+
public static <R extends HasMetadata, P extends HasMetadata> InformerConfigurationBuilder<R, P> from(
111+
EventSourceContext<P> context, Class<R> resourceClass) {
112+
return new InformerConfigurationBuilder<>(resourceClass, context.getConfigurationService());
113+
}
114+
115+
public static InformerConfigurationBuilder from(ConfigurationService configurationService,
116+
Class resourceClass) {
117+
return new InformerConfigurationBuilder<>(resourceClass, configurationService);
118+
}
119+
120+
public static <R extends HasMetadata, P extends HasMetadata> InformerConfigurationBuilder<R, P> from(
121+
InformerConfiguration<R, P> configuration) {
122+
return new InformerConfigurationBuilder<R, P>(configuration.getResourceClass(),
123+
configuration.getConfigurationService())
124+
.withNamespaces(configuration.getNamespaces())
125+
.withLabelSelector(configuration.getLabelSelector())
126+
.skippingEventPropagationIfUnchanged(
127+
configuration.isSkipUpdateEventPropagationIfNoChange())
128+
.withAssociatedSecondaryResourceIdentifier(
129+
configuration.getAssociatedResourceIdentifier())
130+
.withPrimaryResourcesRetriever(configuration.getPrimaryResourcesRetriever());
131+
}
62132
}

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/runtime/AnnotationConfiguration.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ public List<DependentResource> getDependentResources() {
148148
final var skipIfUnchanged =
149149
valueOrDefault(kubeDependent, KubernetesDependent::skipUpdateIfUnchanged,
150150
KubernetesDependent.SKIP_UPDATE_DEFAULT);
151-
final var configuration = new InformerConfiguration(service, labelSelector,
152-
resourceType, null, null,
153-
skipIfUnchanged, namespaces);
151+
final var configuration = InformerConfiguration.from(service, resourceType)
152+
.withLabelSelector(labelSelector)
153+
.skippingEventPropagationIfUnchanged(skipIfUnchanged)
154+
.withNamespaces(namespaces)
155+
.build();
154156
dependent = new KubernetesDependentResourceController(dependent, configuration, owned);
155157
}
156158

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/WebappReconciler.java

+30-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
2626
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
2727
import io.javaoperatorsdk.operator.processing.event.ResourceID;
28+
import io.javaoperatorsdk.operator.processing.event.source.AssociatedSecondaryResourceIdentifier;
2829
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
30+
import io.javaoperatorsdk.operator.processing.event.source.PrimaryResourcesRetriever;
2931
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerConfiguration;
3032
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
3133

@@ -42,23 +44,32 @@ public WebappReconciler(KubernetesClient kubernetesClient) {
4244

4345
@Override
4446
public List<EventSource> prepareEventSources(EventSourceContext<Webapp> context) {
45-
return List.of(
46-
new InformerEventSource<>(
47-
new InformerConfiguration<>(context.getConfigurationService(),
48-
null,
49-
Tomcat.class,
50-
// To create an event to a related WebApp resource and trigger the reconciliation
51-
// we need to find which WebApp this Tomcat custom resource is related to.
52-
// To find the related customResourceId of the WebApp resource we traverse the cache
53-
// and identify it based on naming convention.
54-
(Tomcat t) -> context.getPrimaryCache()
55-
.list(webApp -> webApp.getSpec().getTomcat().equals(t.getMetadata().getName()))
56-
.map(ResourceID::fromResource)
57-
.collect(Collectors.toSet()),
58-
(Webapp webapp) -> new ResourceID(webapp.getSpec().getTomcat(),
59-
webapp.getMetadata().getNamespace()),
60-
true),
61-
context));
47+
/*
48+
* To create an event to a related WebApp resource and trigger the reconciliation we need to
49+
* find which WebApp this Tomcat custom resource is related to. To find the related
50+
* customResourceId of the WebApp resource we traverse the cache and identify it based on naming
51+
* convention.
52+
*/
53+
final PrimaryResourcesRetriever<Tomcat> webappsMatchingTomcatName =
54+
(Tomcat t) -> context.getPrimaryCache()
55+
.list(webApp -> webApp.getSpec().getTomcat().equals(t.getMetadata().getName()))
56+
.map(ResourceID::fromResource)
57+
.collect(Collectors.toSet());
58+
59+
/*
60+
* We retrieve the Tomcat instance associated with out Webapp from its spec
61+
*/
62+
final AssociatedSecondaryResourceIdentifier<Webapp> tomcatFromWebAppSpec =
63+
(Webapp webapp) -> new ResourceID(
64+
webapp.getSpec().getTomcat(),
65+
webapp.getMetadata().getNamespace());
66+
67+
InformerConfiguration<Tomcat, Webapp> configuration =
68+
InformerConfiguration.from(context, Tomcat.class)
69+
.withPrimaryResourcesRetriever(webappsMatchingTomcatName)
70+
.withAssociatedSecondaryResourceIdentifier(tomcatFromWebAppSpec)
71+
.build();
72+
return List.of(new InformerEventSource<>(configuration, context));
6273
}
6374

6475
/**
@@ -148,7 +159,7 @@ private String[] executeCommandInAllPods(
148159

149160
CompletableFuture<String> data = new CompletableFuture<>();
150161
try (ExecWatch execWatch = execCmd(pod, data, command)) {
151-
status[i] = "" + pod.getMetadata().getName() + ":" + data.get(30, TimeUnit.SECONDS);;
162+
status[i] = "" + pod.getMetadata().getName() + ":" + data.get(30, TimeUnit.SECONDS);
152163
} catch (ExecutionException e) {
153164
status[i] = "" + pod.getMetadata().getName() + ": ExecutionException - " + e.getMessage();
154165
} catch (InterruptedException e) {
@@ -198,7 +209,7 @@ public void onFailure(Throwable t, Response response) {
198209

199210
@Override
200211
public void onClose(int code, String reason) {
201-
log.debug("Exit with: " + code + " and with reason: " + reason);
212+
log.debug("Exit with: {} and with reason: {}", code, reason);
202213
data.complete(baos.toString());
203214
}
204215
}

0 commit comments

Comments
 (0)