|  | 
|  | 1 | +/* | 
|  | 2 | +Copyright 2025 The Kubernetes Authors All rights reserved. | 
|  | 3 | +
 | 
|  | 4 | +Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +you may not use this file except in compliance with the License. | 
|  | 6 | +You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +    http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +
 | 
|  | 10 | +Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +See the License for the specific language governing permissions and | 
|  | 14 | +limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +package store | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	basemetrics "k8s.io/component-base/metrics" | 
|  | 21 | + | 
|  | 22 | +	metaapi "k8s.io/apimachinery/pkg/api/meta" | 
|  | 23 | +	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 24 | + | 
|  | 25 | +	"k8s.io/kube-state-metrics/v2/pkg/metric" | 
|  | 26 | + | 
|  | 27 | +	generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" | 
|  | 28 | +) | 
|  | 29 | + | 
|  | 30 | +var ( | 
|  | 31 | +	descAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." | 
|  | 32 | +	descLabelsHelp      = "Kubernetes labels converted to Prometheus labels." | 
|  | 33 | +	descCreationHelp    = "Unix creation timestamp" | 
|  | 34 | +	descDeletionHelp    = "Unix deletion timestamp" | 
|  | 35 | +) | 
|  | 36 | + | 
|  | 37 | +// createMetadataMetricFamiliesGenerator provides metadata metrics for all resources | 
|  | 38 | +func createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descLabelsDefaultLabels []string, descPrefixName string, w func(func(*metav1.ObjectMeta) *metric.Family, []string) func(any) *metric.Family) []generator.FamilyGenerator { | 
|  | 39 | +	wrapFunc := w | 
|  | 40 | + | 
|  | 41 | +	return []generator.FamilyGenerator{ | 
|  | 42 | +		*generator.NewFamilyGeneratorWithStability( | 
|  | 43 | +			descPrefixName+"_created", | 
|  | 44 | +			descCreationHelp, | 
|  | 45 | +			metric.Gauge, | 
|  | 46 | +			basemetrics.STABLE, | 
|  | 47 | +			"", | 
|  | 48 | +			wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { | 
|  | 49 | +				ms := []*metric.Metric{} | 
|  | 50 | + | 
|  | 51 | +				if !t.CreationTimestamp.IsZero() { | 
|  | 52 | +					ms = append(ms, &metric.Metric{ | 
|  | 53 | +						Value: float64(t.CreationTimestamp.Unix()), | 
|  | 54 | +					}) | 
|  | 55 | +				} | 
|  | 56 | + | 
|  | 57 | +				return &metric.Family{ | 
|  | 58 | +					Metrics: ms, | 
|  | 59 | +				} | 
|  | 60 | +			}, descLabelsDefaultLabels), | 
|  | 61 | +		), | 
|  | 62 | +		*generator.NewFamilyGeneratorWithStability( | 
|  | 63 | +			descPrefixName+"_deletion_timestamp", | 
|  | 64 | +			descDeletionHelp, | 
|  | 65 | +			metric.Gauge, | 
|  | 66 | +			basemetrics.ALPHA, | 
|  | 67 | +			"", | 
|  | 68 | +			wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { | 
|  | 69 | +				ms := []*metric.Metric{} | 
|  | 70 | + | 
|  | 71 | +				if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() { | 
|  | 72 | +					ms = append(ms, &metric.Metric{ | 
|  | 73 | +						Value: float64(t.DeletionTimestamp.Unix()), | 
|  | 74 | +					}) | 
|  | 75 | +				} | 
|  | 76 | + | 
|  | 77 | +				return &metric.Family{ | 
|  | 78 | +					Metrics: ms, | 
|  | 79 | +				} | 
|  | 80 | +			}, descLabelsDefaultLabels), | 
|  | 81 | +		), | 
|  | 82 | + | 
|  | 83 | +		*generator.NewFamilyGeneratorWithStability( | 
|  | 84 | +			descPrefixName+"_annotations", | 
|  | 85 | +			descAnnotationsHelp, | 
|  | 86 | +			metric.Gauge, | 
|  | 87 | +			basemetrics.ALPHA, | 
|  | 88 | +			"", | 
|  | 89 | +			wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { | 
|  | 90 | +				if len(allowAnnotationsList) == 0 { | 
|  | 91 | +					return &metric.Family{} | 
|  | 92 | +				} | 
|  | 93 | +				annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList) | 
|  | 94 | +				return &metric.Family{ | 
|  | 95 | +					Metrics: []*metric.Metric{ | 
|  | 96 | +						{ | 
|  | 97 | +							LabelKeys:   annotationKeys, | 
|  | 98 | +							LabelValues: annotationValues, | 
|  | 99 | +							Value:       1, | 
|  | 100 | +						}, | 
|  | 101 | +					}} | 
|  | 102 | +			}, descLabelsDefaultLabels), | 
|  | 103 | +		), | 
|  | 104 | +		*generator.NewFamilyGeneratorWithStability( | 
|  | 105 | +			descPrefixName+"_labels", | 
|  | 106 | +			descLabelsHelp, | 
|  | 107 | +			metric.Gauge, | 
|  | 108 | +			basemetrics.STABLE, | 
|  | 109 | +			"", | 
|  | 110 | +			wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { | 
|  | 111 | +				if len(allowLabelsList) == 0 { | 
|  | 112 | +					return &metric.Family{} | 
|  | 113 | +				} | 
|  | 114 | +				labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList) | 
|  | 115 | +				return &metric.Family{ | 
|  | 116 | +					Metrics: []*metric.Metric{ | 
|  | 117 | +						{ | 
|  | 118 | +							LabelKeys:   labelKeys, | 
|  | 119 | +							LabelValues: labelValues, | 
|  | 120 | +							Value:       1, | 
|  | 121 | +						}, | 
|  | 122 | +					}} | 
|  | 123 | + | 
|  | 124 | +			}, descLabelsDefaultLabels), | 
|  | 125 | +		), | 
|  | 126 | +	} | 
|  | 127 | +} | 
|  | 128 | + | 
|  | 129 | +func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { | 
|  | 130 | +	return func(obj any) *metric.Family { | 
|  | 131 | +		o := obj.(metav1.Object) | 
|  | 132 | +		objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta | 
|  | 133 | +		metricFamily := f(&objectMeta) | 
|  | 134 | + | 
|  | 135 | +		for _, m := range metricFamily.Metrics { | 
|  | 136 | +			m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues) | 
|  | 137 | +		} | 
|  | 138 | + | 
|  | 139 | +		return metricFamily | 
|  | 140 | +	} | 
|  | 141 | +} | 
|  | 142 | + | 
|  | 143 | +func wrapMetadataWithUIDFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { | 
|  | 144 | +	return func(obj any) *metric.Family { | 
|  | 145 | +		o := obj.(metav1.Object) | 
|  | 146 | +		objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta | 
|  | 147 | +		metricFamily := f(&objectMeta) | 
|  | 148 | + | 
|  | 149 | +		for _, m := range metricFamily.Metrics { | 
|  | 150 | +			m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName(), string(o.GetUID())}, m.LabelKeys, m.LabelValues) | 
|  | 151 | +		} | 
|  | 152 | + | 
|  | 153 | +		return metricFamily | 
|  | 154 | +	} | 
|  | 155 | +} | 
0 commit comments