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 diff --git a/docs/insights-archive-sample/config/workload_info.json b/docs/insights-archive-sample/config/workload_info.json index 954228f10..c30608f51 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":"2W0Xq9hxQzho" }, "sha256:7c6d0a0fed7ddb95550623aa23c434446fb99abef18e6d57b8b12add606efde8": { "layerIDs": [ diff --git a/pkg/gatherers/workloads/gather_workloads_info.go b/pkg/gatherers/workloads/gather_workloads_info.go index f5e9201c6..5c7280813 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,10 +509,16 @@ func calculateWorkloadInfo(h hash.Hash, image *imagev1.Image) workloadImage { for _, layer := range image.DockerImageLayers { layers = append(layers, layer.Name) } + info := workloadImage{ LayerIDs: layers, } + // 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 { return info } @@ -587,3 +593,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 +} 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) + }) + } +} 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.