diff --git a/go.mod b/go.mod index 15e35d5..c1ba12a 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/gobuffalo/flect v1.0.3 github.com/golang/mock v1.6.0 github.com/krateoplatformops/plumbing v0.7.2 - github.com/krateoplatformops/unstructured-runtime v0.3.0 + github.com/krateoplatformops/unstructured-runtime v0.3.1 github.com/pkg/errors v0.9.1 github.com/spf13/pflag v1.0.10 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index aa60936..b902a35 100644 --- a/go.sum +++ b/go.sum @@ -177,8 +177,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/krateoplatformops/plumbing v0.7.2 h1:4UuWy9747p9ligMtNEiOOQGsuK6d9lczg7R1no8ERsE= github.com/krateoplatformops/plumbing v0.7.2/go.mod h1:mQ/sm0viyKgfR2ARzHuwCpY0rcyMKqCv8a8SOu52yYQ= -github.com/krateoplatformops/unstructured-runtime v0.3.0 h1:0lQDUDTViPEBx988b1JJYlVJNwbycTngsyqdaUOzUTQ= -github.com/krateoplatformops/unstructured-runtime v0.3.0/go.mod h1:19uT87wZzRSjrfk3731Xhdt8ww7vnsXhljy4jk0cuWA= +github.com/krateoplatformops/unstructured-runtime v0.3.1 h1:tQMH19YEJ7+La5283a4FOQlCeBBhS9cqwYzBPW59srs= +github.com/krateoplatformops/unstructured-runtime v0.3.1/go.mod h1:19uT87wZzRSjrfk3731Xhdt8ww7vnsXhljy4jk0cuWA= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= diff --git a/internal/tools/helmchart/archive/getter.go b/internal/tools/helmchart/archive/getter.go index 5c8c35d..ef8668a 100644 --- a/internal/tools/helmchart/archive/getter.go +++ b/internal/tools/helmchart/archive/getter.go @@ -153,16 +153,28 @@ func (g *dynamicGetter) Get(uns *unstructured.Unstructured) (*Info, error) { } var compositionDefinition *unstructured.Unstructured - if cdInfo != nil { g.logger.Debug("Getting composition definition", "compositionDefinitionName", cdInfo.Name, "compositionDefinitionNamespace", cdInfo.Namespace, "compositionDefinitionGVR", cdInfo.GVR.String()) compositionDefinition, err = g.dynamicClient.Resource(cdInfo.GVR). Namespace(cdInfo.Namespace). Get(context.Background(), cdInfo.Name, metav1.GetOptions{}) if err != nil { - return nil, fmt.Errorf("error getting composition definition '%s' in namespace '%s' with gvr: %s: %w", cdInfo.Name, cdInfo.Namespace, cdInfo.GVR.String(), err) + g.logger.Warn("Error getting composition definition", "error", err.Error(), "compositionDefinitionName", cdInfo.Name, "compositionDefinitionNamespace", cdInfo.Namespace, "gvr", cdInfo.GVR.String()) + compositionDefinition = nil + } + if compositionDefinition != nil { + version, kind, err := getChartVersionKind(compositionDefinition) + if err != nil { + return nil, fmt.Errorf("error getting chart version and kind from composition definition '%s' in namespace '%s': %w", cdInfo.Name, cdInfo.Namespace, err) + } + if version != uns.GetLabels()[compositionMeta.CompositionVersionLabel] || kind != uns.GetKind() { + 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) + compositionDefinition = nil + } } - } else { + } + + if compositionDefinition == nil { // Search for the composition definition in the namespace of the unstructured object g.logger.Debug("Searching for composition definition") compositionDefinition, err = g.searchCompositionDefinition(gvr, uns) @@ -298,31 +310,11 @@ func (g *dynamicGetter) searchCompositionDefinition(gvr schema.GroupVersionResou if tot > 1 { found := false for _, el := range all.Items { - apiversion, ok, err := unstructured.NestedString(el.UnstructuredContent(), "status", "apiVersion") + version, kind, err := getChartVersionKind(&el) if err != nil { - g.logger.Debug("Failed to resolve 'status.apiVersion'", "error", err.Error(), "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace()) + g.logger.Debug("Failed to get chart version and kind", "error", err.Error(), "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace(), "gvr", gvr.String()) continue } - if !ok { - g.logger.Debug("Failed to resolve 'status.apiVersion'", "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace()) - continue - } - versionSplit := strings.Split(apiversion, "/") - if len(versionSplit) != 2 { - g.logger.Debug("Invalid format for 'status.apiVersion'", "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace()) - continue - } - kind, ok, err := unstructured.NestedString(el.UnstructuredContent(), "status", "kind") - if err != nil { - g.logger.Debug("Failed to resolve 'status.kind'", "error", err.Error(), "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace()) - continue - } - if !ok { - g.logger.Debug("Failed to resolve 'status.kind'", "compositionDefinitionName", el.GetName(), "compositionDefinitionNamespace", el.GetNamespace()) - continue - } - - version := versionSplit[1] if version == mg.GetLabels()[compositionMeta.CompositionVersionLabel] && kind == mg.GetKind() { compositionDefinition = &el 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 return compositionDefinition, nil } + +func getChartVersionKind(el *unstructured.Unstructured) (string, string, error) { + apiversion, ok, err := unstructured.NestedString(el.UnstructuredContent(), "status", "apiVersion") + if err != nil { + return "", "", fmt.Errorf("failed to resolve 'status.apiVersion': %w", err) + } + if !ok { + return "", "", fmt.Errorf("missing 'status.apiVersion'") + } + versionSplit := strings.Split(apiversion, "/") + if len(versionSplit) != 2 { + return "", "", fmt.Errorf("invalid format for 'status.apiVersion'") + } + kind, ok, err := unstructured.NestedString(el.UnstructuredContent(), "status", "kind") + if err != nil { + return "", "", fmt.Errorf("failed to resolve 'status.kind': %w", err) + } + if !ok { + return "", "", fmt.Errorf("missing 'status.kind'") + } + + version := versionSplit[1] + return version, kind, nil +}