From 263b162e0629dd78814d99ed46a992507b287008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Thu, 28 Aug 2025 10:13:06 +0200 Subject: [PATCH] chore: Reduce code duplication for metadata metric families --- internal/store/certificatesigningrequest.go | 73 +-------- internal/store/clusterrole.go | 75 +--------- internal/store/clusterrolebinding.go | 76 +--------- internal/store/configmap.go | 72 +-------- internal/store/cronjob.go | 75 +--------- internal/store/daemonset.go | 94 +----------- internal/store/deployment.go | 92 +----------- internal/store/horizontalpodautoscaler.go | 110 +------------- internal/store/metadata.go | 155 ++++++++++++++++++++ internal/store/pod.go | 107 +------------- tests/e2e/main_test.go | 1 + 11 files changed, 187 insertions(+), 743 deletions(-) create mode 100644 internal/store/metadata.go diff --git a/internal/store/certificatesigningrequest.go b/internal/store/certificatesigningrequest.go index ea287e1490..b1afb1177f 100644 --- a/internal/store/certificatesigningrequest.go +++ b/internal/store/certificatesigningrequest.go @@ -33,80 +33,13 @@ import ( ) var ( - descCSRAnnotationsName = "kube_certificatesigningrequest_annotations" - descCSRAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descCSRLabelsName = "kube_certificatesigningrequest_labels" - descCSRLabelsHelp = "Kubernetes labels converted to Prometheus labels." descCSRLabelsDefaultLabels = []string{"certificatesigningrequest", "signer_name"} ) func csrMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - descCSRAnnotationsName, - descCSRAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapCSRFunc(func(j *certv1.CertificateSigningRequest) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", j.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descCSRLabelsName, - descCSRLabelsHelp, - metric.Gauge, - basemetrics.STABLE, - "", - wrapCSRFunc(func(j *certv1.CertificateSigningRequest) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", j.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - "kube_certificatesigningrequest_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapCSRFunc(func(csr *certv1.CertificateSigningRequest) *metric.Family { - ms := []*metric.Metric{} - if !csr.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(csr.CreationTimestamp.Unix()), - }) - } + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descCSRLabelsDefaultLabels, "kube_certificatesigningrequest", wrapMetadataFunc) - return &metric.Family{ - Metrics: ms, - } - }), - ), + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_certificatesigningrequest_condition", "The number of each certificatesigningrequest condition", @@ -137,7 +70,7 @@ func csrMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat } }), ), - } + ) } func wrapCSRFunc(f func(*certv1.CertificateSigningRequest) *metric.Family) func(interface{}) *metric.Family { diff --git a/internal/store/clusterrole.go b/internal/store/clusterrole.go index 374a48a10e..6361da6f13 100644 --- a/internal/store/clusterrole.go +++ b/internal/store/clusterrole.go @@ -32,59 +32,12 @@ import ( ) var ( - descClusterRoleAnnotationsName = "kube_clusterrole_annotations" - descClusterRoleAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descClusterRoleLabelsName = "kube_clusterrole_labels" - descClusterRoleLabelsHelp = "Kubernetes labels converted to Prometheus labels." descClusterRoleLabelsDefaultLabels = []string{"clusterrole"} ) func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - descClusterRoleAnnotationsName, - descClusterRoleAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", r.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descClusterRoleLabelsName, - descClusterRoleLabelsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", r.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descClusterRoleLabelsDefaultLabels, "kube_clusterrole", wrapMetadataFunc) + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_clusterrole_info", "Information about cluster role.", @@ -101,28 +54,6 @@ func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) [ } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_clusterrole_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { - ms := []*metric.Metric{} - - if !r.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(r.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), *generator.NewFamilyGeneratorWithStability( "kube_clusterrole_metadata_resource_version", "Resource version representing a specific version of the cluster role.", @@ -135,7 +66,7 @@ func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) [ } }), ), - } + ) } func createClusterRoleListWatch(kubeClient clientset.Interface, _ string, _ string) cache.ListerWatcher { diff --git a/internal/store/clusterrolebinding.go b/internal/store/clusterrolebinding.go index 302a6b40f4..1b45982ed0 100644 --- a/internal/store/clusterrolebinding.go +++ b/internal/store/clusterrolebinding.go @@ -32,59 +32,13 @@ import ( ) var ( - descClusterRoleBindingAnnotationsName = "kube_clusterrolebinding_annotations" - descClusterRoleBindingAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descClusterRoleBindingLabelsName = "kube_clusterrolebinding_labels" - descClusterRoleBindingLabelsHelp = "Kubernetes labels converted to Prometheus labels." descClusterRoleBindingLabelsDefaultLabels = []string{"clusterrolebinding"} ) func clusterRoleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - descClusterRoleBindingAnnotationsName, - descClusterRoleBindingAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleBindingFunc(func(r *rbacv1.ClusterRoleBinding) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", r.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descClusterRoleBindingLabelsName, - descClusterRoleBindingLabelsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleBindingFunc(func(r *rbacv1.ClusterRoleBinding) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", r.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descClusterRoleBindingLabelsDefaultLabels, "kube_clusterrolebinding", wrapMetadataFunc) + + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_clusterrolebinding_info", "Information about clusterrolebinding.", @@ -103,28 +57,6 @@ func clusterRoleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []st } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_clusterrolebinding_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapClusterRoleBindingFunc(func(r *rbacv1.ClusterRoleBinding) *metric.Family { - ms := []*metric.Metric{} - - if !r.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(r.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), *generator.NewFamilyGeneratorWithStability( "kube_clusterrolebinding_metadata_resource_version", "Resource version representing a specific version of the clusterrolebinding.", @@ -137,7 +69,7 @@ func clusterRoleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []st } }), ), - } + ) } func createClusterRoleBindingListWatch(kubeClient clientset.Interface, _ string, _ string) cache.ListerWatcher { diff --git a/internal/store/configmap.go b/internal/store/configmap.go index 7fc7f246b0..55c5793a13 100644 --- a/internal/store/configmap.go +++ b/internal/store/configmap.go @@ -36,51 +36,9 @@ var ( ) func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - "kube_configmap_annotations", - "Kubernetes annotations converted to Prometheus labels.", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", c.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - "kube_configmap_labels", - "Kubernetes labels converted to Prometheus labels.", - metric.Gauge, - basemetrics.STABLE, - "", - wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", c.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descConfigMapLabelsDefaultLabels, "kube_configmap", wrapMetadataFunc) + + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_configmap_info", "Information about configmap.", @@ -97,28 +55,6 @@ func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_configmap_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family { - ms := []*metric.Metric{} - - if !c.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(c.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), *generator.NewFamilyGeneratorWithStability( "kube_configmap_metadata_resource_version", "Resource version representing a specific version of the configmap.", @@ -131,7 +67,7 @@ func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g } }), ), - } + ) } func createConfigMapListWatch(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher { diff --git a/internal/store/cronjob.go b/internal/store/cronjob.go index eae698fafb..4ddaa4a5c2 100644 --- a/internal/store/cronjob.go +++ b/internal/store/cronjob.go @@ -36,59 +36,13 @@ import ( ) var ( - descCronJobAnnotationsName = "kube_cronjob_annotations" //nolint:gosec - descCronJobAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descCronJobLabelsName = "kube_cronjob_labels" - descCronJobLabelsHelp = "Kubernetes labels converted to Prometheus labels." descCronJobLabelsDefaultLabels = []string{"namespace", "cronjob"} ) func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - descCronJobAnnotationsName, - descCronJobAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapCronJobFunc(func(j *batchv1.CronJob) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", j.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descCronJobLabelsName, - descCronJobLabelsHelp, - metric.Gauge, - basemetrics.STABLE, - "", - wrapCronJobFunc(func(j *batchv1.CronJob) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", j.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descCronJobLabelsDefaultLabels, "kube_cronjob", wrapMetadataFunc) + + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_cronjob_info", "Info about cronjob.", @@ -111,27 +65,6 @@ func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_cronjob_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapCronJobFunc(func(j *batchv1.CronJob) *metric.Family { - ms := []*metric.Metric{} - if !j.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(j.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), *generator.NewFamilyGeneratorWithStability( "kube_cronjob_status_active", "Active holds pointers to currently running jobs.", @@ -321,7 +254,7 @@ func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen } }), ), - } + ) } func wrapCronJobFunc(f func(*batchv1.CronJob) *metric.Family) func(interface{}) *metric.Family { diff --git a/internal/store/daemonset.go b/internal/store/daemonset.go index fb9f9f8e87..1f92d51c47 100644 --- a/internal/store/daemonset.go +++ b/internal/store/daemonset.go @@ -32,37 +32,13 @@ import ( ) var ( - descDaemonSetAnnotationsName = "kube_daemonset_annotations" - descDaemonSetAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descDaemonSetLabelsName = "kube_daemonset_labels" - descDaemonSetLabelsHelp = "Kubernetes labels converted to Prometheus labels." descDaemonSetLabelsDefaultLabels = []string{"namespace", "daemonset"} ) func daemonSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - "kube_daemonset_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapDaemonSetFunc(func(d *v1.DaemonSet) *metric.Family { - ms := []*metric.Metric{} - - if !d.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(d.CreationTimestamp.Unix()), - }) - } + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descDaemonSetLabelsDefaultLabels, "kube_daemonset", wrapMetadataFunc) - return &metric.Family{ - Metrics: ms, - } - }), - ), + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_daemonset_status_current_number_scheduled", "The number of nodes running at least one daemon pod and are supposed to.", @@ -223,71 +199,7 @@ func daemonSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_daemonset_deletion_timestamp", - "Unix deletion timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapDaemonSetFunc(func(d *v1.DaemonSet) *metric.Family { - ms := []*metric.Metric{} - - if !d.DeletionTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - Value: float64(d.DeletionTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descDaemonSetAnnotationsName, - descDaemonSetAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapDaemonSetFunc(func(d *v1.DaemonSet) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", d.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descDaemonSetLabelsName, - descDaemonSetLabelsHelp, - metric.Gauge, - basemetrics.STABLE, - "", - wrapDaemonSetFunc(func(d *v1.DaemonSet) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", d.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), - } + ) } func wrapDaemonSetFunc(f func(*v1.DaemonSet) *metric.Family) func(interface{}) *metric.Family { diff --git a/internal/store/deployment.go b/internal/store/deployment.go index 31e3b200a6..491bf40e7e 100644 --- a/internal/store/deployment.go +++ b/internal/store/deployment.go @@ -34,10 +34,6 @@ import ( ) var ( - descDeploymentAnnotationsName = "kube_deployment_annotations" - descDeploymentAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descDeploymentLabelsName = "kube_deployment_labels" - descDeploymentLabelsHelp = "Kubernetes labels converted to Prometheus labels." descDeploymentLabelsDefaultLabels = []string{"namespace", "deployment"} ) @@ -59,27 +55,9 @@ var ( ) func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ - *generator.NewFamilyGeneratorWithStability( - "kube_deployment_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapDeploymentFunc(func(d *v1.Deployment) *metric.Family { - ms := []*metric.Metric{} - - if !d.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - Value: float64(d.CreationTimestamp.Unix()), - }) - } + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descDeploymentLabelsDefaultLabels, "kube_deployment", wrapMetadataFunc) - return &metric.Family{ - Metrics: ms, - } - }), - ), + return append(metadataFamilies, *generator.NewFamilyGeneratorWithStability( "kube_deployment_status_replicas", "The number of replicas per deployment.", @@ -305,71 +283,7 @@ func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) [] } }), ), - *generator.NewFamilyGeneratorWithStability( - "kube_deployment_deletion_timestamp", - "Unix deletion timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapDeploymentFunc(func(d *v1.Deployment) *metric.Family { - ms := []*metric.Metric{} - - if !d.DeletionTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - Value: float64(d.DeletionTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descDeploymentAnnotationsName, - descDeploymentAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapDeploymentFunc(func(d *v1.Deployment) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", d.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ), - *generator.NewFamilyGeneratorWithStability( - descDeploymentLabelsName, - descDeploymentLabelsHelp, - metric.Gauge, - basemetrics.STABLE, - "", - wrapDeploymentFunc(func(d *v1.Deployment) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", d.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ), - } + ) } func wrapDeploymentFunc(f func(*v1.Deployment) *metric.Family) func(interface{}) *metric.Family { diff --git a/internal/store/horizontalpodautoscaler.go b/internal/store/horizontalpodautoscaler.go index eae40a9a53..d24b7ec981 100644 --- a/internal/store/horizontalpodautoscaler.go +++ b/internal/store/horizontalpodautoscaler.go @@ -44,17 +44,15 @@ func (m metricTargetType) String() string { } var ( - descHorizontalPodAutoscalerAnnotationsName = "kube_horizontalpodautoscaler_annotations" - descHorizontalPodAutoscalerAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." - descHorizontalPodAutoscalerLabelsName = "kube_horizontalpodautoscaler_labels" - descHorizontalPodAutoscalerLabelsHelp = "Kubernetes labels converted to Prometheus labels." descHorizontalPodAutoscalerLabelsDefaultLabels = []string{"namespace", "horizontalpodautoscaler"} targetMetricLabels = []string{"metric_name", "metric_target_type"} ) func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descHorizontalPodAutoscalerLabelsDefaultLabels, "kube_horizontalpodautoscaler", wrapMetadataFunc) + + return append(metadataFamilies, createHPAInfo(), createHPAMetaDataGeneration(), createHPASpecMaxReplicas(), @@ -63,12 +61,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat createHPAStatusTargetMetric(), createHPAStatusCurrentReplicas(), createHPAStatusDesiredReplicas(), - createHPAAnnotations(allowAnnotationsList), - createHPALabels(allowLabelsList), createHPAStatusCondition(), - createHPACreated(), - createHPADeletionTimestamp(), - } + ) } func wrapHPAFunc(f func(*autoscaling.HorizontalPodAutoscaler) *metric.Family) func(interface{}) *metric.Family { @@ -338,56 +332,6 @@ func createHPAStatusDesiredReplicas() generator.FamilyGenerator { ) } -func createHPAAnnotations(allowAnnotationsList []string) generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - descHorizontalPodAutoscalerAnnotationsName, - descHorizontalPodAutoscalerAnnotationsHelp, - metric.Gauge, - basemetrics.ALPHA, - "", - wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family { - if len(allowAnnotationsList) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", a.Annotations, allowAnnotationsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - }, - }, - } - }), - ) -} - -func createHPALabels(allowLabelsList []string) generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - descHorizontalPodAutoscalerLabelsName, - descHorizontalPodAutoscalerLabelsHelp, - metric.Gauge, - basemetrics.STABLE, - "", - wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", a.Labels, allowLabelsList) - return &metric.Family{ - Metrics: []*metric.Metric{ - { - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - }, - }, - } - }), - ) -} - func createHPAStatusCondition() generator.FamilyGenerator { return *generator.NewFamilyGeneratorWithStability( "kube_horizontalpodautoscaler_status_condition", @@ -415,49 +359,3 @@ func createHPAStatusCondition() generator.FamilyGenerator { }), ) } - -func createHPACreated() generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_horizontalpodautoscaler_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family { - ms := []*metric.Metric{} - - if !a.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - Value: float64(a.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) -} - -func createHPADeletionTimestamp() generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_horizontalpodautoscaler_deletion_timestamp", - "Unix deletion timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family { - ms := []*metric.Metric{} - - if !a.DeletionTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - Value: float64(a.DeletionTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) -} diff --git a/internal/store/metadata.go b/internal/store/metadata.go new file mode 100644 index 0000000000..cd500e6cd3 --- /dev/null +++ b/internal/store/metadata.go @@ -0,0 +1,155 @@ +/* +Copyright 2025 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 ( + basemetrics "k8s.io/component-base/metrics" + + metaapi "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "k8s.io/kube-state-metrics/v2/pkg/metric" + + generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" +) + +var ( + descAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." + descLabelsHelp = "Kubernetes labels converted to Prometheus labels." + descCreationHelp = "Unix creation timestamp" + descDeletionHelp = "Unix deletion timestamp" +) + +// createMetadataMetricFamiliesGenerator provides metadata metrics for all resources +func createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descLabelsDefaultLabels []string, descPrefixName string, w func(func(*metav1.ObjectMeta) *metric.Family, []string) func(any) *metric.Family) []generator.FamilyGenerator { + wrapFunc := w + + return []generator.FamilyGenerator{ + *generator.NewFamilyGeneratorWithStability( + descPrefixName+"_created", + descCreationHelp, + metric.Gauge, + basemetrics.STABLE, + "", + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { + ms := []*metric.Metric{} + + if !t.CreationTimestamp.IsZero() { + ms = append(ms, &metric.Metric{ + Value: float64(t.CreationTimestamp.Unix()), + }) + } + + return &metric.Family{ + Metrics: ms, + } + }, descLabelsDefaultLabels), + ), + *generator.NewFamilyGeneratorWithStability( + descPrefixName+"_deletion_timestamp", + descDeletionHelp, + metric.Gauge, + basemetrics.ALPHA, + "", + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { + ms := []*metric.Metric{} + + if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() { + ms = append(ms, &metric.Metric{ + Value: float64(t.DeletionTimestamp.Unix()), + }) + } + + return &metric.Family{ + Metrics: ms, + } + }, descLabelsDefaultLabels), + ), + + *generator.NewFamilyGeneratorWithStability( + descPrefixName+"_annotations", + descAnnotationsHelp, + metric.Gauge, + basemetrics.ALPHA, + "", + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { + if len(allowAnnotationsList) == 0 { + return &metric.Family{} + } + annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList) + return &metric.Family{ + Metrics: []*metric.Metric{ + { + LabelKeys: annotationKeys, + LabelValues: annotationValues, + Value: 1, + }, + }} + }, descLabelsDefaultLabels), + ), + *generator.NewFamilyGeneratorWithStability( + descPrefixName+"_labels", + descLabelsHelp, + metric.Gauge, + basemetrics.STABLE, + "", + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { + if len(allowLabelsList) == 0 { + return &metric.Family{} + } + labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList) + return &metric.Family{ + Metrics: []*metric.Metric{ + { + LabelKeys: labelKeys, + LabelValues: labelValues, + Value: 1, + }, + }} + + }, descLabelsDefaultLabels), + ), + } +} + +func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { + return func(obj any) *metric.Family { + o := obj.(metav1.Object) + objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta + metricFamily := f(&objectMeta) + + for _, m := range metricFamily.Metrics { + m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues) + } + + return metricFamily + } +} + +func wrapMetadataWithUIDFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { + return func(obj any) *metric.Family { + o := obj.(metav1.Object) + objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta + metricFamily := f(&objectMeta) + + for _, m := range metricFamily.Metrics { + m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName(), string(o.GetUID())}, m.LabelKeys, m.LabelValues) + } + + return metricFamily + } +} diff --git a/internal/store/pod.go b/internal/store/pod.go index d4744dbb2d..2039365871 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -41,7 +41,8 @@ var ( ) func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { - return []generator.FamilyGenerator{ + metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descPodLabelsDefaultLabels, "kube_pod", wrapMetadataWithUIDFunc) + return append(metadataFamilies, createPodCompletionTimeFamilyGenerator(), createPodContainerInfoFamilyGenerator(), createPodContainerResourceLimitsFamilyGenerator(), @@ -57,8 +58,6 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat createPodContainerStatusTerminatedReasonFamilyGenerator(), createPodContainerStatusWaitingFamilyGenerator(), createPodContainerStatusWaitingReasonFamilyGenerator(), - createPodCreatedFamilyGenerator(), - createPodDeletionTimestampFamilyGenerator(), createPodInfoFamilyGenerator(), createPodIPFamilyGenerator(), createPodInitContainerInfoFamilyGenerator(), @@ -72,8 +71,6 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat createPodInitContainerStatusTerminatedReasonFamilyGenerator(), createPodInitContainerStatusWaitingFamilyGenerator(), createPodInitContainerStatusWaitingReasonFamilyGenerator(), - createPodAnnotationsGenerator(allowAnnotationsList), - createPodLabelsGenerator(allowLabelsList), createPodOverheadCPUCoresFamilyGenerator(), createPodOverheadMemoryBytesFamilyGenerator(), createPodOwnerFamilyGenerator(), @@ -97,7 +94,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat createPodNodeSelectorsFamilyGenerator(), createPodServiceAccountFamilyGenerator(), createPodSchedulerNameFamilyGenerator(), - } + ) } func createPodCompletionTimeFamilyGenerator() generator.FamilyGenerator { @@ -578,56 +575,6 @@ func createPodContainerStatusWaitingReasonFamilyGenerator() generator.FamilyGene ) } -func createPodCreatedFamilyGenerator() generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_pod_created", - "Unix creation timestamp", - metric.Gauge, - basemetrics.STABLE, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - ms := []*metric.Metric{} - - if !p.CreationTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(p.CreationTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) -} - -func createPodDeletionTimestampFamilyGenerator() generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_pod_deletion_timestamp", - "Unix deletion timestamp", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - ms := []*metric.Metric{} - - if p.DeletionTimestamp != nil && !p.DeletionTimestamp.IsZero() { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64(p.DeletionTimestamp.Unix()), - }) - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) -} - func createPodInfoFamilyGenerator() generator.FamilyGenerator { return *generator.NewFamilyGeneratorWithStability( "kube_pod_info", @@ -1061,54 +1008,6 @@ func createPodInitContainerStatusWaitingReasonFamilyGenerator() generator.Family ) } -func createPodAnnotationsGenerator(allowAnnotations []string) generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_pod_annotations", - "Kubernetes annotations converted to Prometheus labels.", - metric.Gauge, - basemetrics.ALPHA, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - if len(allowAnnotations) == 0 { - return &metric.Family{} - } - annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", p.Annotations, allowAnnotations) - m := metric.Metric{ - LabelKeys: annotationKeys, - LabelValues: annotationValues, - Value: 1, - } - return &metric.Family{ - Metrics: []*metric.Metric{&m}, - } - }), - ) -} - -func createPodLabelsGenerator(allowLabelsList []string) generator.FamilyGenerator { - return *generator.NewFamilyGeneratorWithStability( - "kube_pod_labels", - "Kubernetes labels converted to Prometheus labels.", - metric.Gauge, - basemetrics.STABLE, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - if len(allowLabelsList) == 0 { - return &metric.Family{} - } - labelKeys, labelValues := createPrometheusLabelKeysValues("label", p.Labels, allowLabelsList) - m := metric.Metric{ - LabelKeys: labelKeys, - LabelValues: labelValues, - Value: 1, - } - return &metric.Family{ - Metrics: []*metric.Metric{&m}, - } - }), - ) -} - func createPodOverheadCPUCoresFamilyGenerator() generator.FamilyGenerator { return *generator.NewFamilyGeneratorWithStability( "kube_pod_overhead_cpu_cores", diff --git a/tests/e2e/main_test.go b/tests/e2e/main_test.go index 72d3f1db03..b92059c79f 100644 --- a/tests/e2e/main_test.go +++ b/tests/e2e/main_test.go @@ -264,6 +264,7 @@ func TestDefaultCollectorMetricsAvailable(t *testing.T) { } nonResources := map[string]bool{ "builder": true, + "metadata": true, "utils": true, "testutils": true, }