1
1
package io .javaoperatorsdk .operator .junit ;
2
2
3
- import java .io .InputStream ;
4
- import java .util .ArrayList ;
5
- import java .util .List ;
6
- import java .util .Locale ;
7
- import java .util .concurrent .TimeUnit ;
8
- import java .util .stream .Collectors ;
9
-
10
- import org .awaitility .Awaitility ;
11
- import org .junit .jupiter .api .extension .AfterAllCallback ;
12
- import org .junit .jupiter .api .extension .AfterEachCallback ;
13
- import org .junit .jupiter .api .extension .BeforeAllCallback ;
14
- import org .junit .jupiter .api .extension .BeforeEachCallback ;
15
- import org .junit .jupiter .api .extension .ExtensionContext ;
16
- import org .slf4j .Logger ;
17
- import org .slf4j .LoggerFactory ;
3
+ import static io .javaoperatorsdk .operator .api .config .ControllerConfigurationOverrider .override ;
18
4
19
5
import io .fabric8 .kubernetes .api .model .HasMetadata ;
20
6
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
32
18
import io .javaoperatorsdk .operator .api .config .Version ;
33
19
import io .javaoperatorsdk .operator .processing .ConfiguredController ;
34
20
import io .javaoperatorsdk .operator .processing .retry .Retry ;
35
-
36
- import static io .javaoperatorsdk .operator .api .config .ControllerConfigurationOverrider .override ;
21
+ import java .io .InputStream ;
22
+ import java .util .ArrayList ;
23
+ import java .util .List ;
24
+ import java .util .Locale ;
25
+ import java .util .concurrent .TimeUnit ;
26
+ import java .util .stream .Collectors ;
27
+ import org .awaitility .Awaitility ;
28
+ import org .junit .jupiter .api .extension .AfterAllCallback ;
29
+ import org .junit .jupiter .api .extension .AfterEachCallback ;
30
+ import org .junit .jupiter .api .extension .BeforeAllCallback ;
31
+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
32
+ import org .junit .jupiter .api .extension .ExtensionContext ;
33
+ import org .slf4j .Logger ;
34
+ import org .slf4j .LoggerFactory ;
37
35
38
36
public class OperatorExtension
39
37
implements HasKubernetesClient ,
@@ -67,6 +65,11 @@ private OperatorExtension(
67
65
this .waitForNamespaceDeletion = waitForNamespaceDeletion ;
68
66
}
69
67
68
+ /**
69
+ * Creates a {@link Builder} to set up an {@link OperatorExtension} instance.
70
+ *
71
+ * @return the builder.
72
+ */
70
73
public static Builder builder () {
71
74
return new Builder ();
72
75
}
@@ -96,17 +99,32 @@ public KubernetesClient getKubernetesClient() {
96
99
return kubernetesClient ;
97
100
}
98
101
102
+ /**
103
+ * Returns the test namespace.
104
+ *
105
+ * @return the namespace name.
106
+ */
99
107
public String getNamespace () {
100
108
return namespace ;
101
109
}
102
110
111
+ /**
112
+ * The list of controllers known by the operator.
113
+ *
114
+ * @return the list of {@link ResourceController}.
115
+ */
103
116
@ SuppressWarnings ({"rawtypes" })
104
117
public List <ResourceController > getControllers () {
105
118
return operator .getControllers ().stream ()
106
119
.map (ConfiguredController ::getController )
107
120
.collect (Collectors .toUnmodifiableList ());
108
121
}
109
122
123
+ /**
124
+ * The list of controllers of the give {@code type} known by the operator.
125
+ *
126
+ * @return the list of {@link ResourceController} matching the required type.
127
+ */
110
128
@ SuppressWarnings ({"rawtypes" })
111
129
public <T extends ResourceController > T getControllerOfType (Class <T > type ) {
112
130
return operator .getControllers ().stream ()
@@ -123,22 +141,42 @@ public <T extends HasMetadata> NonNamespaceOperation<T, KubernetesResourceList<T
123
141
return kubernetesClient .resources (type ).inNamespace (namespace );
124
142
}
125
143
126
- public <T extends HasMetadata > T getNamedResource (Class <T > type , String name ) {
144
+ /**
145
+ * Lookup a resource given its {@code type} and {@code name} in the current test namespace.
146
+ *
147
+ * @param type Class for resource
148
+ * @param name the name of the resource
149
+ * @param <T> T type represents resource type
150
+ * @return The resource or null if it does not exist
151
+ */
152
+ public <T extends HasMetadata > T get (Class <T > type , String name ) {
127
153
return kubernetesClient .resources (type ).inNamespace (namespace ).withName (name ).get ();
128
154
}
129
155
156
+ /**
157
+ * Creates a resource in the current test namespace.
158
+ *
159
+ * @param type Class for resource
160
+ * @param resource the resource to create
161
+ * @param <T> T type represents resource type
162
+ * @return The resource or null if it does not exist
163
+ */
130
164
public <T extends HasMetadata > T create (Class <T > type , T resource ) {
131
165
return kubernetesClient .resources (type ).inNamespace (namespace ).create (resource );
132
166
}
133
167
168
+ /**
169
+ * Replaces a resource in the current test namespace.
170
+ *
171
+ * @param type Class for resource
172
+ * @param resource the resource to create
173
+ * @param <T> T type represents resource type
174
+ * @return The resource or null if it does not exist
175
+ */
134
176
public <T extends HasMetadata > T replace (Class <T > type , T resource ) {
135
177
return kubernetesClient .resources (type ).inNamespace (namespace ).replace (resource );
136
178
}
137
179
138
- public <T extends HasMetadata > T get (Class <T > type , String name ) {
139
- return kubernetesClient .resources (type ).inNamespace (namespace ).withName (name ).get ();
140
- }
141
-
142
180
143
181
@ SuppressWarnings ("unchecked" )
144
182
protected void before (ExtensionContext context ) {
@@ -225,33 +263,71 @@ protected Builder() {
225
263
true );
226
264
}
227
265
266
+ /**
267
+ * Configures if the test namespace should be preserved in case of error for troubleshooting.
268
+ *
269
+ * @param value true if the namespace should be preserved.
270
+ * @return this builder
271
+ */
228
272
public Builder preserveNamespaceOnError (boolean value ) {
229
273
this .preserveNamespaceOnError = value ;
230
274
return this ;
231
275
}
232
276
277
+ /**
278
+ * Configures if the extension should wait for the test namespace deletion.
279
+ *
280
+ * @param value true if the waiting for namespace deletion is required.
281
+ * @return this builder
282
+ */
233
283
public Builder waitForNamespaceDeletion (boolean value ) {
234
284
this .waitForNamespaceDeletion = value ;
235
285
return this ;
236
286
}
237
287
288
+ /**
289
+ * Defines the {@link ConfigurationService} the operator should use.
290
+ *
291
+ * @param value the {@link ConfigurationService}.
292
+ * @return this builder
293
+ */
238
294
public Builder withConfigurationService (ConfigurationService value ) {
239
295
configurationService = value ;
240
296
return this ;
241
297
}
242
298
299
+ /**
300
+ * Add a {@link ResourceController} to the operator.
301
+ *
302
+ * @param value the {@link ResourceController}
303
+ * @return this builder
304
+ */
243
305
@ SuppressWarnings ("rawtypes" )
244
306
public Builder withController (ResourceController value ) {
245
307
controllers .add (new ControllerSpec (value , null ));
246
308
return this ;
247
309
}
248
310
311
+ /**
312
+ * Add a {@link ResourceController} to the operator with a {@link Retry} policy.
313
+ *
314
+ * @param value the {@link ResourceController}
315
+ * @param retry the {@link Retry} policy.
316
+ * @return this builder
317
+ */
249
318
@ SuppressWarnings ("rawtypes" )
250
319
public Builder withController (ResourceController value , Retry retry ) {
251
320
controllers .add (new ControllerSpec (value , retry ));
252
321
return this ;
253
322
}
254
323
324
+ /**
325
+ * Add a {@link ResourceController} to the operator by providing its class name. The controller
326
+ * is instantiated using the default empty constructor.
327
+ *
328
+ * @param value the {@link ResourceController} type.
329
+ * @return this builder
330
+ */
255
331
@ SuppressWarnings ("rawtypes" )
256
332
public Builder withController (Class <? extends ResourceController > value ) {
257
333
try {
@@ -262,6 +338,11 @@ public Builder withController(Class<? extends ResourceController> value) {
262
338
return this ;
263
339
}
264
340
341
+ /**
342
+ * Build a new {@link OperatorExtension} instance.
343
+ *
344
+ * @return a new {@link OperatorExtension} instance.
345
+ */
265
346
public OperatorExtension build () {
266
347
return new OperatorExtension (
267
348
configurationService ,
0 commit comments