-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow optional VK in CR metrics, while only requiring the group to be fixed. This would allow users to define custom metrics for: * a specific API version: version is fixed, kind varies. * all the API versions of a resource: version varies, kind is fixed. * all the APIs part of an API group: version varies, kind varies. * also allow users to keep a tab on CRDs by exposing metrics for the same. Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
- Loading branch information
Showing
6 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CustomResourceDefinition Metrics | ||
|
||
| Metric name | Metric type | Labels/tags | Status | | ||
|---------------------------------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------| | ||
| kube_customresourcedefinition_created | Gauge | `customresourcedefinition`=<customresourcedefinition-name> <br> `namespace`=<customresourcedefinition-namespace> | EXPERIMENTAL | | ||
| kube_customresourcedefinition_info | Gauge | `customresourcedefinition`=<customresourcedefinition-name> <br> `namespace`=<customresourcedefinition-namespace> <br> `group`=<customresourcedefinition-group> <br> `versions`=<customresourcedefinition-versions> `kind`=<customresourcedefinition-kind> <br> `scope`=<customresourcedefinition-scope> | EXPERIMENTAL | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/tools/cache" | ||
basemetrics "k8s.io/component-base/metrics" | ||
|
||
"k8s.io/kube-state-metrics/v2/pkg/metric" | ||
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" | ||
) | ||
|
||
func customResourceDefinitionMetricFamilies() []generator.FamilyGenerator { | ||
getCRDVersions := func(crds []v1.CustomResourceDefinitionVersion) string { | ||
var versions []string | ||
for _, crd := range crds { | ||
versions = append(versions, crd.String()) | ||
} | ||
return strings.Join(versions, ",") | ||
} | ||
return []generator.FamilyGenerator{ | ||
*generator.NewFamilyGeneratorWithStability( | ||
"kube_customresourcedefinition_created", | ||
"Unix creation timestamp", | ||
metric.Gauge, | ||
basemetrics.ALPHA, | ||
"", | ||
wrapCustomResourceDefinitionFunc(func(crd *v1.CustomResourceDefinition) *metric.Family { | ||
var ms []*metric.Metric | ||
|
||
if !crd.CreationTimestamp.IsZero() { | ||
ms = append(ms, &metric.Metric{ | ||
Value: float64(crd.CreationTimestamp.Unix()), | ||
}) | ||
} | ||
|
||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
), | ||
*generator.NewFamilyGeneratorWithStability( | ||
"kube_customresourcedefinition_info", | ||
"Information about customresourcedefinition.", | ||
metric.Gauge, | ||
basemetrics.ALPHA, | ||
"", | ||
wrapCustomResourceDefinitionFunc(func(crd *v1.CustomResourceDefinition) *metric.Family { | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: []string{"group", "versions", "kinds", "scope"}, | ||
LabelValues: []string{crd.Spec.Group, getCRDVersions(crd.Spec.Versions), crd.Spec.Names.Kind, string(crd.Spec.Scope)}, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
), | ||
} | ||
} | ||
|
||
func wrapCustomResourceDefinitionFunc(f func(*v1.CustomResourceDefinition) *metric.Family) func(interface{}) *metric.Family { | ||
return func(obj interface{}) *metric.Family { | ||
crd := obj.(*v1.CustomResourceDefinition) | ||
|
||
metricFamily := f(crd) | ||
|
||
for _, m := range metricFamily.Metrics { | ||
m.LabelKeys, m.LabelValues = mergeKeyValues(descDeploymentLabelsDefaultLabels, []string{crd.Namespace, crd.Name}, m.LabelKeys, m.LabelValues) | ||
} | ||
|
||
return metricFamily | ||
} | ||
} | ||
|
||
func createCustomResourceDefinitionListWatch(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher { | ||
return &cache.ListWatch{ | ||
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { | ||
opts.FieldSelector = fieldSelector | ||
return kubeClient.ApiextensionsV1().CustomResourceDefinitions().List(context.TODO(), opts) | ||
}, | ||
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { | ||
opts.FieldSelector = fieldSelector | ||
return kubeClient.ApiextensionsV1().CustomResourceDefinitions().Watch(context.TODO(), opts) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters