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

SharedIndexInformer with label filter only watching local Namespace #2801

Closed
buehlmann opened this issue Feb 9, 2021 · 5 comments
Closed
Assignees
Milestone

Comments

@buehlmann
Copy link

I'm trying to watch all events of ServiceAccounts with a specific Label in all Namespaces of a cluster. I register a SharedIndexInformer the following way:

            factory.sharedIndexInformerFor(
                    ServiceAccount.class,
                    ServiceAccountList.class,
                    new OperationContext().withLabels(Map.of(syncTokenLabelKey, syncTokenLabelValue)),
                    RESYNC_PERIOD_IN_MILLIS)
                    .addEventHandler(serviceAccountController);

But i get only informed for the configured Namespace instead of all Namespaces of the cluster. I'm testing against a local CRC OCP 4.5, run the app with mvn quarkus:dev using Quarkus 1.11.1, K8s Client 5.0.0.

@buehlmann
Copy link
Author

Update: It works if I create the OperationContext like that:

new OperationContext()
        .withIsNamespaceConfiguredFromGlobalConfig(true)
        .withLabels(Map.of(syncTokenLabelKey, syncTokenLabelValue)),

@manusa manusa added this to the 5.2.0 milestone Mar 3, 2021
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Mar 11, 2021
…Selectors

+ Added test for SharedIndexInformer for a ServiceAccount type to monitor
  events filtered with labels across all namespaces and in a particular
  namespace to verify that the use case actually works on master.
+ Minor Cleanup in DefaultSharedIndexInfomerTest to avoid code
  duplication while setting MockServer expectations

Related to fabric8io#2801
@rohanKanojia
Copy link
Member

I wasn't able to reproduce this on master. I've added two tests for SharedIndexInformer for filter with labels in all namespace and in some specific namespace in #2895

manusa pushed a commit that referenced this issue Mar 12, 2021
…Selectors

+ Added test for SharedIndexInformer for a ServiceAccount type to monitor
  events filtered with labels across all namespaces and in a particular
  namespace to verify that the use case actually works on master.
+ Minor Cleanup in DefaultSharedIndexInfomerTest to avoid code
  duplication while setting MockServer expectations

Related to #2801
@manusa
Copy link
Member

manusa commented Mar 12, 2021

The way request URLs are built for Watch requests has changed in the recent versions (however, you report the issue in 5.x)

This fix #2671 should be directly related with the issue which seems to be related to #2668 for the field/selector part.

It also relates to: #2714

@manusa manusa modified the milestones: 5.2.0, 5.3.0 Mar 12, 2021
@rohanKanojia
Copy link
Member

This issue seems to be fixed via this PR : #2880 . Specifically this line:

context.isNamespaceFromGlobalConfig() ? context.isNamespaceFromGlobalConfig() : isNamespaceFromGlobalConfig(),

Here we while merging OperationContexts we pick isNamespaceFromGlobalConfig if it's set to true in provided OperationContext argument. Otherwise, we just take whatever value is currently present in the current object.

Earlier we were just assigning argument value to current OperationContext's field, see v5.1.1 version

I can confirm that my minikube's current context contained a namespace in it:

- context:
    cluster: minikube
    extensions:
    - extension:
        last-update: Fri, 12 Mar 2021 15:45:33 IST
        provider: minikube.sigs.k8s.io
        version: v1.17.1
      name: context_info
    namespace: default
    user: minikube
  name: minikube

I tested it with this code:

        try (KubernetesClient client = new DefaultKubernetesClient()) {
            SharedInformerFactory factory = client.informers();

            SharedIndexInformer<ServiceAccount> saInformer = factory.sharedIndexInformerFor(ServiceAccount.class,
                    new OperationContext().withLabels(Collections.singletonMap("foo", "bar")),
                    10 * 60 * 1000);

            saInformer.addEventHandler(new ResourceEventHandler<ServiceAccount>() {
                @Override
                public void onAdd(ServiceAccount serviceAccount) {
                    System.out.printf("ADDED %s/%s\n", serviceAccount.getMetadata().getNamespace(), serviceAccount.getMetadata().getName());
                }

                @Override
                public void onUpdate(ServiceAccount serviceAccount, ServiceAccount t1) {
                    System.out.printf("MODIFIED %s/%s\n", serviceAccount.getMetadata().getNamespace(), serviceAccount.getMetadata().getName());

                }

                @Override
                public void onDelete(ServiceAccount serviceAccount, boolean b) {
                    System.out.printf("DELETED %s/%s\n", serviceAccount.getMetadata().getNamespace(), serviceAccount.getMetadata().getName());
                }
            });

            factory.startAllRegisteredInformers();

            Thread.sleep(30 * 160 * 1000L);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

It prints the following output:

ADDED default/build-robot
MODIFIED default/build-robot
ADDED default/tutorial-service
MODIFIED default/tutorial-service
ADDED rokumar/build-robot
MODIFIED rokumar/build-robot
ADDED rokumar/tutorial-service
MODIFIED rokumar/tutorial-service

@manusa
Copy link
Member

manusa commented May 24, 2021

I'm closing this issue since it seems to be fixed in our latest releases. Please, feel free to reopen in case the issue persists.

@manusa manusa closed this as completed May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants