Skip to content

Commit 904275a

Browse files
authored
Add ability to provide a preconfigured KubernetesClient (#1507)
1 parent 583e79c commit 904275a

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/AbstractOperatorExtension.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
1818
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
1919
import io.fabric8.kubernetes.client.KubernetesClient;
20-
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
2120
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
2221
import io.fabric8.kubernetes.client.dsl.Resource;
2322
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
@@ -50,9 +49,9 @@ protected AbstractOperatorExtension(
5049
Duration infrastructureTimeout,
5150
boolean oneNamespacePerClass,
5251
boolean preserveNamespaceOnError,
53-
boolean waitForNamespaceDeletion) {
54-
55-
this.kubernetesClient = new KubernetesClientBuilder().build();
52+
boolean waitForNamespaceDeletion,
53+
KubernetesClient kubernetesClient) {
54+
this.kubernetesClient = kubernetesClient;
5655
this.configurationService = configurationService;
5756
this.infrastructure = infrastructure;
5857
this.infrastructureTimeout = infrastructureTimeout;

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/ClusterDeployedOperatorExtension.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import io.fabric8.kubernetes.api.model.HasMetadata;
2020
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
21+
import io.fabric8.kubernetes.client.KubernetesClient;
22+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
2123
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
2224

2325
public class ClusterDeployedOperatorExtension extends AbstractOperatorExtension {
@@ -36,10 +38,12 @@ private ClusterDeployedOperatorExtension(
3638
Duration infrastructureTimeout,
3739
boolean preserveNamespaceOnError,
3840
boolean waitForNamespaceDeletion,
39-
boolean oneNamespacePerClass) {
41+
boolean oneNamespacePerClass,
42+
KubernetesClient kubernetesClient) {
4043
super(configurationService, infrastructure, infrastructureTimeout, oneNamespacePerClass,
4144
preserveNamespaceOnError,
42-
waitForNamespaceDeletion);
45+
waitForNamespaceDeletion,
46+
kubernetesClient);
4347
this.operatorDeployment = operatorDeployment;
4448
this.operatorDeploymentTimeout = operatorDeploymentTimeout;
4549
}
@@ -104,6 +108,7 @@ protected void deleteOperator() {
104108
public static class Builder extends AbstractBuilder<Builder> {
105109
private final List<HasMetadata> operatorDeployment;
106110
private Duration deploymentTimeout;
111+
private KubernetesClient kubernetesClient;
107112

108113
protected Builder() {
109114
super();
@@ -135,6 +140,11 @@ public Builder withOperatorDeployment(HasMetadata... hms) {
135140
return this;
136141
}
137142

143+
public Builder withKubernetesClient(KubernetesClient kubernetesClient) {
144+
this.kubernetesClient = kubernetesClient;
145+
return this;
146+
}
147+
138148
public ClusterDeployedOperatorExtension build() {
139149
return new ClusterDeployedOperatorExtension(
140150
configurationService,
@@ -144,7 +154,8 @@ public ClusterDeployedOperatorExtension build() {
144154
infrastructureTimeout,
145155
preserveNamespaceOnError,
146156
waitForNamespaceDeletion,
147-
oneNamespacePerClass);
157+
oneNamespacePerClass,
158+
kubernetesClient != null ? kubernetesClient : new KubernetesClientBuilder().build());
148159
}
149160
}
150161
}

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/LocallyRunOperatorExtension.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import io.fabric8.kubernetes.api.model.HasMetadata;
1818
import io.fabric8.kubernetes.client.CustomResource;
19+
import io.fabric8.kubernetes.client.KubernetesClient;
20+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
1921
import io.fabric8.kubernetes.client.LocalPortForward;
2022
import io.javaoperatorsdk.operator.Operator;
2123
import io.javaoperatorsdk.operator.ReconcilerUtils;
@@ -48,14 +50,16 @@ private LocallyRunOperatorExtension(
4850
Duration infrastructureTimeout,
4951
boolean preserveNamespaceOnError,
5052
boolean waitForNamespaceDeletion,
51-
boolean oneNamespacePerClass) {
53+
boolean oneNamespacePerClass,
54+
KubernetesClient kubernetesClient) {
5255
super(
5356
configurationService,
5457
infrastructure,
5558
infrastructureTimeout,
5659
oneNamespacePerClass,
5760
preserveNamespaceOnError,
58-
waitForNamespaceDeletion);
61+
waitForNamespaceDeletion,
62+
kubernetesClient);
5963
this.reconcilers = reconcilers;
6064
this.portForwards = portForwards;
6165
this.localPortForwards = new ArrayList<>(portForwards.size());
@@ -194,6 +198,7 @@ public static class Builder extends AbstractBuilder<Builder> {
194198
private final List<ReconcilerSpec> reconcilers;
195199
private final List<PortForwardSpec> portForwards;
196200
private final List<Class<? extends CustomResource>> additionalCustomResourceDefinitions;
201+
private KubernetesClient kubernetesClient;
197202

198203
protected Builder() {
199204
super();
@@ -243,6 +248,10 @@ public Builder withPortForward(String namespace, String labelKey, String labelVa
243248
return this;
244249
}
245250

251+
public Builder withKubernetesClient(KubernetesClient kubernetesClient) {
252+
this.kubernetesClient = kubernetesClient;
253+
return this;
254+
}
246255

247256
public Builder withAdditionalCustomResourceDefinition(
248257
Class<? extends CustomResource> customResource) {
@@ -260,7 +269,8 @@ public LocallyRunOperatorExtension build() {
260269
infrastructureTimeout,
261270
preserveNamespaceOnError,
262271
waitForNamespaceDeletion,
263-
oneNamespacePerClass);
272+
oneNamespacePerClass,
273+
kubernetesClient != null ? kubernetesClient : new KubernetesClientBuilder().build());
264274
}
265275
}
266276

0 commit comments

Comments
 (0)