Skip to content

Commit

Permalink
OCPBUGS-17428: workload info gatherer, add external image repo (opens…
Browse files Browse the repository at this point in the history
…hift#811)

* Add new property to save image repository url

* Add unit tests for repository image handling

* Add new function to get only external repos urls

* Update last change in documentation

* Add sample data

* Hash image information to match gatherer functionality

* Fix sample with hashed data (from output)
  • Loading branch information
ncaak authored and JoaoFula committed Jan 23, 2024
1 parent f42c64a commit 27e83ff
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/gathered-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,6 @@ None
None

### Changes
None
- Image repository is now collected if it comes from outside the Red Hat domain


3 changes: 2 additions & 1 deletion docs/insights-archive-sample/config/workload_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"sha256:1bd85e07834910cad1fda7967cfd86ada69377ba0baf875683e7739d8de6d1b0"
],
"firstCommand": "icTsn2s_EIax",
"firstArg": "2v1NneeWoS_9"
"firstArg": "2v1NneeWoS_9",
"repository":"2W0Xq9hxQzho"
},
"sha256:7c6d0a0fed7ddb95550623aa23c434446fb99abef18e6d57b8b12add606efde8": {
"layerIDs": [
Expand Down
16 changes: 15 additions & 1 deletion pkg/gatherers/workloads/gather_workloads_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions pkg/gatherers/workloads/gather_workloads_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}
}
3 changes: 3 additions & 0 deletions pkg/gatherers/workloads/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 27e83ff

Please sign in to comment.