Is there a way to list all resources created by a specific operator and their status?

2.8k views Asked by At

I use config connector https://cloud.google.com/config-connector/docs/overview

I create gcp resources with CRDs that config connector provides:

kind: IAMServiceAccount
kind: StorageBucket
etc

Now what I'd really like is to be able to get a simple list of each resource and its status (if it was created successfully or not). Where each resource is a single line that's something like: kind, name, status, etc

Is there a way with kubectl to get a list of all resources that were created by an operator like this? I suppose I could manually label all these resources and try to select with a label but I really don't want to do that

Edit

Per the comment I could do this, but curious if there is a less unwieldy command

kubectl get crds --selector cnrm.cloud.google.com/managed-by-kcc=true \
    -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -n 1 \
    kubectl get -Ao jsonpath='{range .items[*]}{" Kind: "}{@.kind}{"Name: "}{@.metadata.name}{" Status: "}{@.status.conditions[].status}{" Reason: "}{@.status.conditions[].reason}{"\n"}{end}' --ignore-not-found
2

There are 2 answers

2
Dawid Kruk On BEST ANSWER

I've made a bit of research on this topic and I found 2 possible solutions to retrieve all the resources that were created by config-connector:

  • $ kubectl api-resources way
  • $ kubectl get-all/ketall way with labels (please see the explanation as it's not installed by default)

The discussion that is referencing similar issue can be found here:


$ kubectl api-resources

As pointed in the comment I made you can use the following expression:

kubectl get crds --selector cnrm.cloud.google.com/managed-by-kcc=true -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -n 1 kubectl get --ignore-not-found

Dissecting this solution:

  • kubectl get crds --selector cnrm.cloud.google.com/managed-by-kcc=true
    • retrieve the Customer Resource Definitions that have a matching selector
  • -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
    • use the jsonpath to retrieve only the value stored in .metadata.name key (get the name of the crd)
  • | xargs -n 1 kubectl get
    • pipe the output to the xargs and use each CRD retrieved from previous command to run $ kubectl get <RESOURCE>
  • --ignore-not-found
    • do not display a message about missing resource

This command could also be altered to suit the specific needs as it's shown in the question.

A side note!

Similar command is referenced in the github link I pasted above:


$ kubectl get-all/ketall

Above commands can be used to retrieve all of the resources in the cluster. They are not available in default kubectl and they need additional configuration.

More reference about the installation can be found in this github page:

Using the approach described in the official Kubernetes documentation:

Labels are intended to be used to specify identifying attributes of objects

Kubernetes.io: Docs: Concepts: Overview: Working with objects: Labels

You can label those resources created by config connector (I know that you would like to avoid it) and look for this resources like:

  • $ kubectl get-all -l look=here
NAME                                                                      NAMESPACE         AGE
storagebucket.storage.cnrm.cloud.google.com/config-connector-bucket       config-connector  135m
storagebucket.storage.cnrm.cloud.google.com/config-connector-bucket-test  config-connector  13s

This resources have the .metadata.labels.look=here added to it's definitions.


Additional resources:

0
Ilya Trofimov On

There is also a way suggested in GCP config-connector docs:

kubectl get gcp

from https://cloud.google.com/config-connector/docs/how-to/monitoring-your-resources#listing_all_resources