Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
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"`
Expand Down Expand Up @@ -89,17 +89,6 @@
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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the upstream schema.GVK doesn't include tags here, I wonder if this will cause any issues?
CC: @rexagod

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.
Expand Down Expand Up @@ -188,13 +177,13 @@
// 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))

Check failure on line 180 in pkg/customresourcestate/config.go

View workflow job for this annotation

GitHub Actions / ci-go-lint

unnecessary conversion (unconvert)
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 = GroupVersionKind(resolved.GroupVersionKind)
resource.GroupVersionKind = schema.GroupVersionKind(resolved.GroupVersionKind)

Check failure on line 186 in pkg/customresourcestate/config.go

View workflow job for this annotation

GitHub Actions / ci-go-lint

unnecessary conversion (unconvert)
// 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)
Expand Down
12 changes: 6 additions & 6 deletions pkg/customresourcestate/custom_resource_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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, Kind=Deployment",
Each: &compiledInfo{},
Labels: map[string]string{
"customresource_group": "apps",
Expand All @@ -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",
Expand Down Expand Up @@ -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, Kind=Deployment",
Each: &compiledInfo{},
Labels: map[string]string{
"customresource_group": "apps",
Expand All @@ -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",
Expand Down Expand Up @@ -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, Kind=Deployment",
Each: &compiledInfo{},
Labels: map[string]string{
"customresource_group": "apps",
Expand Down
5 changes: 4 additions & 1 deletion pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ func compileFamily(f Generator, resource Resource) (*compiledFamily, error) {
if errorLogV == 0 {
errorLogV = resource.ErrorLogV
}

help := fmt.Sprintf("%s for %s", f.Help, resource.GroupVersionKind)

return &compiledFamily{
Name: fullName(resource, f),
ErrorLogV: errorLogV,
Help: f.Help,
Help: help,
Each: metric,
Labels: labels.CommonLabels,
LabelFromPath: labelsFromPath,
Expand Down
5 changes: 3 additions & 2 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"

"k8s.io/apimachinery/pkg/runtime/schema"

Check failure on line 28 in pkg/customresourcestate/registry_factory_test.go

View workflow job for this annotation

GitHub Actions / ci-go-lint

File is not properly formatted (goimports)
"k8s.io/kube-state-metrics/v2/pkg/metric"
)

Expand Down Expand Up @@ -493,8 +494,8 @@
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,
Expand Down
Loading