Operator-SDK: Create a group of objects that can be queried together

Operator-SDK CustomResourceDefinition categories groups

2 min read | by Jordi Prats

When you have several kubernetes objects (CRDs) that work together it can be useful to be able to query them all together to get a better idea of what's deployed without having to query all the individial resources. With operator-SDK we just need to annotate the objects.

To do so we just need to annotate the main struct with //+kubebuilder:resource, for example:

//+kubebuilder:resource:categories="postgres" 

We can add multiple categories by using the ; separator as follows:

//+kubebuilder:object:root=true //+kubebuilder:subresource:status //+kubebuilder:resource:categories="postgres";"servicecatalog"  // PgCluster is the Schema for the mskclusters API type PgCluster struct {  metav1.TypeMeta `json:",inline"`  metav1.ObjectMeta `json:"metadata,omitempty"`  Spec PgClusterSpec `json:"spec,omitempty"`  Status PgClusterStatus `json:"status,omitempty"` } 

As soon as we generate the manifests and install them in the cluster (make install) we'll be able to query all the objects that have the same group with kubectl get:

$ kubectl get postgres NAME PG CLUSTER ADMIN SYNC STATUS pguser.msk.pet2cattle.com/operator-user sample true in-sync pguser.msk.pet2cattle.com/developer sample in-sync NAME CLUSTER NAME SYNC STATUS pgcluster.msk.pet2cattle.com/sample sample in-sync 

Posted on 01/08/2023