|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors All rights reserved. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package store |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + basemetrics "k8s.io/component-base/metrics" |
| 20 | + |
| 21 | + "k8s.io/kube-state-metrics/v2/pkg/metric" |
| 22 | + generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" |
| 23 | + |
| 24 | + networkingv1 "k8s.io/api/networking/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/apimachinery/pkg/watch" |
| 28 | + clientset "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/client-go/tools/cache" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + descIngressClassAnnotationsName = "kube_ingressclass_annotations" |
| 34 | + descIngressClassAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." |
| 35 | + descIngressClassLabelsName = "kube_ingressclass_labels" |
| 36 | + descIngressClassLabelsHelp = "Kubernetes labels converted to Prometheus labels." |
| 37 | + descIngressClassLabelsDefaultLabels = []string{"ingressclass"} |
| 38 | +) |
| 39 | + |
| 40 | +func ingressClassMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { |
| 41 | + return []generator.FamilyGenerator{ |
| 42 | + *generator.NewFamilyGeneratorWithStability( |
| 43 | + "kube_ingressclass_info", |
| 44 | + "Information about ingressclass.", |
| 45 | + metric.Gauge, |
| 46 | + basemetrics.ALPHA, |
| 47 | + "", |
| 48 | + wrapIngressClassFunc(func(s *networkingv1.IngressClass) *metric.Family { |
| 49 | + |
| 50 | + m := metric.Metric{ |
| 51 | + LabelKeys: []string{"controller"}, |
| 52 | + LabelValues: []string{s.Spec.Controller}, |
| 53 | + Value: 1, |
| 54 | + } |
| 55 | + return &metric.Family{Metrics: []*metric.Metric{&m}} |
| 56 | + }), |
| 57 | + ), |
| 58 | + *generator.NewFamilyGeneratorWithStability( |
| 59 | + "kube_ingressclass_created", |
| 60 | + "Unix creation timestamp", |
| 61 | + metric.Gauge, |
| 62 | + basemetrics.ALPHA, |
| 63 | + "", |
| 64 | + wrapIngressClassFunc(func(s *networkingv1.IngressClass) *metric.Family { |
| 65 | + ms := []*metric.Metric{} |
| 66 | + if !s.CreationTimestamp.IsZero() { |
| 67 | + ms = append(ms, &metric.Metric{ |
| 68 | + Value: float64(s.CreationTimestamp.Unix()), |
| 69 | + }) |
| 70 | + } |
| 71 | + return &metric.Family{ |
| 72 | + Metrics: ms, |
| 73 | + } |
| 74 | + }), |
| 75 | + ), |
| 76 | + *generator.NewFamilyGeneratorWithStability( |
| 77 | + descIngressClassAnnotationsName, |
| 78 | + descIngressClassAnnotationsHelp, |
| 79 | + metric.Gauge, |
| 80 | + basemetrics.ALPHA, |
| 81 | + "", |
| 82 | + wrapIngressClassFunc(func(s *networkingv1.IngressClass) *metric.Family { |
| 83 | + annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", s.Annotations, allowAnnotationsList) |
| 84 | + return &metric.Family{ |
| 85 | + Metrics: []*metric.Metric{ |
| 86 | + { |
| 87 | + LabelKeys: annotationKeys, |
| 88 | + LabelValues: annotationValues, |
| 89 | + Value: 1, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + }), |
| 94 | + ), |
| 95 | + *generator.NewFamilyGeneratorWithStability( |
| 96 | + descIngressClassLabelsName, |
| 97 | + descIngressClassLabelsHelp, |
| 98 | + metric.Gauge, |
| 99 | + basemetrics.ALPHA, |
| 100 | + "", |
| 101 | + wrapIngressClassFunc(func(s *networkingv1.IngressClass) *metric.Family { |
| 102 | + labelKeys, labelValues := createPrometheusLabelKeysValues("label", s.Labels, allowLabelsList) |
| 103 | + return &metric.Family{ |
| 104 | + Metrics: []*metric.Metric{ |
| 105 | + { |
| 106 | + LabelKeys: labelKeys, |
| 107 | + LabelValues: labelValues, |
| 108 | + Value: 1, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + }), |
| 113 | + ), |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func wrapIngressClassFunc(f func(*networkingv1.IngressClass) *metric.Family) func(interface{}) *metric.Family { |
| 118 | + return func(obj interface{}) *metric.Family { |
| 119 | + ingressClass := obj.(*networkingv1.IngressClass) |
| 120 | + |
| 121 | + metricFamily := f(ingressClass) |
| 122 | + |
| 123 | + for _, m := range metricFamily.Metrics { |
| 124 | + m.LabelKeys, m.LabelValues = mergeKeyValues(descIngressClassLabelsDefaultLabels, []string{ingressClass.Name}, m.LabelKeys, m.LabelValues) |
| 125 | + } |
| 126 | + |
| 127 | + return metricFamily |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func createIngressClassListWatch(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher { |
| 132 | + return &cache.ListWatch{ |
| 133 | + ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { |
| 134 | + return kubeClient.NetworkingV1().IngressClasses().List(context.TODO(), opts) |
| 135 | + }, |
| 136 | + WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { |
| 137 | + return kubeClient.NetworkingV1().IngressClasses().Watch(context.TODO(), opts) |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
0 commit comments