From f1a12d64a5bc1099c8cf79086cacfe8a11e71b02 Mon Sep 17 00:00:00 2001 From: dbw7 Date: Wed, 15 May 2024 11:10:58 -0400 Subject: [PATCH 1/3] Update helm.go --- RELEASE_NOTES.md | 1 + pkg/helm/helm.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 1240752d..d3808c57 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -13,6 +13,7 @@ ## Bug Fixes * [#429](https://github.com/suse-edge/edge-image-builder/issues/429) - Automatically set execute bit on scripts +* [#442](https://github.com/suse-edge/edge-image-builder/issues/442) - Only get images from specific Kubernetes objects --- diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 202c0742..0e77eb11 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "github.com/suse-edge/edge-image-builder/pkg/fileio" @@ -270,6 +271,15 @@ func templateCommand(chart, repository, version, valuesFilePath, kubeVersion, ta func parseChartContents(chartContents string) ([]map[string]any, error) { var resources []map[string]any + var k8sKinds = []string{ + "Pod", + "Deployment", + "StatefulSet", + "DaemonSet", + "ReplicaSet", + "Job", + "CronJob", + } for _, resource := range strings.Split(chartContents, "---\n") { if resource == "" { @@ -292,7 +302,10 @@ func parseChartContents(chartContents string) ([]map[string]any, error) { return nil, fmt.Errorf("decoding resource from source '%s': %w", source, err) } - resources = append(resources, r) + kind, _ := r["kind"].(string) + if slices.Contains(k8sKinds, kind) { + resources = append(resources, r) + } } return resources, nil From 3358e20cf19439018ceb2792a18d70ebe5f1d4ca Mon Sep 17 00:00:00 2001 From: dbw7 Date: Wed, 15 May 2024 11:40:58 -0400 Subject: [PATCH 2/3] updates based on feedback --- pkg/helm/helm.go | 15 +-------------- pkg/registry/manifests.go | 16 ++++++++++++++++ pkg/registry/testdata/sample-crd.yaml | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 0e77eb11..202c0742 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "path/filepath" - "slices" "strings" "github.com/suse-edge/edge-image-builder/pkg/fileio" @@ -271,15 +270,6 @@ func templateCommand(chart, repository, version, valuesFilePath, kubeVersion, ta func parseChartContents(chartContents string) ([]map[string]any, error) { var resources []map[string]any - var k8sKinds = []string{ - "Pod", - "Deployment", - "StatefulSet", - "DaemonSet", - "ReplicaSet", - "Job", - "CronJob", - } for _, resource := range strings.Split(chartContents, "---\n") { if resource == "" { @@ -302,10 +292,7 @@ func parseChartContents(chartContents string) ([]map[string]any, error) { return nil, fmt.Errorf("decoding resource from source '%s': %w", source, err) } - kind, _ := r["kind"].(string) - if slices.Contains(k8sKinds, kind) { - resources = append(resources, r) - } + resources = append(resources, r) } return resources, nil diff --git a/pkg/registry/manifests.go b/pkg/registry/manifests.go index a0a833fb..63826a8b 100644 --- a/pkg/registry/manifests.go +++ b/pkg/registry/manifests.go @@ -7,6 +7,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "github.com/suse-edge/edge-image-builder/pkg/http" @@ -85,6 +86,21 @@ func readManifest(manifestPath string) ([]map[string]any, error) { } func storeManifestImages(resource map[string]any, images map[string]bool) { + var k8sKinds = []string{ + "Pod", + "Deployment", + "StatefulSet", + "DaemonSet", + "ReplicaSet", + "Job", + "CronJob", + } + + kind, _ := resource["kind"].(string) + if !slices.Contains(k8sKinds, kind) { + return + } + var findImages func(data any) findImages = func(data any) { diff --git a/pkg/registry/testdata/sample-crd.yaml b/pkg/registry/testdata/sample-crd.yaml index 710b7425..8864e9a6 100644 --- a/pkg/registry/testdata/sample-crd.yaml +++ b/pkg/registry/testdata/sample-crd.yaml @@ -1,5 +1,5 @@ apiVersion: "custom.example.com/v1" -kind: ComplexApplication +kind: Deployment metadata: name: my-complex-app labels: From c4e63e2fd0f841a43cb24cf06273ef14055663bb Mon Sep 17 00:00:00 2001 From: dbw7 Date: Mon, 20 May 2024 13:09:30 -0400 Subject: [PATCH 3/3] add unit test --- pkg/registry/manifests_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/registry/manifests_test.go b/pkg/registry/manifests_test.go index b7629d26..daefc062 100644 --- a/pkg/registry/manifests_test.go +++ b/pkg/registry/manifests_test.go @@ -92,6 +92,29 @@ func TestStoreManifestImages(t *testing.T) { assert.ElementsMatch(t, expectedImages, allImages) } +func TestStoreManifestImages_InvalidKinds(t *testing.T) { + // Setup + var extractedImagesSet = make(map[string]bool) + manifestData := map[string]any{ + "apiVersion": "apps/v1", + "kind": "InvalidKind", + "spec": map[string]any{ + "containers": []any{ + map[string]any{ + "name": "nginx", + "image": "nginx:1.14.2", + }, + }, + }, + } + + // Test + storeManifestImages(manifestData, extractedImagesSet) + + // Verify + assert.Equal(t, map[string]bool{}, extractedImagesSet) +} + func TestStoreManifestImages_EmptyManifest(t *testing.T) { // Setup var extractedImagesSet = make(map[string]bool)