Skip to content

Commit 6e2153f

Browse files
authored
Further configuration polish (#927)
* refactor: DependentResourceManager now configures DependentResources This makes it possible to pass a configuration DependentResource implementations and removes the need to initializers. * refactor: remove DesiredSupplier since it makes things less readable
1 parent f45afb1 commit 6e2153f

File tree

16 files changed

+245
-376
lines changed

16 files changed

+245
-376
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public interface DependentResourceConfiguration<R, P extends HasMetadata> {
77

8-
Class<? extends DependentResource<R, P>> getDependentResourceClass();
8+
Class<? extends DependentResource<R, P, ? extends DependentResourceConfiguration<R, P>>> getDependentResourceClass();
99

1010
Class<R> getResourceClass();
1111
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class DefaultKubernetesDependentResourceConfiguration<R extends HasMetadata, P e
2424
implements KubernetesDependentResourceConfiguration<R, P> {
2525

2626

27-
private final Class<DependentResource<R, P>> dependentResourceClass;
27+
private final Class<DependentResource<R, P, KubernetesDependentResourceConfiguration<R, P>>> dependentResourceClass;
2828
private final boolean owned;
2929

3030
protected DefaultKubernetesDependentResourceConfiguration(
3131
ConfigurationService configurationService,
3232
String labelSelector, Class<R> resourceClass,
3333
Set<String> namespaces, boolean owned,
34-
Class<DependentResource<R, P>> dependentResourceClass) {
34+
Class<DependentResource<R, P, KubernetesDependentResourceConfiguration<R, P>>> dependentResourceClass) {
3535
super(labelSelector, resourceClass, namespaces);
3636
setConfigurationService(configurationService);
3737
this.owned = owned;
@@ -44,7 +44,7 @@ public boolean isOwned() {
4444
}
4545

4646
@Override
47-
public Class<? extends DependentResource<R, P>> getDependentResourceClass() {
47+
public Class<? extends DependentResource<R, P, KubernetesDependentResourceConfiguration<R, P>>> getDependentResourceClass() {
4848
return dependentResourceClass;
4949
}
5050
}
@@ -56,6 +56,6 @@ static <R extends HasMetadata, P extends HasMetadata> KubernetesDependentResourc
5656
return new DefaultKubernetesDependentResourceConfiguration<>(cfg.getConfigurationService(),
5757
cfg.getLabelSelector(), cfg.getResourceClass(),
5858
cfg.getNamespaces(), owned,
59-
(Class<DependentResource<R, P>>) dependentResourceClass);
59+
(Class<DependentResource<R, P, KubernetesDependentResourceConfiguration<R, P>>>) dependentResourceClass);
6060
}
6161
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package io.javaoperatorsdk.operator.api.reconciler.dependent;
22

33
import io.fabric8.kubernetes.api.model.HasMetadata;
4+
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfiguration;
45
import io.javaoperatorsdk.operator.api.reconciler.Context;
56

6-
public abstract class AbstractDependentResource<R, P extends HasMetadata>
7-
implements DependentResource<R, P> {
7+
public abstract class AbstractDependentResource<R, P extends HasMetadata, C extends DependentResourceConfiguration<R, P>>
8+
implements DependentResource<R, P, C> {
89

910
@Override
1011
public void reconcile(P primary, Context context) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import io.fabric8.kubernetes.api.model.HasMetadata;
66
import io.javaoperatorsdk.operator.api.config.Utils;
7+
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfiguration;
78
import io.javaoperatorsdk.operator.api.reconciler.Context;
89
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
910
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
1011

11-
public interface DependentResource<R, P extends HasMetadata> {
12+
public interface DependentResource<R, P extends HasMetadata, C extends DependentResourceConfiguration<R, P>> {
1213

1314
Optional<EventSource> eventSource(EventSourceContext<P> context);
1415

@@ -23,4 +24,5 @@ default Class<R> resourceType() {
2324

2425
Optional<R> getResource(P primaryResource);
2526

27+
default void configureWith(C config) {}
2628
}

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

Lines changed: 0 additions & 10 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package io.javaoperatorsdk.operator.api.reconciler.dependent;
22

33
import java.util.Optional;
4+
import java.util.Set;
45

56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

89
import io.fabric8.kubernetes.api.model.HasMetadata;
910
import io.fabric8.kubernetes.client.KubernetesClient;
1011
import io.javaoperatorsdk.operator.ReconcilerUtils;
12+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
13+
import io.javaoperatorsdk.operator.api.config.dependent.KubernetesDependent;
1114
import io.javaoperatorsdk.operator.api.config.dependent.KubernetesDependentResourceConfiguration;
1215
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
1316
import io.javaoperatorsdk.operator.api.reconciler.Context;
@@ -20,55 +23,43 @@
2023
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
2124

2225
public abstract class KubernetesDependentResource<R extends HasMetadata, P extends HasMetadata>
23-
extends AbstractDependentResource<R, P> implements KubernetesClientAware {
26+
extends AbstractDependentResource<R, P, KubernetesDependentResourceConfiguration<R, P>>
27+
implements KubernetesClientAware {
2428

2529
private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);
2630

2731
protected KubernetesClient client;
28-
private boolean explicitDelete = false;
29-
private boolean owned = true;
3032
private InformerEventSource<R, P> informerEventSource;
31-
private DesiredSupplier<R, P> desiredSupplier = null;
32-
private Class<R> resourceType = null;
33-
private AssociatedSecondaryResourceIdentifier<P> associatedSecondaryResourceIdentifier =
34-
ResourceID::fromResource;
35-
private PrimaryResourcesRetriever<R> primaryResourcesRetriever = Mappers.fromOwnerReference();
36-
37-
public KubernetesDependentResource() {
38-
this(null);
39-
}
40-
41-
public KubernetesDependentResource(KubernetesClient client) {
42-
this.client = client;
43-
}
44-
45-
public KubernetesDependentResource(
46-
KubernetesClient client, Class<R> resourceType, DesiredSupplier<R, P> desiredSupplier) {
47-
this.client = client;
48-
this.resourceType = resourceType;
49-
this.desiredSupplier = desiredSupplier;
50-
}
33+
private KubernetesDependentResourceConfiguration<R, P> configuration;
5134

52-
public KubernetesDependentResource(
53-
Class<R> resourceType, DesiredSupplier<R, P> desiredSupplier) {
54-
this(null, resourceType, desiredSupplier);
35+
public void configureWith(KubernetesDependentResourceConfiguration<R, P> config) {
36+
configureWith(config.getConfigurationService(), config.getLabelSelector(),
37+
config.getNamespaces(), config.isOwned());
5538
}
5639

57-
// todo builder and/or factory methods
58-
public void initWithConfiguration(KubernetesDependentResourceConfiguration<R, P> config) {
59-
this.owned = config.isOwned();
40+
@SuppressWarnings("unchecked")
41+
private void configureWith(ConfigurationService service, String labelSelector,
42+
Set<String> namespaces, boolean owned) {
43+
final var primaryResourcesRetriever =
44+
(this instanceof PrimaryResourcesRetriever) ? (PrimaryResourcesRetriever<R>) this
45+
: Mappers.fromOwnerReference();
46+
final AssociatedSecondaryResourceIdentifier<P> secondaryResourceIdentifier =
47+
(this instanceof AssociatedSecondaryResourceIdentifier)
48+
? (AssociatedSecondaryResourceIdentifier<P>) this
49+
: ResourceID::fromResource;
6050
InformerConfiguration<R, P> ic =
61-
InformerConfiguration.from(config.getConfigurationService(), resourceType())
62-
.withLabelSelector(config.getLabelSelector())
63-
.withNamespaces(config.getNamespaces())
64-
.withPrimaryResourcesRetriever(getPrimaryResourcesRetriever())
65-
.withAssociatedSecondaryResourceIdentifier(getAssociatedSecondaryResourceIdentifier())
51+
InformerConfiguration.from(service, resourceType())
52+
.withLabelSelector(labelSelector)
53+
.withNamespaces(namespaces)
54+
.withPrimaryResourcesRetriever(primaryResourcesRetriever)
55+
.withAssociatedSecondaryResourceIdentifier(secondaryResourceIdentifier)
6656
.build();
57+
configuration = KubernetesDependentResourceConfiguration.from(ic, owned, getClass());
6758
informerEventSource = new InformerEventSource<>(ic, client);
6859
}
6960

7061
protected void beforeCreateOrUpdate(R desired, P primary) {
71-
if (owned) {
62+
if (configuration.isOwned()) {
7263
desired.addOwnerReference(primary);
7364
}
7465
}
@@ -100,38 +91,17 @@ protected R update(R actual, R target, P primary, Context context) {
10091
.replace(target);
10192
}
10293

103-
@SuppressWarnings({"unchecked", "rawtypes"})
10494
@Override
10595
public Optional<EventSource> eventSource(EventSourceContext<P> context) {
106-
if (informerEventSource != null) {
107-
return Optional.of(informerEventSource);
96+
if (informerEventSource == null) {
97+
configureWith(context.getConfigurationService(), null, null,
98+
KubernetesDependent.OWNED_DEFAULT);
99+
log.warn("Using default configuration for " + resourceType().getSimpleName()
100+
+ " KubernetesDependentResource, call configureWith to provide configuration");
108101
}
109-
var informerConfig = initDefaultInformerConfiguration(context);
110-
informerEventSource = new InformerEventSource(informerConfig, context);
111102
return Optional.of(informerEventSource);
112103
}
113104

114-
@SuppressWarnings("unchecked")
115-
private InformerConfiguration<R, P> initDefaultInformerConfiguration(
116-
EventSourceContext<P> context) {
117-
return InformerConfiguration.from(context, resourceType())
118-
.withPrimaryResourcesRetriever(getPrimaryResourcesRetriever())
119-
.withAssociatedSecondaryResourceIdentifier(getAssociatedSecondaryResourceIdentifier())
120-
.build();
121-
}
122-
123-
124-
protected PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever() {
125-
return (this instanceof PrimaryResourcesRetriever) ? (PrimaryResourcesRetriever<R>) this
126-
: primaryResourcesRetriever;
127-
}
128-
129-
protected AssociatedSecondaryResourceIdentifier<P> getAssociatedSecondaryResourceIdentifier() {
130-
return (this instanceof AssociatedSecondaryResourceIdentifier)
131-
? (AssociatedSecondaryResourceIdentifier<P>) this
132-
: associatedSecondaryResourceIdentifier;
133-
}
134-
135105
public KubernetesDependentResource<R, P> setInformerEventSource(
136106
InformerEventSource<R, P> informerEventSource) {
137107
this.informerEventSource = informerEventSource;
@@ -140,10 +110,9 @@ public KubernetesDependentResource<R, P> setInformerEventSource(
140110

141111
@Override
142112
public void delete(P primary, Context context) {
143-
if (explicitDelete) {
144-
var resource = getResource(primary);
145-
resource.ifPresent(r -> client.resource(r).delete());
146-
}
113+
// todo: do we need explicit delete? If yes, add it to configuration
114+
var resource = getResource(primary);
115+
resource.ifPresent(r -> client.resource(r).delete());
147116
}
148117

149118
@Override
@@ -155,59 +124,4 @@ public Optional<R> getResource(P primaryResource) {
155124
public void setKubernetesClient(KubernetesClient client) {
156125
this.client = client;
157126
}
158-
159-
public KubernetesDependentResource<R, P> setExplicitDelete(boolean explicitDelete) {
160-
this.explicitDelete = explicitDelete;
161-
return this;
162-
}
163-
164-
public boolean isExplicitDelete() {
165-
return explicitDelete;
166-
}
167-
168-
public boolean isOwned() {
169-
return owned;
170-
}
171-
172-
public KubernetesDependentResource<R, P> setOwned(boolean owned) {
173-
this.owned = owned;
174-
return this;
175-
}
176-
177-
@Override
178-
public Class<R> resourceType() {
179-
if (resourceType != null) {
180-
return resourceType;
181-
} else {
182-
return super.resourceType();
183-
}
184-
}
185-
186-
@Override
187-
protected R desired(P primary, Context context) {
188-
return desiredSupplier.getDesired(primary, context);
189-
}
190-
191-
public KubernetesDependentResource<R, P> setAssociatedSecondaryResourceIdentifier(
192-
AssociatedSecondaryResourceIdentifier<P> associatedSecondaryResourceIdentifier) {
193-
this.associatedSecondaryResourceIdentifier = associatedSecondaryResourceIdentifier;
194-
return this;
195-
}
196-
197-
public KubernetesDependentResource<R, P> setPrimaryResourcesRetriever(
198-
PrimaryResourcesRetriever<R> primaryResourcesRetriever) {
199-
this.primaryResourcesRetriever = primaryResourcesRetriever;
200-
return this;
201-
}
202-
203-
public KubernetesDependentResource<R, P> setDesiredSupplier(
204-
DesiredSupplier<R, P> desiredSupplier) {
205-
this.desiredSupplier = desiredSupplier;
206-
return this;
207-
}
208-
209-
public KubernetesDependentResource<R, P> setResourceType(Class<R> resourceType) {
210-
this.resourceType = resourceType;
211-
return this;
212-
}
213127
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/Controller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Controller(Reconciler<R> reconciler,
5656
this.kubernetesClient = kubernetesClient;
5757

5858
eventSourceManager = new EventSourceManager<>(this);
59-
dependents = new DependentResourceManager<>(this, kubernetesClient);
59+
dependents = new DependentResourceManager<>(this);
6060
}
6161

6262
@Override

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/DependentResourceInitializer.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)