From 0284d49ae3a1b571808dd69665cbee3c8580f78c Mon Sep 17 00:00:00 2001 From: Duologic Date: Mon, 24 Mar 2025 14:26:28 +0100 Subject: [PATCH 1/6] fix(customresourcestate): generate unique HELP message for each family The family generator creates a family per GVK, this requires that the configuration has a unique HELP text per custom metric. This is not feasible when using wildcards as the generate will reuse the same HELP text for each GVK it finds. --- pkg/customresourcestate/registry_factory.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 2a2cb067f..c7cd0f69a 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -90,10 +90,13 @@ func compileFamily(f Generator, resource Resource) (*compiledFamily, error) { if errorLogV == 0 { errorLogV = resource.ErrorLogV } + + help := fmt.Sprintf("%s for %s/%s/%s", f.Help, resource.GroupVersionKind.Group, resource.GroupVersionKind.Version, resource.GroupVersionKind.Kind) + return &compiledFamily{ Name: fullName(resource, f), ErrorLogV: errorLogV, - Help: f.Help, + Help: help, Each: metric, Labels: labels.CommonLabels, LabelFromPath: labelsFromPath, From 8053a6bfc3731aa47482d8a6cec676f869ee0309 Mon Sep 17 00:00:00 2001 From: Duologic Date: Mon, 24 Mar 2025 15:37:14 +0100 Subject: [PATCH 2/6] test: update test cases --- pkg/customresourcestate/custom_resource_metrics_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/customresourcestate/custom_resource_metrics_test.go b/pkg/customresourcestate/custom_resource_metrics_test.go index fa3102992..8fbdbca4f 100644 --- a/pkg/customresourcestate/custom_resource_metrics_test.go +++ b/pkg/customresourcestate/custom_resource_metrics_test.go @@ -83,7 +83,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "kube_customresource_test_metrics", - Help: "metrics for testing", + Help: "metrics for testing for apps/v1/Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -146,7 +146,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing", + Help: "metrics for testing for apps/v1/Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -208,7 +208,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing", + Help: "metrics for testing for apps/v1/Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", From 39b950b5fafe6b42c5421296865e2ba548279ffe Mon Sep 17 00:00:00 2001 From: Duologic Date: Tue, 13 May 2025 15:02:31 +0200 Subject: [PATCH 3/6] fix: rely on GroupVersionKind string representation --- pkg/customresourcestate/registry_factory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index c7cd0f69a..36f1ef6bd 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -91,7 +91,7 @@ func compileFamily(f Generator, resource Resource) (*compiledFamily, error) { errorLogV = resource.ErrorLogV } - help := fmt.Sprintf("%s for %s/%s/%s", f.Help, resource.GroupVersionKind.Group, resource.GroupVersionKind.Version, resource.GroupVersionKind.Kind) + help := fmt.Sprintf("%s for %s", f.Help, resource.GroupVersionKind) return &compiledFamily{ Name: fullName(resource, f), From d86898b20ae32833b148342d1d1384393165d8c8 Mon Sep 17 00:00:00 2001 From: Duologic Date: Thu, 28 Aug 2025 00:21:00 +0200 Subject: [PATCH 4/6] test: output format for resource.GroupVersionKind uses underscores --- pkg/customresourcestate/custom_resource_metrics_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/customresourcestate/custom_resource_metrics_test.go b/pkg/customresourcestate/custom_resource_metrics_test.go index 8fbdbca4f..f22ff2a26 100644 --- a/pkg/customresourcestate/custom_resource_metrics_test.go +++ b/pkg/customresourcestate/custom_resource_metrics_test.go @@ -83,7 +83,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "kube_customresource_test_metrics", - Help: "metrics for testing for apps/v1/Deployment", + Help: "metrics for testing for apps_v1_Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -146,7 +146,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing for apps/v1/Deployment", + Help: "metrics for testing for apps_v1_Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -208,7 +208,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing for apps/v1/Deployment", + Help: "metrics for testing for apps_v1_Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", From c1f3f3958c10b307dba823513f6b4fccec7fd73e Mon Sep 17 00:00:00 2001 From: Duologic Date: Mon, 1 Dec 2025 16:17:23 +0100 Subject: [PATCH 5/6] fix: use upstream representation of GroupVersionKind --- pkg/customresourcestate/config.go | 15 ++------------- .../custom_resource_metrics_test.go | 12 ++++++------ pkg/customresourcestate/registry_factory_test.go | 5 +++-- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/pkg/customresourcestate/config.go b/pkg/customresourcestate/config.go index a9e47614a..2bf949823 100644 --- a/pkg/customresourcestate/config.go +++ b/pkg/customresourcestate/config.go @@ -59,7 +59,7 @@ type Resource struct { MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"` // GroupVersionKind of the custom resource to be monitored. - GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"` + GroupVersionKind schema.GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"` // ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize. ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"` @@ -89,17 +89,6 @@ func (r Resource) GetResourceName() string { return strings.ToLower(flect.Pluralize(r.GroupVersionKind.Kind)) } -// GroupVersionKind is the Kubernetes group, version, and kind of a resource. -type GroupVersionKind struct { - Group string `yaml:"group" json:"group"` - Version string `yaml:"version" json:"version"` - Kind string `yaml:"kind" json:"kind"` -} - -func (gvk GroupVersionKind) String() string { - return fmt.Sprintf("%s_%s_%s", gvk.Group, gvk.Version, gvk.Kind) -} - // Labels is common configuration of labels to add to metrics. type Labels struct { // CommonLabels are added to all metrics. @@ -194,7 +183,7 @@ func FromConfig(decoder ConfigDecoder, discovererInstance *discovery.CRDiscovere } for _, resolved /* GVKP */ := range resolvedSet { // Set their G** attributes to various resolutions of the GVK. - resource.GroupVersionKind = GroupVersionKind(resolved.GroupVersionKind) + resource.GroupVersionKind = schema.GroupVersionKind(resolved.GroupVersionKind) // Set the plural name of the resource based on the extracted value from the same field in the CRD schema. resource.ResourcePlural = resolved.Plural resolvedGVKPs = append(resolvedGVKPs, resource) diff --git a/pkg/customresourcestate/custom_resource_metrics_test.go b/pkg/customresourcestate/custom_resource_metrics_test.go index f22ff2a26..83b61c25c 100644 --- a/pkg/customresourcestate/custom_resource_metrics_test.go +++ b/pkg/customresourcestate/custom_resource_metrics_test.go @@ -39,7 +39,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { // https://github.com/kubernetes/kube-state-metrics/issues/1886 name: "cr metric with dynamic metric type", r: Resource{ - GroupVersionKind: GroupVersionKind{ + GroupVersionKind: schema.GroupVersionKind{ Group: "apps", Version: "v1", Kind: "Deployment", @@ -83,7 +83,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "kube_customresource_test_metrics", - Help: "metrics for testing for apps_v1_Deployment", + Help: "metrics for testing for apps/v1, Kind=Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -101,7 +101,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { { name: "cr metric with custom prefix", r: Resource{ - GroupVersionKind: GroupVersionKind{ + GroupVersionKind: schema.GroupVersionKind{ Group: "apps", Version: "v1", Kind: "Deployment", @@ -146,7 +146,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing for apps_v1_Deployment", + Help: "metrics for testing for apps/v1, Kind=Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", @@ -164,7 +164,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { { name: "cr metric with custom prefix - expect error", r: Resource{ - GroupVersionKind: GroupVersionKind{ + GroupVersionKind: schema.GroupVersionKind{ Group: "apps", Version: "v1", Kind: "Deployment", @@ -208,7 +208,7 @@ func TestNewCustomResourceMetrics(t *testing.T) { Families: []compiledFamily{ { Name: "apps_deployment_test_metrics", - Help: "metrics for testing for apps_v1_Deployment", + Help: "metrics for testing for apps/v1, Kind=Deployment", Each: &compiledInfo{}, Labels: map[string]string{ "customresource_group": "apps", diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index 7f5d1a092..3d8f9c4f7 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/assert" "k8s.io/utils/ptr" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/kube-state-metrics/v2/pkg/metric" ) @@ -493,8 +494,8 @@ func r(metricNamePrefix *string) Resource { return Resource{MetricNamePrefix: metricNamePrefix, GroupVersionKind: gkv("apps", "v1", "Deployment")} } -func gkv(group, version, kind string) GroupVersionKind { - return GroupVersionKind{ +func gkv(group, version, kind string) schema.GroupVersionKind { + return schema.GroupVersionKind{ Group: group, Version: version, Kind: kind, From 22bf566a902761840cd216de016123d85c991b35 Mon Sep 17 00:00:00 2001 From: Duologic Date: Tue, 2 Dec 2025 13:06:49 +0100 Subject: [PATCH 6/6] fix: lint issues --- pkg/customresourcestate/config.go | 4 ++-- pkg/customresourcestate/custom_resource_metrics.go | 3 +-- pkg/customresourcestate/registry_factory_test.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/customresourcestate/config.go b/pkg/customresourcestate/config.go index 2bf949823..f640cb8e7 100644 --- a/pkg/customresourcestate/config.go +++ b/pkg/customresourcestate/config.go @@ -177,13 +177,13 @@ func FromConfig(decoder ConfigDecoder, discovererInstance *discovery.CRDiscovere // resolvedGVKPs will have the final list of GVKs, in addition to the resolved G** resources. var resolvedGVKPs []Resource for _, resource := range resources /* G** */ { - resolvedSet /* GVKPs */, err := discovererInstance.ResolveGVKToGVKPs(schema.GroupVersionKind(resource.GroupVersionKind)) + resolvedSet /* GVKPs */, err := discovererInstance.ResolveGVKToGVKPs(resource.GroupVersionKind) if err != nil { klog.ErrorS(err, "failed to resolve GVK", "gvk", resource.GroupVersionKind) } for _, resolved /* GVKP */ := range resolvedSet { // Set their G** attributes to various resolutions of the GVK. - resource.GroupVersionKind = schema.GroupVersionKind(resolved.GroupVersionKind) + resource.GroupVersionKind = resolved.GroupVersionKind // Set the plural name of the resource based on the extracted value from the same field in the CRD schema. resource.ResourcePlural = resolved.Plural resolvedGVKPs = append(resolvedGVKPs, resource) diff --git a/pkg/customresourcestate/custom_resource_metrics.go b/pkg/customresourcestate/custom_resource_metrics.go index 9ca270083..bbf05300e 100644 --- a/pkg/customresourcestate/custom_resource_metrics.go +++ b/pkg/customresourcestate/custom_resource_metrics.go @@ -50,10 +50,9 @@ func NewCustomResourceMetrics(resource Resource) (customresource.RegistryFactory if err != nil { return nil, err } - gvk := schema.GroupVersionKind(resource.GroupVersionKind) return &customResourceMetrics{ MetricNamePrefix: resource.GetMetricNamePrefix(), - GroupVersionKind: gvk, + GroupVersionKind: resource.GroupVersionKind, Families: compiled, ResourceName: resource.GetResourceName(), }, nil diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index 3d8f9c4f7..511d4a21d 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -23,9 +23,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/utils/ptr" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/kube-state-metrics/v2/pkg/metric" )