From 1f5061f1f04b046ed0fb40b2a9a7432b72b452cb Mon Sep 17 00:00:00 2001 From: shawkins Date: Tue, 22 Jun 2021 12:10:42 -0400 Subject: [PATCH] changing the generic resource method name also allowing inform to work off of withName --- .../kubernetes/client/KubernetesClient.java | 8 ++++---- .../fabric8/kubernetes/client/dsl/Resource.java | 2 +- .../client/dsl/base/BaseOperation.java | 12 +++++++++--- .../kubernetes/client/mock/InformTest.java | 17 ++++++++--------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java index 70b0a645471..ebf11c6fc74 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java @@ -161,12 +161,12 @@ public interface KubernetesClient extends Client { > MixedOperation> customResources(CustomResourceDefinitionContext crdContext, Class resourceType, Class listClass); /** - * Semi-Typed API for managing CustomResources. + * Semi-Typed API for managing {@link GenericKubernetesResource}s which can represent any resource. * - * @param crdContext CustomResourceDefinitionContext describes the core fields used to search for CustomResources - * @return returns a MixedOperation object with which you can do basic CustomResource operations + * @param crdContext CustomResourceDefinitionContext describes the core fields + * @return returns a MixedOperation object with which you can do basic operations */ - default MixedOperation> genericCustomResources( + default MixedOperation> genericKubernetesResources( CustomResourceDefinitionContext crdContext) { return customResources(crdContext, GenericKubernetesResource.class, GenericKubernetesResourceList.class); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/Resource.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/Resource.java index 153d248c09f..5c606e0b92a 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/Resource.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/Resource.java @@ -25,5 +25,5 @@ public interface Resource extends CreateOrReplaceable, CascadingEditReplacePatchDeletable, VersionWatchAndWaitable, DryRunable>, - Requirable, Readiable { + Requirable, Readiable, Informable { } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java index ac1b4992a2a..b7493e949d2 100755 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java @@ -72,6 +72,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -1206,16 +1207,20 @@ public Informable withIndexers(Map>> indexer @Override public SharedIndexInformer inform(ResourceEventHandler handler, long resync) { - // create an informer that will consume no additional threads beyond the underlying Watch + // convert the name into something listable + FilterWatchListDeletable baseOperation = + getName() == null ? this : withFields(Collections.singletonMap("metadata.name", getName())); + + // use the local context / namespace DefaultSharedIndexInformer informer = new DefaultSharedIndexInformer<>(getType(), new ListerWatcher() { @Override public L list(ListOptions params, String namespace, OperationContext context) { - return BaseOperation.this.list(); + return baseOperation.list(params); } @Override public Watch watch(ListOptions params, String namespace, OperationContext context, Watcher watcher) { - return BaseOperation.this.watch(params, watcher); + return baseOperation.watch(params, watcher); } }, resync, context, Runnable::run); // just run the event notification in the websocket thread if (handler != null) { @@ -1224,6 +1229,7 @@ public Watch watch(ListOptions params, String namespace, OperationContext contex if (indexers != null) { informer.addIndexers(indexers); } + // synchronous start list/watch must succeed in the calling thread informer.run(); return informer; } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/InformTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/InformTest.java index 43efa21430e..7456c43f1f5 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/InformTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/InformTest.java @@ -54,7 +54,7 @@ void testInformPodWithLabel() throws InterruptedException { .withResourceVersion("1").endMetadata().build(); server.expect() - .withPath("/api/v1/namespaces/test/pods?labelSelector=my-label") + .withPath("/api/v1/namespaces/test/pods?watch=false&labelSelector=my-label") .andReturn(HttpURLConnection.HTTP_OK, new PodListBuilder().withNewMetadata().withResourceVersion("1").endMetadata().withItems(pod1).build()) .once(); @@ -108,7 +108,7 @@ void testInformGeneric() throws InterruptedException { list.setItems(Arrays.asList(dummy)); server.expect() - .withPath("/apis/demos.fabric8.io/v1/namespaces/test/dummies?labelSelector=my-label") + .withPath("/apis/demos.fabric8.io/v1/namespaces/test/dummies?watch=false&labelSelector=my-label") .andReturn(HttpURLConnection.HTTP_OK, list) .once(); @@ -151,7 +151,7 @@ public void onUpdate(GenericKubernetesResource oldObj, GenericKubernetesResource .build(); SharedIndexInformer informer = - client.genericCustomResources(context).withLabel("my-label").inform(handler); + client.genericKubernetesResources(context).withLabel("my-label").inform(handler); assertTrue(deleteLatch.await(10, TimeUnit.SECONDS)); assertTrue(addLatch.await(10, TimeUnit.SECONDS)); @@ -169,13 +169,13 @@ void testGenericWithKnownType() throws InterruptedException { .withResourceVersion("1").endMetadata().build(); server.expect() - .withPath("/apis/demos.fabric8.io/v1/namespaces/test/dummies?labelSelector=my-label") + .withPath("/api/v1/namespaces/test/pods?watch=false&fieldSelector=metadata.name%3Dpod1") .andReturn(HttpURLConnection.HTTP_OK, new PodListBuilder().withNewMetadata().withResourceVersion("1").endMetadata().withItems(pod1).build()) .once(); server.expect() - .withPath("/apis/demos.fabric8.io/v1/namespaces/test/dummies?labelSelector=my-label&resourceVersion=1&watch=true") + .withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1%2Cmetadata.name%3Dpod1&resourceVersion=1&watch=true") .andUpgradeToWebSocket() .open() .waitFor(EVENT_WAIT_PERIOD_MS) @@ -206,15 +206,14 @@ public void onUpdate(GenericKubernetesResource oldObj, GenericKubernetesResource // When CustomResourceDefinitionContext context = new CustomResourceDefinitionContext.Builder() - .withKind("Dummy") + .withKind("Pod") .withScope("Namespaced") .withVersion("v1") - .withGroup("demos.fabric8.io") - .withPlural("dummies") + .withPlural("pods") .build(); SharedIndexInformer informer = - client.genericCustomResources(context).withLabel("my-label").inform(handler); + client.genericKubernetesResources(context).withName("pod1").inform(handler); assertTrue(deleteLatch.await(1000, TimeUnit.SECONDS)); assertTrue(addLatch.await(1000, TimeUnit.SECONDS));