Skip to content

Commit

Permalink
fix: support empty groups again, e.g. Node
Browse files Browse the repository at this point in the history
PR#1851 introduced accidental breakage for custom metrics on resources
with an empty empty, e.g. Node.

Fix it, and add tests to catch the next breakage.

Reference: kubernetes#1851
Reference: kubernetes#2434
Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>
  • Loading branch information
robbat2 committed Jun 28, 2024
1 parent d4e70df commit 286c043
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (r *CRDiscoverer) ResolveGVKToGVKPs(gvk schema.GroupVersionKind) (resolvedG
g := gvk.Group
v := gvk.Version
k := gvk.Kind
if g == "" || g == "*" {
return nil, fmt.Errorf("group is required in the defined GVK %v", gvk)
if g == "*" {
return nil, fmt.Errorf("non-wildcard group is required in the defined GVK %v", gvk)
}
hasVersion := v != "" && v != "*"
hasKind := k != "" && k != "*"
Expand Down
22 changes: 22 additions & 0 deletions internal/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ func TestGVKMapsResolveGVK(t *testing.T) {
},
},
},
{
desc: "fixed version and kind, empty group",
gvkmaps: &CRDiscoverer{
Map: map[string]map[string][]kindPlural{
"": {
"v1": {
kindPlural{
Kind: "Node",
Plural: "nodes",
},
},
},
},
},
gvk: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Node"},
want: []groupVersionKindPlural{
{
GroupVersionKind: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Node"},
Plural: "nodes",
},
},
},
}
for _, tc := range testcases {
got, err := tc.gvkmaps.ResolveGVKToGVKPs(tc.gvk)
Expand Down
63 changes: 63 additions & 0 deletions pkg/customresourcestate/custom_resource_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,69 @@ func TestNewCustomResourceMetrics(t *testing.T) {
},
},
},
{
// https://github.com/kubernetes/kube-state-metrics/issues/2434
name: "cr metric with dynamic metric type, empty group",
r: Resource{
GroupVersionKind: GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Node",
},
Labels: Labels{
LabelsFromPath: map[string][]string{
"name": {"metadata", "name"},
},
CommonLabels: map[string]string{
"hello": "world",
},
},
Metrics: []Generator{
{
Name: "test_metrics",
Help: "metrics for testing",
Each: Metric{
Type: metric.Info,
Info: &MetricInfo{
MetricMeta: MetricMeta{
Path: []string{
"metadata",
"annotations",
},
},
LabelFromKey: "test",
},
},
},
},
},
wantErr: false,
wantResult: &customResourceMetrics{
MetricNamePrefix: "kube_customresource",
GroupVersionKind: schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Node",
},
ResourceName: "nodes",
Families: []compiledFamily{
{
Name: "kube_customresource_test_metrics",
Help: "metrics for testing",
Each: &compiledInfo{},
Labels: map[string]string{
"customresource_group": "",
"customresource_kind": "Node",
"customresource_version": "v1",
"hello": "world",
},
LabelFromPath: map[string]valuePath{
"name": mustCompilePath(t, "metadata", "name"),
},
},
},
},
},
{
name: "cr metric with custom prefix - expect error",
r: Resource{
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,20 @@ spec:
path: [metadata]
labelsFromPath:
name: [name]
- groupVersionKind:
group: ""
version: v1
kind: Node
metricNamePrefix: demo
metrics:
- name: timestamp
help: "example custom metric from non-custom resource"
each:
type: Gauge
gauge:
path: [metadata]
labelsFromPath:
node: [name]
valueFrom: [creationTimestamp]
`
}

0 comments on commit 286c043

Please sign in to comment.