|
| 1 | +/* |
| 2 | +Copyright 2022 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 | + "context" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/watch" |
| 27 | + clientset "k8s.io/client-go/kubernetes" |
| 28 | + "k8s.io/client-go/tools/cache" |
| 29 | + |
| 30 | + "k8s.io/kube-state-metrics/v2/pkg/metric" |
| 31 | + generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + descServiceAccountLabelsDefaultLabels = []string{"namespace", "serviceaccount", "uid"} |
| 36 | +) |
| 37 | + |
| 38 | +func serviceAccountMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { |
| 39 | + return []generator.FamilyGenerator{ |
| 40 | + createServiceAccountInfoFamilyGenerator(), |
| 41 | + createServiceAccountCreatedFamilyGenerator(), |
| 42 | + createServiceAccountDeletedFamilyGenerator(), |
| 43 | + createServiceAccountSecretFamilyGenerator(), |
| 44 | + createServiceAccountImagePullSecretFamilyGenerator(), |
| 45 | + createServiceAccountAnnotationsGenerator(allowAnnotationsList), |
| 46 | + createServiceAccountLabelsGenerator(allowLabelsList), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func createServiceAccountInfoFamilyGenerator() generator.FamilyGenerator { |
| 51 | + return *generator.NewFamilyGenerator( |
| 52 | + "kube_serviceaccount_info", |
| 53 | + "Information about a service account", |
| 54 | + metric.Gauge, |
| 55 | + "", |
| 56 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 57 | + var labelKeys []string |
| 58 | + var labelValues []string |
| 59 | + |
| 60 | + if sa.AutomountServiceAccountToken != nil { |
| 61 | + labelKeys = append(labelKeys, "automount_token") |
| 62 | + labelValues = append(labelValues, strconv.FormatBool(*sa.AutomountServiceAccountToken)) |
| 63 | + } |
| 64 | + |
| 65 | + return &metric.Family{ |
| 66 | + Metrics: []*metric.Metric{{ |
| 67 | + LabelKeys: labelKeys, |
| 68 | + LabelValues: labelValues, |
| 69 | + Value: 1, |
| 70 | + }}, |
| 71 | + } |
| 72 | + }), |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +func createServiceAccountCreatedFamilyGenerator() generator.FamilyGenerator { |
| 77 | + return *generator.NewFamilyGenerator( |
| 78 | + "kube_serviceaccount_created", |
| 79 | + "Unix creation timestamp", |
| 80 | + metric.Gauge, |
| 81 | + "", |
| 82 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 83 | + var ms []*metric.Metric |
| 84 | + |
| 85 | + if !sa.CreationTimestamp.IsZero() { |
| 86 | + ms = append(ms, &metric.Metric{ |
| 87 | + LabelKeys: []string{}, |
| 88 | + LabelValues: []string{}, |
| 89 | + Value: float64(sa.CreationTimestamp.Unix()), |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + return &metric.Family{ |
| 94 | + Metrics: ms, |
| 95 | + } |
| 96 | + }), |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +func createServiceAccountDeletedFamilyGenerator() generator.FamilyGenerator { |
| 101 | + return *generator.NewFamilyGenerator( |
| 102 | + "kube_serviceaccount_deleted", |
| 103 | + "Unix deletion timestamp", |
| 104 | + metric.Gauge, |
| 105 | + "", |
| 106 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 107 | + var ms []*metric.Metric |
| 108 | + |
| 109 | + if sa.DeletionTimestamp != nil && !sa.DeletionTimestamp.IsZero() { |
| 110 | + ms = append(ms, &metric.Metric{ |
| 111 | + LabelKeys: []string{}, |
| 112 | + LabelValues: []string{}, |
| 113 | + Value: float64(sa.DeletionTimestamp.Unix()), |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + return &metric.Family{ |
| 118 | + Metrics: ms, |
| 119 | + } |
| 120 | + }), |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +func createServiceAccountSecretFamilyGenerator() generator.FamilyGenerator { |
| 125 | + return *generator.NewFamilyGenerator( |
| 126 | + "kube_serviceaccount_secret", |
| 127 | + "Secret being referenced by a service account", |
| 128 | + metric.Gauge, |
| 129 | + "", |
| 130 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 131 | + var ms []*metric.Metric |
| 132 | + |
| 133 | + for _, s := range sa.Secrets { |
| 134 | + ms = append(ms, &metric.Metric{ |
| 135 | + LabelKeys: []string{"name"}, |
| 136 | + LabelValues: []string{s.Name}, |
| 137 | + Value: 1, |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + return &metric.Family{ |
| 142 | + Metrics: ms, |
| 143 | + } |
| 144 | + }), |
| 145 | + ) |
| 146 | +} |
| 147 | + |
| 148 | +func createServiceAccountImagePullSecretFamilyGenerator() generator.FamilyGenerator { |
| 149 | + return *generator.NewFamilyGenerator( |
| 150 | + "kube_serviceaccount_image_pull_secret", |
| 151 | + "Secret being referenced by a service account for the purpose of pulling images", |
| 152 | + metric.Gauge, |
| 153 | + "", |
| 154 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 155 | + var ms []*metric.Metric |
| 156 | + |
| 157 | + for _, s := range sa.ImagePullSecrets { |
| 158 | + ms = append(ms, &metric.Metric{ |
| 159 | + LabelKeys: []string{"name"}, |
| 160 | + LabelValues: []string{s.Name}, |
| 161 | + Value: 1, |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + return &metric.Family{ |
| 166 | + Metrics: ms, |
| 167 | + } |
| 168 | + }), |
| 169 | + ) |
| 170 | +} |
| 171 | + |
| 172 | +func createServiceAccountAnnotationsGenerator(allowAnnotations []string) generator.FamilyGenerator { |
| 173 | + return *generator.NewFamilyGenerator( |
| 174 | + "kube_serviceaccount_annotations", |
| 175 | + "Kubernetes annotations converted to Prometheus labels.", |
| 176 | + metric.Gauge, |
| 177 | + "", |
| 178 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 179 | + annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", sa.Annotations, allowAnnotations) |
| 180 | + m := metric.Metric{ |
| 181 | + LabelKeys: annotationKeys, |
| 182 | + LabelValues: annotationValues, |
| 183 | + Value: 1, |
| 184 | + } |
| 185 | + return &metric.Family{ |
| 186 | + Metrics: []*metric.Metric{&m}, |
| 187 | + } |
| 188 | + }), |
| 189 | + ) |
| 190 | +} |
| 191 | + |
| 192 | +func createServiceAccountLabelsGenerator(allowLabelsList []string) generator.FamilyGenerator { |
| 193 | + return *generator.NewFamilyGenerator( |
| 194 | + "kube_serviceaccount_labels", |
| 195 | + "Kubernetes labels converted to Prometheus labels.", |
| 196 | + metric.Gauge, |
| 197 | + "", |
| 198 | + wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family { |
| 199 | + labelKeys, labelValues := createPrometheusLabelKeysValues("label", sa.Labels, allowLabelsList) |
| 200 | + m := metric.Metric{ |
| 201 | + LabelKeys: labelKeys, |
| 202 | + LabelValues: labelValues, |
| 203 | + Value: 1, |
| 204 | + } |
| 205 | + return &metric.Family{ |
| 206 | + Metrics: []*metric.Metric{&m}, |
| 207 | + } |
| 208 | + }), |
| 209 | + ) |
| 210 | +} |
| 211 | + |
| 212 | +func wrapServiceAccountFunc(f func(*v1.ServiceAccount) *metric.Family) func(interface{}) *metric.Family { |
| 213 | + return func(obj interface{}) *metric.Family { |
| 214 | + serviceAccount := obj.(*v1.ServiceAccount) |
| 215 | + |
| 216 | + metricFamily := f(serviceAccount) |
| 217 | + |
| 218 | + for _, m := range metricFamily.Metrics { |
| 219 | + m.LabelKeys, m.LabelValues = mergeKeyValues(descServiceAccountLabelsDefaultLabels, []string{serviceAccount.Namespace, serviceAccount.Name, string(serviceAccount.UID)}, m.LabelKeys, m.LabelValues) |
| 220 | + } |
| 221 | + |
| 222 | + return metricFamily |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +func createServiceAccountListWatch(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher { |
| 227 | + return &cache.ListWatch{ |
| 228 | + ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { |
| 229 | + opts.FieldSelector = fieldSelector |
| 230 | + return kubeClient.CoreV1().ServiceAccounts(ns).List(context.TODO(), opts) |
| 231 | + }, |
| 232 | + WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { |
| 233 | + opts.FieldSelector = fieldSelector |
| 234 | + return kubeClient.CoreV1().ServiceAccounts(ns).Watch(context.TODO(), opts) |
| 235 | + }, |
| 236 | + } |
| 237 | +} |
0 commit comments