@@ -3,6 +3,7 @@ package orchestrator
33import (
44 "context"
55 "fmt"
6+ "slices"
67 "strconv"
78 "strings"
89
@@ -57,19 +58,21 @@ func (h *ManagedHandler) sendStatusBasedMetricValue(ctx context.Context) (string
5758 for _ , cr := range resources {
5859 // Create a new data point for each resource
5960 dataPoint := clientoptl .NewDataPoint ()
60- dataPoint .AddDimension ("kind" , cr .MangedResource .Kind )
61- dataPoint .AddDimension ("apiversion" , cr .MangedResource .APIVersion )
61+
62+ // Add GVK dimensions from resource
63+ gv , err := schema .ParseGroupVersion (cr .MangedResource .APIVersion )
64+ if err != nil {
65+ return "" , err
66+ }
67+ dataPoint .AddDimension (KIND , cr .MangedResource .Kind )
68+ dataPoint .AddDimension (GROUP , gv .Group )
69+ dataPoint .AddDimension (VERSION , gv .Version )
6270
6371 // Add cluster dimension if available
6472 if h .clusterName != nil {
6573 dataPoint .AddDimension (CLUSTER , * h .clusterName )
6674 }
6775
68- // Add GVK dimensions
69- dataPoint .AddDimension (KIND , h .metric .Spec .Kind )
70- dataPoint .AddDimension (GROUP , h .metric .Spec .Group )
71- dataPoint .AddDimension (VERSION , h .metric .Spec .Version )
72-
7376 // Add status conditions as dimensions
7477 for typ , state := range cr .Status {
7578 dataPoint .AddDimension (strings .ToLower (typ ), strconv .FormatBool (state ))
@@ -150,31 +153,39 @@ func (h *ManagedHandler) getManagedResources(ctx context.Context) ([]Managed, er
150153 return nil , err
151154 }
152155
153- var resourceCRDs []apiextensionsv1.CustomResourceDefinition
156+ resourceCRDs := make ( []apiextensionsv1.CustomResourceDefinition , 0 , len ( crds . Items ))
154157 for _ , crd := range crds .Items {
155- if h .hasCategory ("crossplane" , crd ) && h .hasCategory ("managed" , crd ) { // filter previously acquired crds
156- resourceCRDs = append (resourceCRDs , crd )
158+ // drop non-crossplane crds
159+ if ! h .hasCategory ("crossplane" , crd ) || ! h .hasCategory ("managed" , crd ) {
160+ continue
157161 }
162+ // drop crds that don't match the spec gvk
163+ if ! h .matchesGroupVersionKind (crd ) {
164+ continue
165+ }
166+ resourceCRDs = append (resourceCRDs , crd )
158167 }
159168
160169 var resources []unstructured.Unstructured
161170 for _ , crd := range resourceCRDs {
162-
163- // Use the stored versions of the CRD
164- storedVersions := make (map [string ]bool )
165- for _ , v := range crd .Status .StoredVersions {
166- storedVersions [v ] = true
167- }
168-
171+ versionsToRetrieve := make ([]string , 0 , len (crd .Spec .Versions ))
169172 for _ , crdv := range crd .Spec .Versions {
170- if ! crdv .Served || ! storedVersions [crdv .Name ] {
173+ // only use served versions for retrieval
174+ if ! crdv .Served {
171175 continue
172176 }
173-
177+ // only use the metric target version if provided
178+ if h .metric .Spec .Version != "" && crdv .Name != h .metric .Spec .Version {
179+ continue
180+ }
181+ versionsToRetrieve = append (versionsToRetrieve , crdv .Name )
182+ }
183+ // finally retrieve all matching resources
184+ for _ , version := range versionsToRetrieve {
174185 gvr := schema.GroupVersionResource {
175186 Resource : crd .Spec .Names .Plural ,
176187 Group : crd .Spec .Group ,
177- Version : crdv . Name ,
188+ Version : version ,
178189 }
179190
180191 list , err := h .dCli .Resource (gvr ).List (ctx , metav1.ListOptions {}) // gets resources from all the available crds
@@ -236,3 +247,20 @@ type ClusterResourceStatus struct {
236247 MangedResource Managed
237248 Status map [string ]bool
238249}
250+
251+ func (h * ManagedHandler ) matchesGroupVersionKind (crd apiextensionsv1.CustomResourceDefinition ) bool {
252+ crdVersions := make ([]string , 0 , len (crd .Spec .Versions ))
253+ for _ , version := range crd .Spec .Versions {
254+ crdVersions = append (crdVersions , version .Name )
255+ }
256+ if h .metric .Spec .Version != "" && ! slices .Contains (crdVersions , h .metric .Spec .Version ) {
257+ return false
258+ }
259+ if h .metric .Spec .Group != "" && crd .Spec .Group != h .metric .Spec .Group {
260+ return false
261+ }
262+ if h .metric .Spec .Kind != "" && crd .Spec .Names .Kind != h .metric .Spec .Kind {
263+ return false
264+ }
265+ return true
266+ }
0 commit comments