Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic informers to kubernetes client #3228

Closed
rohanKanojia opened this issue Jun 8, 2021 · 0 comments · Fixed by #3233
Closed

Add support for dynamic informers to kubernetes client #3228

rohanKanojia opened this issue Jun 8, 2021 · 0 comments · Fixed by #3233
Assignees

Comments

@rohanKanojia
Copy link
Member

rohanKanojia commented Jun 8, 2021

With introduction of GenericKuberntesResource(equivalent of Unstructured[0] in golang client), we should now able to support dynamic informers[1] with SharedIndexInformer.sharedIndexInformerForCustomResource accepting CustomResourceDefinitionContext.

With just this minor change I was able to get below code working:

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 f2bf14ca1..8654c7d62 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
@@ -152,10 +152,8 @@ public class SharedInformerFactory extends BaseOperation {
    * @param <T> the type parameter (should extend {@link io.fabric8.kubernetes.api.model.HasMetadata} and implement {@link io.fabric8.kubernetes.api.model.Namespaced})
    * @param <L> the type's list parameter (should extend {@link io.fabric8.kubernetes.api.model.KubernetesResourceList}
    * @return the shared index informer
-   * @deprecated Since 5.x versions of client {@link CustomResourceDefinitionContext} are configured via annotations in CustomResource implementations, please use any of the alternative sharedIndexInformerForCustomResource methods
    */
-  @Deprecated
-  public synchronized <T extends CustomResource<?, ?>, L extends KubernetesResourceList<T>> SharedIndexInformer<T> sharedIndexInformerForCustomResource(
+  public synchronized <T extends HasMetadata, L extends KubernetesResourceList<T>> SharedIndexInformer<T> sharedIndexInformerForCustomResource(
     CustomResourceDefinitionContext customResourceContext, Class<T> apiTypeClass, Class<L> apiListTypeClass, long resyncPeriodInMillis) {
     KubernetesDeserializer.registerCustomKind(String.format("%s/%s",
       Objects.requireNonNull(customResourceContext.getGroup()),

Dynamic Informer Sample I tested:

SharedInformerFactory informerFactory = client.informers();
CustomResourceDefinitionContext context = new CustomResourceDefinitionContext.Builder()
        .withGroup("stable.example.com")
        .withVersion("v1")
        .withPlural("crontabs")
        .withScope("Namespaced")
        .build();

SharedIndexInformer<GenericKubernetesResource> informer = informerFactory.sharedIndexInformerForCustomResource(context, GenericKubernetesResource.class, GenericKubernetesResourceList.class, 60 * 1000L);
informer.addEventHandler(new ResourceEventHandler<>() {
    @Override
    public void onAdd(GenericKubernetesResource genericKubernetesResource) {
        System.out.printf("ADD %s", genericKubernetesResource.getMetadata().getName());
    }

    @Override
    public void onUpdate(GenericKubernetesResource genericKubernetesResource, GenericKubernetesResource t1) {
        System.out.printf("UPDATE %s", genericKubernetesResource.getMetadata().getName());
    }

    @Override
    public void onDelete(GenericKubernetesResource genericKubernetesResource, boolean b) {
        System.out.printf("DELETE %s", genericKubernetesResource.getMetadata().getName());
    }
});

informerFactory.startAllRegisteredInformers();

[0] https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/unstructured/unstructured.go#L41
[1] https://firehydrant.io/blog/dynamic-kubernetes-informers/

rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jun 9, 2021
…ient

Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jun 9, 2021
…ient

Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
@rohanKanojia rohanKanojia self-assigned this Jun 9, 2021
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jun 9, 2021
…ient

Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jun 10, 2021
…ient

Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jun 15, 2021
…ient

Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
manusa pushed a commit that referenced this issue Jun 15, 2021
Introduce a new method in SharedInformerFactory which will only accept
CustomResourceDefinitionContext, types would automatically be assumed
to be GenericKubernetesResource and GenericKubernetesResourceList.

Till now we were deciding whether a resource is namespaced or not
depending upon whether it implements Namespaced interface or not. But
having dynamic informers required change to consider `scope` field in
CustomResourceDefinitionContext as well, hence I introduced a new class
SharedInformerOperationsImpl which overrides `isResourceNamespaced()` so
that it can be configured from calling methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant