|
| 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, withUID bool) []generator.FamilyGenerator { |
| 39 | + wrapFunc := wrapMetadataFunc |
| 40 | + if withUID == true { |
| 41 | + wrapFunc = wrapMetadataWithUIDFunc |
| 42 | + } |
| 43 | + |
| 44 | + return []generator.FamilyGenerator{ |
| 45 | + *generator.NewFamilyGeneratorWithStability( |
| 46 | + descPrefixName+"_created", |
| 47 | + descCreationHelp, |
| 48 | + metric.Gauge, |
| 49 | + basemetrics.STABLE, |
| 50 | + "", |
| 51 | + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { |
| 52 | + ms := []*metric.Metric{} |
| 53 | + |
| 54 | + if !t.CreationTimestamp.IsZero() { |
| 55 | + ms = append(ms, &metric.Metric{ |
| 56 | + Value: float64(t.CreationTimestamp.Unix()), |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + return &metric.Family{ |
| 61 | + Metrics: ms, |
| 62 | + } |
| 63 | + }, descLabelsDefaultLabels), |
| 64 | + ), |
| 65 | + *generator.NewFamilyGeneratorWithStability( |
| 66 | + descPrefixName+"_deletion_timestamp", |
| 67 | + descDeletionHelp, |
| 68 | + metric.Gauge, |
| 69 | + basemetrics.ALPHA, |
| 70 | + "", |
| 71 | + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { |
| 72 | + ms := []*metric.Metric{} |
| 73 | + |
| 74 | + if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() { |
| 75 | + ms = append(ms, &metric.Metric{ |
| 76 | + Value: float64(t.DeletionTimestamp.Unix()), |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return &metric.Family{ |
| 81 | + Metrics: ms, |
| 82 | + } |
| 83 | + }, descLabelsDefaultLabels), |
| 84 | + ), |
| 85 | + |
| 86 | + *generator.NewFamilyGeneratorWithStability( |
| 87 | + descPrefixName+"_annotations", |
| 88 | + descAnnotationsHelp, |
| 89 | + metric.Gauge, |
| 90 | + basemetrics.ALPHA, |
| 91 | + "", |
| 92 | + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { |
| 93 | + if len(allowAnnotationsList) == 0 { |
| 94 | + return &metric.Family{} |
| 95 | + } |
| 96 | + annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList) |
| 97 | + return &metric.Family{ |
| 98 | + Metrics: []*metric.Metric{ |
| 99 | + { |
| 100 | + LabelKeys: annotationKeys, |
| 101 | + LabelValues: annotationValues, |
| 102 | + Value: 1, |
| 103 | + }, |
| 104 | + }} |
| 105 | + }, descLabelsDefaultLabels), |
| 106 | + ), |
| 107 | + *generator.NewFamilyGeneratorWithStability( |
| 108 | + descPrefixName+"_labels", |
| 109 | + descLabelsHelp, |
| 110 | + metric.Gauge, |
| 111 | + basemetrics.STABLE, |
| 112 | + "", |
| 113 | + wrapFunc(func(t *metav1.ObjectMeta) *metric.Family { |
| 114 | + if len(allowLabelsList) == 0 { |
| 115 | + return &metric.Family{} |
| 116 | + } |
| 117 | + labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList) |
| 118 | + return &metric.Family{ |
| 119 | + Metrics: []*metric.Metric{ |
| 120 | + { |
| 121 | + LabelKeys: labelKeys, |
| 122 | + LabelValues: labelValues, |
| 123 | + Value: 1, |
| 124 | + }, |
| 125 | + }} |
| 126 | + |
| 127 | + }, descLabelsDefaultLabels), |
| 128 | + ), |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { |
| 133 | + return func(obj any) *metric.Family { |
| 134 | + o := obj.(metav1.Object) |
| 135 | + objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta |
| 136 | + metricFamily := f(&objectMeta) |
| 137 | + |
| 138 | + for _, m := range metricFamily.Metrics { |
| 139 | + m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues) |
| 140 | + } |
| 141 | + |
| 142 | + return metricFamily |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func wrapMetadataWithUIDFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family { |
| 147 | + return func(obj any) *metric.Family { |
| 148 | + o := obj.(metav1.Object) |
| 149 | + objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta |
| 150 | + metricFamily := f(&objectMeta) |
| 151 | + |
| 152 | + for _, m := range metricFamily.Metrics { |
| 153 | + m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName(), string(o.GetUID())}, m.LabelKeys, m.LabelValues) |
| 154 | + } |
| 155 | + |
| 156 | + return metricFamily |
| 157 | + } |
| 158 | +} |
0 commit comments