...
1
16
17 package ipallocator
18
19 import (
20 "sync"
21
22 "k8s.io/component-base/metrics"
23 "k8s.io/component-base/metrics/legacyregistry"
24 )
25
26 const (
27 namespace = "kube_apiserver"
28 subsystem = "clusterip_allocator"
29 )
30
31 var (
32
33 clusterIPAllocated = metrics.NewGaugeVec(
34 &metrics.GaugeOpts{
35 Namespace: namespace,
36 Subsystem: subsystem,
37 Name: "allocated_ips",
38 Help: "Gauge measuring the number of allocated IPs for Services",
39 StabilityLevel: metrics.ALPHA,
40 },
41 []string{"cidr"},
42 )
43
44 clusterIPAvailable = metrics.NewGaugeVec(
45 &metrics.GaugeOpts{
46 Namespace: namespace,
47 Subsystem: subsystem,
48 Name: "available_ips",
49 Help: "Gauge measuring the number of available IPs for Services",
50 StabilityLevel: metrics.ALPHA,
51 },
52 []string{"cidr"},
53 )
54
55 clusterIPAllocations = metrics.NewCounterVec(
56 &metrics.CounterOpts{
57 Namespace: namespace,
58 Subsystem: subsystem,
59 Name: "allocation_total",
60 Help: "Number of Cluster IPs allocations",
61 StabilityLevel: metrics.ALPHA,
62 },
63 []string{"cidr", "scope"},
64 )
65
66 clusterIPAllocationErrors = metrics.NewCounterVec(
67 &metrics.CounterOpts{
68 Namespace: namespace,
69 Subsystem: subsystem,
70 Name: "allocation_errors_total",
71 Help: "Number of errors trying to allocate Cluster IPs",
72 StabilityLevel: metrics.ALPHA,
73 },
74 []string{"cidr", "scope"},
75 )
76 )
77
78 var registerMetricsOnce sync.Once
79
80 func registerMetrics() {
81 registerMetricsOnce.Do(func() {
82 legacyregistry.MustRegister(clusterIPAllocated)
83 legacyregistry.MustRegister(clusterIPAvailable)
84 legacyregistry.MustRegister(clusterIPAllocations)
85 legacyregistry.MustRegister(clusterIPAllocationErrors)
86 })
87 }
88
89
90 type metricsRecorderInterface interface {
91 setAllocated(cidr string, allocated int)
92 setAvailable(cidr string, available int)
93 incrementAllocations(cidr, scope string)
94 incrementAllocationErrors(cidr, scope string)
95 }
96
97
98 type metricsRecorder struct{}
99
100 func (m *metricsRecorder) setAllocated(cidr string, allocated int) {
101 clusterIPAllocated.WithLabelValues(cidr).Set(float64(allocated))
102 }
103
104 func (m *metricsRecorder) setAvailable(cidr string, available int) {
105 clusterIPAvailable.WithLabelValues(cidr).Set(float64(available))
106 }
107
108 func (m *metricsRecorder) incrementAllocations(cidr, scope string) {
109 clusterIPAllocations.WithLabelValues(cidr, scope).Inc()
110 }
111
112 func (m *metricsRecorder) incrementAllocationErrors(cidr, scope string) {
113 clusterIPAllocationErrors.WithLabelValues(cidr, scope).Inc()
114 }
115
116
117 type emptyMetricsRecorder struct{}
118
119 func (*emptyMetricsRecorder) setAllocated(cidr string, allocated int) {}
120 func (*emptyMetricsRecorder) setAvailable(cidr string, available int) {}
121 func (*emptyMetricsRecorder) incrementAllocations(cidr, scope string) {}
122 func (*emptyMetricsRecorder) incrementAllocationErrors(cidr, scope string) {}
123
View as plain text