Skip to content

Commit df72b9b

Browse files
chore: update runtime lib (#164)
* chore: update runtime lib * fix: minor improvement in compositiodefinition search
1 parent 49bb0be commit df72b9b

3 files changed

Lines changed: 44 additions & 28 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/gobuffalo/flect v1.0.3
1111
github.com/golang/mock v1.6.0
1212
github.com/krateoplatformops/plumbing v0.7.2
13-
github.com/krateoplatformops/unstructured-runtime v0.3.0
13+
github.com/krateoplatformops/unstructured-runtime v0.3.1
1414
github.com/pkg/errors v0.9.1
1515
github.com/spf13/pflag v1.0.10
1616
github.com/stretchr/testify v1.11.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
177177
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
178178
github.com/krateoplatformops/plumbing v0.7.2 h1:4UuWy9747p9ligMtNEiOOQGsuK6d9lczg7R1no8ERsE=
179179
github.com/krateoplatformops/plumbing v0.7.2/go.mod h1:mQ/sm0viyKgfR2ARzHuwCpY0rcyMKqCv8a8SOu52yYQ=
180-
github.com/krateoplatformops/unstructured-runtime v0.3.0 h1:0lQDUDTViPEBx988b1JJYlVJNwbycTngsyqdaUOzUTQ=
181-
github.com/krateoplatformops/unstructured-runtime v0.3.0/go.mod h1:19uT87wZzRSjrfk3731Xhdt8ww7vnsXhljy4jk0cuWA=
180+
github.com/krateoplatformops/unstructured-runtime v0.3.1 h1:tQMH19YEJ7+La5283a4FOQlCeBBhS9cqwYzBPW59srs=
181+
github.com/krateoplatformops/unstructured-runtime v0.3.1/go.mod h1:19uT87wZzRSjrfk3731Xhdt8ww7vnsXhljy4jk0cuWA=
182182
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
183183
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
184184
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=

internal/tools/helmchart/archive/getter.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)