Skip to content

Commit 3ab2d9c

Browse files
csvirimetacosm
authored andcommitted
feat: builder for kubernetes dependent configuration (#2054)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 9b609b9 commit 3ab2d9c

File tree

5 files changed

+118
-26
lines changed

5 files changed

+118
-26
lines changed

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

+22-16
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ public class KubernetesDependentResourceConfig<R> {
1616

1717
public static final boolean DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA = true;
1818

19-
private Set<String> namespaces = Constants.SAME_AS_CONTROLLER_NAMESPACES_SET;
20-
private String labelSelector = NO_VALUE_SET;
21-
private boolean namespacesWereConfigured = false;
22-
private boolean createResourceOnlyIfNotExistingWithSSA;
23-
private ResourceDiscriminator<R, ?> resourceDiscriminator;
24-
private Boolean useSSA;
25-
26-
private OnAddFilter<R> onAddFilter;
27-
28-
private OnUpdateFilter<R> onUpdateFilter;
29-
30-
private OnDeleteFilter<R> onDeleteFilter;
31-
32-
private GenericFilter<R> genericFilter;
33-
34-
public KubernetesDependentResourceConfig() {}
19+
private Set<String> namespaces;
20+
private String labelSelector;
21+
private final boolean namespacesWereConfigured;
22+
private final boolean createResourceOnlyIfNotExistingWithSSA;
23+
private final ResourceDiscriminator<R, ?> resourceDiscriminator;
24+
private final Boolean useSSA;
25+
26+
private final OnAddFilter<R> onAddFilter;
27+
private final OnUpdateFilter<R> onUpdateFilter;
28+
private final OnDeleteFilter<R> onDeleteFilter;
29+
private final GenericFilter<R> genericFilter;
30+
31+
public KubernetesDependentResourceConfig() {
32+
this(Constants.SAME_AS_CONTROLLER_NAMESPACES_SET, NO_VALUE_SET, true,
33+
DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA,
34+
null, null, null,
35+
null, null, null);
36+
}
3537

3638
public KubernetesDependentResourceConfig(Set<String> namespaces,
3739
String labelSelector,
@@ -54,12 +56,16 @@ public KubernetesDependentResourceConfig(Set<String> namespaces,
5456
this.useSSA = useSSA;
5557
}
5658

59+
// use builder instead
60+
@Deprecated(forRemoval = true)
5761
public KubernetesDependentResourceConfig(Set<String> namespaces, String labelSelector) {
5862
this(namespaces, labelSelector, true, DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA,
5963
null, null, null,
6064
null, null, null);
6165
}
6266

67+
// use builder instead
68+
@Deprecated(forRemoval = true)
6369
public KubernetesDependentResourceConfig<R> setLabelSelector(String labelSelector) {
6470
this.labelSelector = labelSelector;
6571
return this;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
2+
3+
import java.util.Set;
4+
5+
import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator;
6+
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
7+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
8+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
9+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
10+
11+
public final class KubernetesDependentResourceConfigBuilder<R> {
12+
13+
private Set<String> namespaces;
14+
private String labelSelector;
15+
private boolean createResourceOnlyIfNotExistingWithSSA;
16+
private ResourceDiscriminator<R, ?> resourceDiscriminator;
17+
private Boolean useSSA;
18+
private OnAddFilter<R> onAddFilter;
19+
private OnUpdateFilter<R> onUpdateFilter;
20+
private OnDeleteFilter<R> onDeleteFilter;
21+
private GenericFilter<R> genericFilter;
22+
23+
public KubernetesDependentResourceConfigBuilder() {}
24+
25+
public static <R> KubernetesDependentResourceConfigBuilder<R> aKubernetesDependentResourceConfig() {
26+
return new KubernetesDependentResourceConfigBuilder<>();
27+
}
28+
29+
public KubernetesDependentResourceConfigBuilder<R> withNamespaces(Set<String> namespaces) {
30+
this.namespaces = namespaces;
31+
return this;
32+
}
33+
34+
public KubernetesDependentResourceConfigBuilder<R> withLabelSelector(String labelSelector) {
35+
this.labelSelector = labelSelector;
36+
return this;
37+
}
38+
39+
public KubernetesDependentResourceConfigBuilder<R> withCreateResourceOnlyIfNotExistingWithSSA(
40+
boolean createResourceOnlyIfNotExistingWithSSA) {
41+
this.createResourceOnlyIfNotExistingWithSSA = createResourceOnlyIfNotExistingWithSSA;
42+
return this;
43+
}
44+
45+
public KubernetesDependentResourceConfigBuilder<R> withResourceDiscriminator(
46+
ResourceDiscriminator<R, ?> resourceDiscriminator) {
47+
this.resourceDiscriminator = resourceDiscriminator;
48+
return this;
49+
}
50+
51+
public KubernetesDependentResourceConfigBuilder<R> withUseSSA(Boolean useSSA) {
52+
this.useSSA = useSSA;
53+
return this;
54+
}
55+
56+
public KubernetesDependentResourceConfigBuilder<R> withOnAddFilter(OnAddFilter<R> onAddFilter) {
57+
this.onAddFilter = onAddFilter;
58+
return this;
59+
}
60+
61+
public KubernetesDependentResourceConfigBuilder<R> withOnUpdateFilter(
62+
OnUpdateFilter<R> onUpdateFilter) {
63+
this.onUpdateFilter = onUpdateFilter;
64+
return this;
65+
}
66+
67+
public KubernetesDependentResourceConfigBuilder<R> withOnDeleteFilter(
68+
OnDeleteFilter<R> onDeleteFilter) {
69+
this.onDeleteFilter = onDeleteFilter;
70+
return this;
71+
}
72+
73+
public KubernetesDependentResourceConfigBuilder<R> withGenericFilter(
74+
GenericFilter<R> genericFilter) {
75+
this.genericFilter = genericFilter;
76+
return this;
77+
}
78+
79+
public KubernetesDependentResourceConfig<R> build() {
80+
return new KubernetesDependentResourceConfig<>(namespaces, labelSelector, false,
81+
createResourceOnlyIfNotExistingWithSSA, resourceDiscriminator, useSSA, onAddFilter,
82+
onUpdateFilter, onDeleteFilter, genericFilter);
83+
}
84+
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
2121
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
2222
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
23+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfigBuilder;
2324
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
2425

2526
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -327,7 +328,10 @@ void replaceNamedDependentResourceConfigShouldWork() {
327328
final var overridden = ControllerConfigurationOverrider.override(configuration)
328329
.replacingNamedDependentResourceConfig(
329330
DependentResource.defaultNameFor(ReadOnlyDependent.class),
330-
new KubernetesDependentResourceConfig(Set.of(overriddenNS), labelSelector))
331+
new KubernetesDependentResourceConfigBuilder<>()
332+
.withNamespaces(Set.of(overriddenNS))
333+
.withLabelSelector(labelSelector)
334+
.build())
331335
.build();
332336
dependents = overridden.getDependentResources();
333337
dependentSpec = dependents.stream().filter(dr -> dr.getName().equals(dependentResourceName))

sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageDependentsWorkflowReconciler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
1818
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
1919
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
20-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
20+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfigBuilder;
2121
import io.javaoperatorsdk.operator.processing.dependent.workflow.Workflow;
2222
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowBuilder;
2323
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
@@ -91,8 +91,8 @@ private void initDependentResources(KubernetesClient client) {
9191

9292
Arrays.asList(configMapDR, deploymentDR, serviceDR, ingressDR).forEach(dr -> {
9393
dr.setKubernetesClient(client);
94-
dr.configureWith(new KubernetesDependentResourceConfig()
95-
.setLabelSelector(DEPENDENT_RESOURCE_LABEL_SELECTOR));
94+
dr.configureWith(new KubernetesDependentResourceConfigBuilder()
95+
.withLabelSelector(DEPENDENT_RESOURCE_LABEL_SELECTOR).build());
9696
});
9797
}
9898

sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageStandaloneDependentsReconciler.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.fabric8.kubernetes.client.KubernetesClient;
1414
import io.javaoperatorsdk.operator.api.reconciler.*;
1515
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
16-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
16+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfigBuilder;
1717
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
1818
import io.javaoperatorsdk.operator.sample.customresource.WebPage;
1919
import io.javaoperatorsdk.operator.sample.dependentresource.ConfigMapDependentResource;
@@ -79,21 +79,19 @@ public ErrorStatusUpdateControl<WebPage> updateErrorStatus(
7979
return handleError(resource, e);
8080
}
8181

82-
@SuppressWarnings("unchecked")
82+
@SuppressWarnings({"unchecked", "rawtypes"})
8383
private void createDependentResources(KubernetesClient client) {
8484
this.configMapDR = new ConfigMapDependentResource();
8585
this.deploymentDR = new DeploymentDependentResource();
8686
this.serviceDR = new ServiceDependentResource();
8787
this.ingressDR = new IngressDependentResource();
8888

89-
9089
Arrays.asList(configMapDR, deploymentDR, serviceDR, ingressDR).forEach(dr -> {
9190
dr.setKubernetesClient(client);
92-
dr.configureWith(new KubernetesDependentResourceConfig()
93-
.setLabelSelector(SELECTOR + "=true"));
91+
dr.configureWith(new KubernetesDependentResourceConfigBuilder()
92+
.withLabelSelector(SELECTOR + "=true").build());
9493
});
9594
}
9695

9796

98-
9997
}

0 commit comments

Comments
 (0)