From 226e25390d26777eebb7b3eff1b6317036e46919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20L=C3=BCders?= Date: Tue, 23 Jan 2024 15:22:24 +0100 Subject: [PATCH] NO-JIRA: fix helmchart gather unit test (#880) * test: fix helmchart gather unit test * docs: better gather helm docs --- docs/gathered-data.md | 12 ++++++++++-- pkg/gatherers/workloads/gather_helm_info.go | 14 +++++++++++--- pkg/gatherers/workloads/types_test.go | 9 ++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/gathered-data.md b/docs/gathered-data.md index 1ba666edc..0a93fa132 100644 --- a/docs/gathered-data.md +++ b/docs/gathered-data.md @@ -773,8 +773,16 @@ None ## HelmInfo -Collects summarized info about the helm usage on a cluster -in a generic fashion +Collects statistics about resources deployed via HelmChart, counting only the resources +with `app.kubernetes.io/managed-by=Helm` and `helm.sh/chart` labels. The data is then summarized +and grouped by hashed namespace. + +Resource types included: +- ReplicaSets +- DaemonSets +- StatefulSets +- Services +- Deployments ### API Reference None diff --git a/pkg/gatherers/workloads/gather_helm_info.go b/pkg/gatherers/workloads/gather_helm_info.go index 13ab6f583..e5de3f5bf 100644 --- a/pkg/gatherers/workloads/gather_helm_info.go +++ b/pkg/gatherers/workloads/gather_helm_info.go @@ -20,8 +20,16 @@ import ( const labelChartNameKey = "helm.sh/chart" -// GatherHelmInfo Collects summarized info about the helm usage on a cluster -// in a generic fashion +// GatherHelmInfo Collects statistics about resources deployed via HelmChart, counting only the resources +// with `app.kubernetes.io/managed-by=Helm` and `helm.sh/chart` labels. The data is then summarized +// and grouped by hashed namespace. +// +// Resource types included: +// - ReplicaSets +// - DaemonSets +// - StatefulSets +// - Services +// - Deployments // // ### API Reference // None @@ -84,7 +92,7 @@ func gatherHelmInfo( // Anonymize the namespace to make it unique identifier hash, err := createHash(item.GetNamespace()) if err != nil { - klog.Errorf("unable to hash the namespace '%s': %v", labels[labelChartNameKey], err) + klog.Errorf("unable to hash the namespace '%s': %v", item.GetNamespace(), err) continue } diff --git a/pkg/gatherers/workloads/types_test.go b/pkg/gatherers/workloads/types_test.go index 9360ff188..c02f2821c 100644 --- a/pkg/gatherers/workloads/types_test.go +++ b/pkg/gatherers/workloads/types_test.go @@ -88,8 +88,6 @@ func TestAddItem(t *testing.T) { for _, testCase := range tests { tt := testCase t.Run(tt.name, func(t *testing.T) { - t.Parallel() - helmList := newHelmChartInfoList() for namespace, resources := range tt.info { for resource, charts := range resources { @@ -99,7 +97,12 @@ func TestAddItem(t *testing.T) { } } - assert.Equal(t, tt.expectedList, helmList.Namespaces, "expected '%v', got '%v'", tt.expectedList, helmList.Namespaces) + // check equality of elements in slices, ignoring order + for namespace, expectedCharts := range tt.expectedList { + actualCharts, ok := helmList.Namespaces[namespace] + assert.True(t, ok, "expected namespace '%s' not found", namespace) + assert.ElementsMatch(t, expectedCharts, actualCharts, "unexpected charts for namespace '%s'", namespace) + } }) } }