From 14c43102616ecfbe7ab13a848b9681cb1bbbef4e Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 31 Jul 2023 18:32:33 +0200 Subject: [PATCH 1/7] Add new property to save image repository url --- pkg/gatherers/workloads/types.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/gatherers/workloads/types.go b/pkg/gatherers/workloads/types.go index e7bb256cf..f1010bbb4 100644 --- a/pkg/gatherers/workloads/types.go +++ b/pkg/gatherers/workloads/types.go @@ -36,6 +36,9 @@ type workloadImage struct { // FirstArg is a hash of the first value in the command array, if any // was set. Normalized to be consistent with pods FirstArg string `json:"firstArg,omitempty"` + // Repository is a URL of an external image + // It is gathered when the image is not from Red Hat domain + Repository string `json:"repository,omitempty"` } // Empty returns true if the image has no contents and can be ignored. From 5c08ac5eadf0eaf5f391ab05eb2c7186a5e9e217 Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 31 Jul 2023 18:32:50 +0200 Subject: [PATCH 2/7] Add unit tests for repository image handling --- .../workloads/gather_workloads_info_test.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/gatherers/workloads/gather_workloads_info_test.go b/pkg/gatherers/workloads/gather_workloads_info_test.go index 7e91999d8..055c7d201 100644 --- a/pkg/gatherers/workloads/gather_workloads_info_test.go +++ b/pkg/gatherers/workloads/gather_workloads_info_test.go @@ -13,6 +13,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "github.com/openshift/insights-operator/pkg/record" + "github.com/stretchr/testify/assert" ) // nolint: funlen, gocyclo, gosec @@ -149,3 +150,32 @@ func Test_gatherWorkloadInfo(t *testing.T) { ) } } + +func Test_getExternalImageRepo(t *testing.T) { + testCases := []struct { + name string + url string + expected string + }{ + { + name: "Image repository under the Red Hat domain will be ignored", + url: "registry.redhat.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:abc", + expected: "", + }, + { + name: "Image repository outside the Red Hat domain is returned", + url: "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:abc", + expected: "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:abc", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + // When + test := getExternalImageRepo(testCase.url) + + // Assert + assert.Equal(t, testCase.expected, test) + }) + } +} From 4af787fc2d85d8f53cb9807a7a7c2e0efc928330 Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 31 Jul 2023 18:33:06 +0200 Subject: [PATCH 3/7] Add new function to get only external repos urls --- pkg/gatherers/workloads/gather_workloads_info.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/gatherers/workloads/gather_workloads_info.go b/pkg/gatherers/workloads/gather_workloads_info.go index f5e9201c6..f4eb41866 100644 --- a/pkg/gatherers/workloads/gather_workloads_info.go +++ b/pkg/gatherers/workloads/gather_workloads_info.go @@ -60,7 +60,7 @@ const ( // None // // ### Changes -// None +// - Image repository is now collected if it comes from outside the Red Hat domain func (g *Gatherer) GatherWorkloadInfo(ctx context.Context) ([]record.Record, []error) { gatherKubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig) if err != nil { @@ -509,8 +509,11 @@ func calculateWorkloadInfo(h hash.Hash, image *imagev1.Image) workloadImage { for _, layer := range image.DockerImageLayers { layers = append(layers, layer.Name) } + info := workloadImage{ LayerIDs: layers, + //RepoURL: "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:5710554c08735126986b7c553cdb9a31bf97071c7adceda20f7aa116f35e867f", + Repository: getExternalImageRepo(image.DockerImageReference), } if err := imageutil.ImageWithMetadata(image); err != nil { @@ -587,3 +590,11 @@ func workloadImageAdd(imageID string, image workloadImage) { defer workloadSizeLock.Unlock() workloadImageLRU.Add(imageID, image) } + +// getExternalImageRepo returns image URLs if they are not from Red Hat domain +func getExternalImageRepo(s string) (repo string) { + if !strings.Contains(s, "redhat") { + repo = s + } + return +} From 63116c378a6d7c4eab9696b5ede55a774b21d051 Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 31 Jul 2023 18:36:28 +0200 Subject: [PATCH 4/7] Update last change in documentation --- docs/gathered-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gathered-data.md b/docs/gathered-data.md index c8db19ded..73f04d6e7 100644 --- a/docs/gathered-data.md +++ b/docs/gathered-data.md @@ -1957,6 +1957,6 @@ None None ### Changes -None +- Image repository is now collected if it comes from outside the Red Hat domain From dea736008f0bba53d6584689f1c95828f76aef9c Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 31 Jul 2023 18:55:14 +0200 Subject: [PATCH 5/7] Add sample data --- docs/insights-archive-sample/config/workload_info.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/insights-archive-sample/config/workload_info.json b/docs/insights-archive-sample/config/workload_info.json index 954228f10..e80f1bff9 100644 --- a/docs/insights-archive-sample/config/workload_info.json +++ b/docs/insights-archive-sample/config/workload_info.json @@ -22,7 +22,8 @@ "sha256:1bd85e07834910cad1fda7967cfd86ada69377ba0baf875683e7739d8de6d1b0" ], "firstCommand": "icTsn2s_EIax", - "firstArg": "2v1NneeWoS_9" + "firstArg": "2v1NneeWoS_9", + "repository":"quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:a16c19e8f7372bec6e2b62f1eae6d4796d6aec8435679c7821fdc267fa667140" }, "sha256:7c6d0a0fed7ddb95550623aa23c434446fb99abef18e6d57b8b12add606efde8": { "layerIDs": [ From 8f62b7f1b5d52f54034cb3a63d88bac662fb879c Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Fri, 4 Aug 2023 12:38:16 +0200 Subject: [PATCH 6/7] Hash image information to match gatherer functionality --- pkg/gatherers/workloads/gather_workloads_info.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/gatherers/workloads/gather_workloads_info.go b/pkg/gatherers/workloads/gather_workloads_info.go index f4eb41866..5c7280813 100644 --- a/pkg/gatherers/workloads/gather_workloads_info.go +++ b/pkg/gatherers/workloads/gather_workloads_info.go @@ -512,8 +512,11 @@ func calculateWorkloadInfo(h hash.Hash, image *imagev1.Image) workloadImage { info := workloadImage{ LayerIDs: layers, - //RepoURL: "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:5710554c08735126986b7c553cdb9a31bf97071c7adceda20f7aa116f35e867f", - Repository: getExternalImageRepo(image.DockerImageReference), + } + + // we only need repo URLs from outside RH + if repo := getExternalImageRepo(image.DockerImageReference); repo != "" { + info.Repository = workloadHashString(h, repo) } if err := imageutil.ImageWithMetadata(image); err != nil { From eb2778608804397a4c87507f83b4104275849058 Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Fri, 4 Aug 2023 12:39:01 +0200 Subject: [PATCH 7/7] Fix sample with hashed data (from output) --- docs/insights-archive-sample/config/workload_info.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/insights-archive-sample/config/workload_info.json b/docs/insights-archive-sample/config/workload_info.json index e80f1bff9..c30608f51 100644 --- a/docs/insights-archive-sample/config/workload_info.json +++ b/docs/insights-archive-sample/config/workload_info.json @@ -23,7 +23,7 @@ ], "firstCommand": "icTsn2s_EIax", "firstArg": "2v1NneeWoS_9", - "repository":"quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:a16c19e8f7372bec6e2b62f1eae6d4796d6aec8435679c7821fdc267fa667140" + "repository":"2W0Xq9hxQzho" }, "sha256:7c6d0a0fed7ddb95550623aa23c434446fb99abef18e6d57b8b12add606efde8": { "layerIDs": [