Skip to content

Commit

Permalink
changing the generic resource method name
Browse files Browse the repository at this point in the history
also allowing inform to work off of withName
  • Loading branch information
shawkins authored and manusa committed Jun 23, 2021
1 parent 23b25dd commit 1f5061f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ public interface KubernetesClient extends Client {
<T extends HasMetadata, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> customResources(CustomResourceDefinitionContext crdContext, Class<T> resourceType, Class<L> 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<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> genericCustomResources(
default MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> genericKubernetesResources(
CustomResourceDefinitionContext crdContext) {
return customResources(crdContext, GenericKubernetesResource.class, GenericKubernetesResourceList.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface Resource<T> extends CreateOrReplaceable<T>,
CascadingEditReplacePatchDeletable<T>,
VersionWatchAndWaitable<T>,
DryRunable<WritableOperation<T>>,
Requirable<T>, Readiable {
Requirable<T>, Readiable, Informable<T> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1206,16 +1207,20 @@ public Informable<T> withIndexers(Map<String, Function<T, List<String>>> indexer

@Override
public SharedIndexInformer<T> inform(ResourceEventHandler<T> handler, long resync) {
// create an informer that will consume no additional threads beyond the underlying Watch
// convert the name into something listable
FilterWatchListDeletable<T, L> baseOperation =
getName() == null ? this : withFields(Collections.singletonMap("metadata.name", getName()));

// use the local context / namespace
DefaultSharedIndexInformer<T, L> informer = new DefaultSharedIndexInformer<>(getType(), new ListerWatcher<T, L>() {
@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<T> 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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -151,7 +151,7 @@ public void onUpdate(GenericKubernetesResource oldObj, GenericKubernetesResource
.build();

SharedIndexInformer<GenericKubernetesResource> 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));
Expand All @@ -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)
Expand Down Expand Up @@ -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<GenericKubernetesResource> 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));
Expand Down

0 comments on commit 1f5061f

Please sign in to comment.