9
9
10
10
import io .fabric8 .kubernetes .api .model .HasMetadata ;
11
11
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
12
+ import io .fabric8 .kubernetes .api .model .Namespaced ;
12
13
import io .fabric8 .kubernetes .client .CustomResource ;
13
14
import io .fabric8 .kubernetes .client .KubernetesClientException ;
14
15
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
16
+ import io .fabric8 .kubernetes .client .dsl .NonNamespaceOperation ;
15
17
import io .fabric8 .kubernetes .client .dsl .Resource ;
16
18
import io .fabric8 .kubernetes .client .dsl .internal .HasMetadataOperationsImpl ;
17
19
import io .fabric8 .kubernetes .client .utils .Serialization ;
@@ -170,7 +172,7 @@ private PostExecutionControl<P> handleErrorStatusHandler(P resource, P originalR
170
172
Exception e ) throws Exception {
171
173
if (isErrorStatusHandlerPresent ()) {
172
174
try {
173
- RetryInfo retryInfo = context .getRetryInfo ().orElse ( new RetryInfo () {
175
+ RetryInfo retryInfo = context .getRetryInfo ().orElseGet (() -> new RetryInfo () {
174
176
@ Override
175
177
public int getAttemptCount () {
176
178
return 0 ;
@@ -362,28 +364,28 @@ public CustomResourceFacade(
362
364
}
363
365
364
366
public R getResource (String namespace , String name ) {
365
- return resourceOperation .inNamespace (namespace ).withName (name ).get ();
367
+ if (namespace != null ) {
368
+ return resourceOperation .inNamespace (namespace ).withName (name ).get ();
369
+ } else {
370
+ return resourceOperation .withName (name ).get ();
371
+ }
366
372
}
367
373
368
374
public R updateResource (R resource ) {
369
- log .debug (
370
- "Trying to replace resource {}, version: {}" ,
371
- getName (resource ),
372
- resource .getMetadata ().getResourceVersion ());
373
- return resourceOperation
374
- .inNamespace (resource .getMetadata ().getNamespace ())
375
- .withName (getName (resource ))
376
- .lockResourceVersion (resource .getMetadata ().getResourceVersion ())
377
- .replace (resource );
375
+ final var resourceVersion = resource .getMetadata ().getResourceVersion ();
376
+ log .debug ("Trying to replace resource {}, version: {}" , getName (resource ), resourceVersion );
377
+
378
+ return resource (resource ).withName (resource .getMetadata ().getName ())
379
+ .lockResourceVersion (resourceVersion ).replace (resource );
378
380
}
379
381
380
382
@ SuppressWarnings ({"rawtypes" , "unchecked" })
381
383
public R updateStatus (R resource ) {
382
384
log .trace ("Updating status for resource: {}" , resource );
383
- HasMetadataOperationsImpl hasMetadataOperation = ( HasMetadataOperationsImpl ) resourceOperation
384
- . inNamespace ( resource . getMetadata (). getNamespace () )
385
- .withName (getName (resource ))
386
- .lockResourceVersion (resource .getMetadata ().getResourceVersion ());
385
+ HasMetadataOperationsImpl hasMetadataOperation =
386
+ ( HasMetadataOperationsImpl ) resource ( resource )
387
+ .withName (getName (resource ))
388
+ .lockResourceVersion (resource .getMetadata ().getResourceVersion ());
387
389
return (R ) hasMetadataOperation .replaceStatus (resource );
388
390
}
389
391
@@ -395,8 +397,7 @@ public R patchStatus(R resource, R originalResource) {
395
397
resource .getMetadata ().setResourceVersion (null );
396
398
try (var bis = new ByteArrayInputStream (
397
399
Serialization .asJson (originalResource ).getBytes (StandardCharsets .UTF_8 ))) {
398
- return resourceOperation
399
- .inNamespace (resource .getMetadata ().getNamespace ())
400
+ return resource (originalResource )
400
401
// will be simplified in fabric8 v6
401
402
.load (bis )
402
403
.editStatus (r -> resource );
@@ -408,5 +409,11 @@ public R patchStatus(R resource, R originalResource) {
408
409
resource .getMetadata ().setResourceVersion (resourceVersion );
409
410
}
410
411
}
412
+
413
+ private NonNamespaceOperation <R , KubernetesResourceList <R >, Resource <R >> resource (R resource ) {
414
+ return resource instanceof Namespaced
415
+ ? resourceOperation .inNamespace (resource .getMetadata ().getNamespace ())
416
+ : resourceOperation ;
417
+ }
411
418
}
412
419
}
0 commit comments