Skip to content

Commit c1f3f39

Browse files
committed
fix: use upstream representation of GroupVersionKind
1 parent d86898b commit c1f3f39

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

pkg/customresourcestate/config.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type Resource struct {
5959
MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"`
6060

6161
// GroupVersionKind of the custom resource to be monitored.
62-
GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"`
62+
GroupVersionKind schema.GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"`
6363

6464
// ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize.
6565
ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"`
@@ -89,17 +89,6 @@ func (r Resource) GetResourceName() string {
8989
return strings.ToLower(flect.Pluralize(r.GroupVersionKind.Kind))
9090
}
9191

92-
// GroupVersionKind is the Kubernetes group, version, and kind of a resource.
93-
type GroupVersionKind struct {
94-
Group string `yaml:"group" json:"group"`
95-
Version string `yaml:"version" json:"version"`
96-
Kind string `yaml:"kind" json:"kind"`
97-
}
98-
99-
func (gvk GroupVersionKind) String() string {
100-
return fmt.Sprintf("%s_%s_%s", gvk.Group, gvk.Version, gvk.Kind)
101-
}
102-
10392
// Labels is common configuration of labels to add to metrics.
10493
type Labels struct {
10594
// CommonLabels are added to all metrics.
@@ -194,7 +183,7 @@ func FromConfig(decoder ConfigDecoder, discovererInstance *discovery.CRDiscovere
194183
}
195184
for _, resolved /* GVKP */ := range resolvedSet {
196185
// Set their G** attributes to various resolutions of the GVK.
197-
resource.GroupVersionKind = GroupVersionKind(resolved.GroupVersionKind)
186+
resource.GroupVersionKind = schema.GroupVersionKind(resolved.GroupVersionKind)
198187
// Set the plural name of the resource based on the extracted value from the same field in the CRD schema.
199188
resource.ResourcePlural = resolved.Plural
200189
resolvedGVKPs = append(resolvedGVKPs, resource)

pkg/customresourcestate/custom_resource_metrics_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
3939
// https://github.com/kubernetes/kube-state-metrics/issues/1886
4040
name: "cr metric with dynamic metric type",
4141
r: Resource{
42-
GroupVersionKind: GroupVersionKind{
42+
GroupVersionKind: schema.GroupVersionKind{
4343
Group: "apps",
4444
Version: "v1",
4545
Kind: "Deployment",
@@ -83,7 +83,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
8383
Families: []compiledFamily{
8484
{
8585
Name: "kube_customresource_test_metrics",
86-
Help: "metrics for testing for apps_v1_Deployment",
86+
Help: "metrics for testing for apps/v1, Kind=Deployment",
8787
Each: &compiledInfo{},
8888
Labels: map[string]string{
8989
"customresource_group": "apps",
@@ -101,7 +101,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
101101
{
102102
name: "cr metric with custom prefix",
103103
r: Resource{
104-
GroupVersionKind: GroupVersionKind{
104+
GroupVersionKind: schema.GroupVersionKind{
105105
Group: "apps",
106106
Version: "v1",
107107
Kind: "Deployment",
@@ -146,7 +146,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
146146
Families: []compiledFamily{
147147
{
148148
Name: "apps_deployment_test_metrics",
149-
Help: "metrics for testing for apps_v1_Deployment",
149+
Help: "metrics for testing for apps/v1, Kind=Deployment",
150150
Each: &compiledInfo{},
151151
Labels: map[string]string{
152152
"customresource_group": "apps",
@@ -164,7 +164,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
164164
{
165165
name: "cr metric with custom prefix - expect error",
166166
r: Resource{
167-
GroupVersionKind: GroupVersionKind{
167+
GroupVersionKind: schema.GroupVersionKind{
168168
Group: "apps",
169169
Version: "v1",
170170
Kind: "Deployment",
@@ -208,7 +208,7 @@ func TestNewCustomResourceMetrics(t *testing.T) {
208208
Families: []compiledFamily{
209209
{
210210
Name: "apps_deployment_test_metrics",
211-
Help: "metrics for testing for apps_v1_Deployment",
211+
Help: "metrics for testing for apps/v1, Kind=Deployment",
212212
Each: &compiledInfo{},
213213
Labels: map[string]string{
214214
"customresource_group": "apps",

pkg/customresourcestate/registry_factory_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/stretchr/testify/assert"
2626
"k8s.io/utils/ptr"
2727

28+
"k8s.io/apimachinery/pkg/runtime/schema"
2829
"k8s.io/kube-state-metrics/v2/pkg/metric"
2930
)
3031

@@ -493,8 +494,8 @@ func r(metricNamePrefix *string) Resource {
493494
return Resource{MetricNamePrefix: metricNamePrefix, GroupVersionKind: gkv("apps", "v1", "Deployment")}
494495
}
495496

496-
func gkv(group, version, kind string) GroupVersionKind {
497-
return GroupVersionKind{
497+
func gkv(group, version, kind string) schema.GroupVersionKind {
498+
return schema.GroupVersionKind{
498499
Group: group,
499500
Version: version,
500501
Kind: kind,

0 commit comments

Comments
 (0)