diff --git a/CHANGELOG.md b/CHANGELOG.md index 356b8cf447..e9a466e5d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,12 @@ * Fix #2316: Cannot load resource from stream without apiVersion #### Improvements +* Fix #2331: Fixed documentation for namespaced informer for all custom types implementing `Namespaced` interface #### Dependency Upgrade -* Fix #2355: bump jandex from 2.1.3.Final to 2.2.0.Final -* Fix #2353: bump workflow action-setup- versions + kubernetes to 1.18.6 +* Fix #2360: bump mockito-core from 3.4.0 to 3.4.2 +* Fix #2355: bump jandex from 2.1.3.Final to 2.2.0.Final +* Fix #2353: chore: bump workflow action-setup versions + kubernetes to 1.18.6 #### New Features * Fix #2287: Add support for V1 and V1Beta1 CustomResourceDefinition diff --git a/doc/CHEATSHEET.md b/doc/CHEATSHEET.md index d6de7e2274..76b6d79e6a 100644 --- a/doc/CHEATSHEET.md +++ b/doc/CHEATSHEET.md @@ -1706,6 +1706,8 @@ spec: image: my-awesome-cron-image ``` For a CustomResource like this one, we should have a `CronTab` java class like this: + +**Note:** Please make sure that your CustomResource POJO is implementing `Namespaced` interface if it's a namespaced resource. Otherwise it would be considered a Cluster scoped resource. ``` /** * Copyright (C) 2015 Red Hat, Inc. @@ -1907,7 +1909,7 @@ Kubernetes Client also provides `SharedInformer` support in order to stay update ``` SharedInformerFactory sharedInformerFactory = client.informers(); ``` -- Create `SharedIndexInformer` for some Kubernetes Resource(requires resource's class, resource's list class, and resync period(when to check with server again while watching something): +- Create `SharedIndexInformer` for some Kubernetes Resource(requires resource's class, resource's list class, and resync period(when to check with server again while watching something). By default it watches in all namespaces.: ``` SharedIndexInformer podInformer = sharedInformerFactory.sharedIndexInformerFor(Pod.class, PodList.class, 30 * 1000L); podInformer.addEventHandler(new ResourceEventHandler() { @@ -1927,26 +1929,31 @@ podInformer.addEventHandler(new ResourceEventHandler() { } }); ``` -- Create `SharedIndexInformer` for some Custom Resource(in our case, `Dummy` resource provided in our [examples](https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/crds): +- Create `SharedIndexInformer` for some Custom Resource(in our case, `Dummy` resource provided in our [examples](https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/crds) . By default it watches in all namespaces. ``` +CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder() + .withVersion("v1") + .withScope("Namespaced") + .withGroup("demo.fabric8.io") + .withPlural("dummies") + .build(); SharedIndexInformer dummyInformer = sharedInformerFactory.sharedIndexInformerForCustomResource(crdContext, Dummy.class, DummyList.class, 1 * 60 * 1000); dummyInformer.addEventHandler(new ResourceEventHandler() { @Override - public void onAdd(Dummy pod) { - System.out.printf("%s dummy added\n", pod.getMetadata().getName()); + public void onAdd(Dummy dummy) { + System.out.printf("%s dummy added\n", dummy.getMetadata().getName()); } @Override - public void onUpdate(Dummy oldPod, Dummy newPod) { - System.out.printf("%s dummy updated\n", oldPod.getMetadata().getName()); + public void onUpdate(Dummy oldDummy, Dummy newDummy) { + System.out.printf("%s dummy updated\n", oldDummy.getMetadata().getName()); } @Override - public void onDelete(Dummy pod, boolean deletedFinalStateUnknown) { - System.out.printf("%s dummy deleted \n", pod.getMetadata().getName()); + public void onDelete(Dummy dummy, boolean deletedFinalStateUnknown) { + System.out.printf("%s dummy deleted \n", dummy.getMetadata().getName()); } }); - ``` - Create namespaced `SharedIndexInformer` (informers specific to a particular `Namespace`): ``` @@ -1975,6 +1982,37 @@ podInformer.addEventHandler(new ResourceEventHandler() { } }); ``` +- Create Namespaced Informer for a Custom Resource(**Note:** Your CustomResource POJO must implement `Namespaced` interface like the one used in this example: [Dummy.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/crds/Dummy.java)) +``` +CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder() + .withVersion("v1") + .withScope("Namespaced") + .withGroup("demo.fabric8.io") + .withPlural("dummies") + .build(); +SharedIndexInformer dummyInformer = sharedInformerFactory.sharedIndexInformerForCustomResource(crdContext, + Dummy.class, + DummyList.class, + new OperationContext().withNamespace("default"), // Namespace to watch + 1 * 60 * 1000); +dummyInformer.addEventHandler(new ResourceEventHandler() { + @Override + public void onAdd(Dummy dummy) { + System.out.printf("%s dummy added\n", dummy.getMetadata().getName()); + } + + @Override + public void onUpdate(Dummy oldDummy, Dummy newDummy) { + System.out.printf("%s dummy updated\n", oldDummy.getMetadata().getName()); + } + + @Override + public void onDelete(Dummy dummy, boolean deletedFinalStateUnknown) { + System.out.printf("%s dummy deleted \n", dummy.getMetadata().getName()); + } +}); +``` + - Start all registered informers: ``` sharedInformerFactory.startAllRegisteredInformers(); diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/SharedInformerFactory.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/SharedInformerFactory.java index 58e1d6ef93..714a5149c6 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/SharedInformerFactory.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/SharedInformerFactory.java @@ -81,8 +81,8 @@ public SharedInformerFactory(ExecutorService threadPool, OkHttpClient okHttpClie * @param apiTypeClass apiType class * @param apiListTypeClass api list type class * @param resyncPeriodInMillis resync period in milliseconds - * @param the type parameter - * @param the type's list parameter + * @param the type parameter (should extend {@link io.fabric8.kubernetes.api.model.HasMetadata} and implement {@link io.fabric8.kubernetes.api.model.Namespaced}) + * @param the type's list parameter (should extend {@link io.fabric8.kubernetes.api.model.KubernetesResourceList} * @return the shared index informer */ public synchronized > SharedIndexInformer sharedIndexInformerFor(Class apiTypeClass, Class apiListTypeClass, long resyncPeriodInMillis) { @@ -98,8 +98,8 @@ public synchronized > * @param apiTypeClass apiType class * @param apiListTypeClass api list type class * @param resyncPeriodInMillis resync period in milliseconds - * @param the type parameter - * @param the type's list parameter + * @param the type parameter (should extend {@link io.fabric8.kubernetes.api.model.HasMetadata} and implement {@link io.fabric8.kubernetes.api.model.Namespaced}) + * @param the type's list parameter (should extend {@link io.fabric8.kubernetes.api.model.KubernetesResourceList} * @return the shared index informer */ public synchronized > SharedIndexInformer sharedIndexInformerForCustomResource(CustomResourceDefinitionContext customResourceContext, Class apiTypeClass, Class apiListTypeClass, long resyncPeriodInMillis) { @@ -116,8 +116,8 @@ public synchronized > * @param apiListTypeClass api list type class * @param operationContext operation context * @param resyncPeriodInMillis resync period in milliseconds - * @param the type parameter - * @param the type's list parameter + * @param the type parameter (should extend {@link io.fabric8.kubernetes.api.model.HasMetadata} and implement {@link io.fabric8.kubernetes.api.model.Namespaced}) + * @param the type's list parameter (should extend {@link io.fabric8.kubernetes.api.model.KubernetesResourceList} * @return the shared index informer */ public synchronized > SharedIndexInformer sharedIndexInformerForCustomResource(CustomResourceDefinitionContext customResourceContext, Class apiTypeClass, Class apiListTypeClass, OperationContext operationContext, long resyncPeriodInMillis) { @@ -135,8 +135,8 @@ public synchronized > * @param apiListTypeClass api list type class * @param operationContext operation context * @param resyncPeriodInMillis resync period in milliseconds - * @param the type parameter - * @param the type's list parameter + * @param the type parameter (should extend {@link io.fabric8.kubernetes.api.model.HasMetadata} and implement {@link io.fabric8.kubernetes.api.model.Namespaced}) + * @param the type's list parameter (should extend {@link io.fabric8.kubernetes.api.model.KubernetesResourceList} * @return the shared index informer */ public synchronized > SharedIndexInformer sharedIndexInformerFor(Class apiTypeClass, Class apiListTypeClass, OperationContext operationContext, long resyncPeriodInMillis) {