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

How to search for resources #887

Closed
dlaidlaw opened this issue Oct 18, 2017 · 15 comments · Fixed by #4284
Closed

How to search for resources #887

dlaidlaw opened this issue Oct 18, 2017 · 15 comments · Fixed by #4284

Comments

@dlaidlaw
Copy link

Is there a way to search for resources by label or annotation? For example, to search for all resources that include the label app=my-app. This would return Pods, Deployments, Services, StatefulSets, etc.

Thanks!

@stale
Copy link

stale bot commented Aug 6, 2019

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@stale stale bot added the status/stale label Aug 6, 2019
@rohanKanojia
Copy link
Member

still looks like a relevant issue to me. It would be nice if we could add something like client.resourceList().inNamespace("default").withLabels().get()

@stale
Copy link

stale bot commented Nov 5, 2019

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@stale stale bot added the status/stale label Nov 5, 2019
@stale stale bot closed this as completed Nov 12, 2019
@rohanKanojia rohanKanojia reopened this Nov 13, 2019
@stale stale bot removed the status/stale label Nov 13, 2019
@stale
Copy link

stale bot commented Feb 11, 2020

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@mwllgr
Copy link

mwllgr commented Mar 9, 2021

Hi,

I'd need this feature too. Is there any chance that this will get implemented in the future?

@shawkins
Copy link
Contributor

I don't think this will be directly implemented in the near future. The best workaround is to enumerate the resource classes, then apply the same logic, search for a label or whatever, over each:

Arrays.asList(Pod.class, ...).forEach(c -> client.resources(c).withLabel(...).list()...);

@metacosm
Copy link
Collaborator

metacosm commented Mar 1, 2022

I don't think this will be directly implemented in the near future. The best workaround is to enumerate the resource classes, then apply the same logic, search for a label or whatever, over each:

Arrays.asList(Pod.class, ...).forEach(c -> client.resources(c).withLabel(...).list()...);

This only works if you know what kind of resources to expect beforehand and if it's practical to enumerate them. This feels rather cumbersome for a use case that's quite common, imo.

@manusa
Copy link
Member

manusa commented Mar 1, 2022

This only works if you know what kind of resources to expect beforehand and if it's practical to enumerate them

As discussed internally, after the many improvements to the GenericKubernetesResource handling and the addition of the methods to list groups and resources, this should be much easier to implement now. We'll probably give it a spin in the next weeks.

@shawkins
Copy link
Contributor

After #4065 you should be able to walk the APIResources for each APIGroup and "v1", and perform a filtered list operation on those without a '/' in their name and that support the get verb - it appears that not everything that supports list reports supporting the list verb.

@shawkins
Copy link
Contributor

See https://github.com/fabric8io/kubernetes-client/blob/bb524af2542b0da1ed4d91ec272982fba2a57d91/kubernetes-itests/src/test/java/io/fabric8/kubernetes/PluralizeIT.java

Something like this:

    Map<String, APIResourceList> resources = new HashMap<>();
    resources.put("v1", client.getApiResources("v1"));
    client.getApiGroups().getGroups().stream().flatMap(g -> g.getVersions().stream()).forEach(v -> {
      resources.put(v.getGroupVersion(), client.getApiResources(v.getGroupVersion()));
    });

    resources.entrySet().forEach(e -> {
      e.getValue().getResources().stream()
          .filter(ar -> !ar.getName().contains("/"))
          // filtering specific to listing
          .filter(ar -> ar.getVerbs().contains("get") || ar.getVerbs().contains("list"))
          .map(ar -> client.genericKubernetesResources(e.getKey(), ar.getKind()))
          .forEach(o -> o.withLabel("x").list()); // or whatever business logic is needed to filter, then collect
    });

@manusa
Copy link
Member

manusa commented Apr 27, 2022

Since this one is extremely expensive, it's also a good candidate for a reactive approach.

@metacosm
Copy link
Collaborator

metacosm commented May 2, 2022

Also, this really isn't straightforward to do either… I think we should provide a top-level operation similar to #887 (comment)
This is getting into query-language territory, though… 😅

@manusa
Copy link
Member

manusa commented May 3, 2022

I think Steven was proposing the implementation of such method. If I'm not mistaken, I think we all agree to include this operation and no one is against it.

My argumentation was that this might be a very expensive operation in large clusters, so providing a reactive approach that can be stopped/cancelled at any point (e.g. the user retrieves what he/she is looking for), would be a good option for this. However, I'm not sure consumer-side cancellation would be possible with Java8 Streams only.

@shawkins shawkins added this to the 6.x milestone Jun 24, 2022
@shawkins
Copy link
Contributor

Looking at the openshift console the search functionality by label makes you select resource types - there is no option for selecting all. It would be sensible to follow the same approach, which alleviates much of the concern of how expensive the operation could be.

Specifically for filtering / searching it could look like:

client.search(ConfigMap.class).search("foo/v1", "kind")...withLabels(...).resources() - returns a Stream of KubernetesResource

But you could make an argument for composite operations in general:

client.resources(ConfigMap.class).resources("foo/v1", "kind")...withLabels(...).delete()

I'm not sure how easily that could be added to the existing resources entry point, or if a new one would be needed.

@shawkins
Copy link
Contributor

Looked at the above again - it's not really worth the effort vs just using lambdas against a list of types.

A more concrete proposal is https://github.com/fabric8io/kubernetes-client/compare/master...shawkins:search?expand=1 - this just assumes that it will be a while before we have reactive versions of the relevant operations available (we kind of do for list, but submitList is not exposed). This adds a visitor / walking entry point similar to java's file visiting:

kubernetesClient.visitResources((apiResource, operation) -> {
      if (apiResource.getVerbs().contains("get") || apiResource.getVerbs().contains("list")) {
        operation.with... // any MixedOperation method
      }
      return ApiVisitResult.CONTINUE;
    });

There are additional hooks for a visitor to skip / terminate on a given api name, or apiVersion.

shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2022
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2022
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2022
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2022
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2022
@manusa manusa linked a pull request Aug 9, 2022 that will close this issue
11 tasks
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Aug 9, 2022
@manusa manusa modified the milestones: 6.x, 6.1.0 Aug 10, 2022
manusa pushed a commit that referenced this issue Aug 10, 2022
* fix #887: adding visiting logic for walking api resources

* fix #887: refining the method names and parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Project FKC
  
Awaiting triage
Development

Successfully merging a pull request may close this issue.

6 participants