5
5
6
6
import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
7
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
8
+ import io .fabric8 .kubernetes .api .model .Namespaced ;
8
9
import io .fabric8 .kubernetes .client .CustomResource ;
9
10
import io .fabric8 .kubernetes .client .KubernetesClientException ;
10
11
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
11
12
import io .fabric8 .kubernetes .client .dsl .Resource ;
12
- import io .fabric8 .kubernetes .client .dsl .internal .HasMetadataOperationsImpl ;
13
13
import io .javaoperatorsdk .operator .OperatorException ;
14
14
import io .javaoperatorsdk .operator .api .ObservedGenerationAware ;
15
15
import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
@@ -355,29 +355,29 @@ public CustomResourceFacade(
355
355
}
356
356
357
357
public R getResource (String namespace , String name ) {
358
- return resourceOperation .inNamespace (namespace ).withName (name ).get ();
358
+ if (namespace != null ) {
359
+ return resourceOperation .inNamespace (namespace ).withName (name ).get ();
360
+ } else {
361
+ return resourceOperation .withName (name ).get ();
362
+ }
359
363
}
360
364
361
365
public R updateResource (R resource ) {
362
366
log .debug (
363
367
"Trying to replace resource {}, version: {}" ,
364
368
getName (resource ),
365
369
resource .getMetadata ().getResourceVersion ());
366
- return resourceOperation
367
- .inNamespace (resource .getMetadata ().getNamespace ())
368
- .resource (resource )
369
- .lockResourceVersion (resource .getMetadata ().getResourceVersion ())
370
+
371
+ return resource (resource ).lockResourceVersion (resource .getMetadata ().getResourceVersion ())
370
372
.replace ();
371
373
}
372
374
373
375
@ SuppressWarnings ({"rawtypes" , "unchecked" })
374
376
public R updateStatus (R resource ) {
375
377
log .trace ("Updating status for resource: {}" , resource );
376
- HasMetadataOperationsImpl hasMetadataOperation = (HasMetadataOperationsImpl ) resourceOperation
377
- .inNamespace (resource .getMetadata ().getNamespace ())
378
- .withName (getName (resource ))
379
- .lockResourceVersion (resource .getMetadata ().getResourceVersion ());
380
- return (R ) hasMetadataOperation .replaceStatus (resource );
378
+ return resource (resource )
379
+ .lockResourceVersion ()
380
+ .replaceStatus ();
381
381
}
382
382
383
383
public R patchStatus (R resource , R originalResource ) {
@@ -387,15 +387,19 @@ public R patchStatus(R resource, R originalResource) {
387
387
originalResource .getMetadata ().setResourceVersion (null );
388
388
resource .getMetadata ().setResourceVersion (null );
389
389
try {
390
- return resourceOperation
391
- .inNamespace (resource .getMetadata ().getNamespace ())
392
- .resource (originalResource )
390
+ return resource (originalResource )
393
391
.editStatus (r -> resource );
394
392
} finally {
395
393
// restore initial resource version
396
394
originalResource .getMetadata ().setResourceVersion (resourceVersion );
397
395
resource .getMetadata ().setResourceVersion (resourceVersion );
398
396
}
399
397
}
398
+
399
+ private Resource <R > resource (R resource ) {
400
+ return resource instanceof Namespaced ? resourceOperation
401
+ .inNamespace (resource .getMetadata ().getNamespace ())
402
+ .resource (resource ) : resourceOperation .resource (resource );
403
+ }
400
404
}
401
405
}
0 commit comments