@@ -153,16 +153,28 @@ func (g *dynamicGetter) Get(uns *unstructured.Unstructured) (*Info, error) {
153153 }
154154
155155 var compositionDefinition * unstructured.Unstructured
156-
157156 if cdInfo != nil {
158157 g .logger .Debug ("Getting composition definition" , "compositionDefinitionName" , cdInfo .Name , "compositionDefinitionNamespace" , cdInfo .Namespace , "compositionDefinitionGVR" , cdInfo .GVR .String ())
159158 compositionDefinition , err = g .dynamicClient .Resource (cdInfo .GVR ).
160159 Namespace (cdInfo .Namespace ).
161160 Get (context .Background (), cdInfo .Name , metav1.GetOptions {})
162161 if err != nil {
163- return nil , fmt .Errorf ("error getting composition definition '%s' in namespace '%s' with gvr: %s: %w" , cdInfo .Name , cdInfo .Namespace , cdInfo .GVR .String (), err )
162+ g .logger .Warn ("Error getting composition definition" , "error" , err .Error (), "compositionDefinitionName" , cdInfo .Name , "compositionDefinitionNamespace" , cdInfo .Namespace , "gvr" , cdInfo .GVR .String ())
163+ compositionDefinition = nil
164+ }
165+ if compositionDefinition != nil {
166+ version , kind , err := getChartVersionKind (compositionDefinition )
167+ if err != nil {
168+ return nil , fmt .Errorf ("error getting chart version and kind from composition definition '%s' in namespace '%s': %w" , cdInfo .Name , cdInfo .Namespace , err )
169+ }
170+ if version != uns .GetLabels ()[compositionMeta .CompositionVersionLabel ] || kind != uns .GetKind () {
171+ g .logger .Warn ("Labels do not match composition definition" , "compositionDefinitionName" , cdInfo .Name , "compositionDefinitionNamespace" , cdInfo .Namespace , "gvr" , gvr .String (), "expectedVersion" , uns .GetLabels ()[compositionMeta .CompositionVersionLabel ], "foundVersion" , version , "expectedKind" , uns .GetKind (), "foundKind" , kind )
172+ compositionDefinition = nil
173+ }
164174 }
165- } else {
175+ }
176+
177+ if compositionDefinition == nil {
166178 // Search for the composition definition in the namespace of the unstructured object
167179 g .logger .Debug ("Searching for composition definition" )
168180 compositionDefinition , err = g .searchCompositionDefinition (gvr , uns )
@@ -298,31 +310,11 @@ func (g *dynamicGetter) searchCompositionDefinition(gvr schema.GroupVersionResou
298310 if tot > 1 {
299311 found := false
300312 for _ , el := range all .Items {
301- apiversion , ok , err := unstructured . NestedString ( el . UnstructuredContent (), "status" , "apiVersion" )
313+ version , kind , err := getChartVersionKind ( & el )
302314 if err != nil {
303- g .logger .Debug ("Failed to resolve 'status.apiVersion' " , "error" , err .Error (), "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace ())
315+ g .logger .Debug ("Failed to get chart version and kind " , "error" , err .Error (), "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace (), "gvr" , gvr . String ())
304316 continue
305317 }
306- if ! ok {
307- g .logger .Debug ("Failed to resolve 'status.apiVersion'" , "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace ())
308- continue
309- }
310- versionSplit := strings .Split (apiversion , "/" )
311- if len (versionSplit ) != 2 {
312- g .logger .Debug ("Invalid format for 'status.apiVersion'" , "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace ())
313- continue
314- }
315- kind , ok , err := unstructured .NestedString (el .UnstructuredContent (), "status" , "kind" )
316- if err != nil {
317- g .logger .Debug ("Failed to resolve 'status.kind'" , "error" , err .Error (), "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace ())
318- continue
319- }
320- if ! ok {
321- g .logger .Debug ("Failed to resolve 'status.kind'" , "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace ())
322- continue
323- }
324-
325- version := versionSplit [1 ]
326318 if version == mg .GetLabels ()[compositionMeta .CompositionVersionLabel ] && kind == mg .GetKind () {
327319 compositionDefinition = & el
328320 g .logger .Debug ("Found matching composition definition" , "compositionDefinitionName" , el .GetName (), "compositionDefinitionNamespace" , el .GetNamespace (), "gvr" , gvr .String ())
@@ -340,3 +332,27 @@ func (g *dynamicGetter) searchCompositionDefinition(gvr schema.GroupVersionResou
340332
341333 return compositionDefinition , nil
342334}
335+
336+ func getChartVersionKind (el * unstructured.Unstructured ) (string , string , error ) {
337+ apiversion , ok , err := unstructured .NestedString (el .UnstructuredContent (), "status" , "apiVersion" )
338+ if err != nil {
339+ return "" , "" , fmt .Errorf ("failed to resolve 'status.apiVersion': %w" , err )
340+ }
341+ if ! ok {
342+ return "" , "" , fmt .Errorf ("missing 'status.apiVersion'" )
343+ }
344+ versionSplit := strings .Split (apiversion , "/" )
345+ if len (versionSplit ) != 2 {
346+ return "" , "" , fmt .Errorf ("invalid format for 'status.apiVersion'" )
347+ }
348+ kind , ok , err := unstructured .NestedString (el .UnstructuredContent (), "status" , "kind" )
349+ if err != nil {
350+ return "" , "" , fmt .Errorf ("failed to resolve 'status.kind': %w" , err )
351+ }
352+ if ! ok {
353+ return "" , "" , fmt .Errorf ("missing 'status.kind'" )
354+ }
355+
356+ version := versionSplit [1 ]
357+ return version , kind , nil
358+ }
0 commit comments