...
1
16
17 package portallocator
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 = "nodeport_allocator"
29 )
30
31 var (
32
33 nodePortAllocated = metrics.NewGauge(
34 &metrics.GaugeOpts{
35 Namespace: namespace,
36 Subsystem: subsystem,
37 Name: "allocated_ports",
38 Help: "Gauge measuring the number of allocated NodePorts for Services",
39 StabilityLevel: metrics.ALPHA,
40 },
41 )
42
43 nodePortAvailable = metrics.NewGauge(
44 &metrics.GaugeOpts{
45 Namespace: namespace,
46 Subsystem: subsystem,
47 Name: "available_ports",
48 Help: "Gauge measuring the number of available NodePorts for Services",
49 StabilityLevel: metrics.ALPHA,
50 },
51 )
52
53 nodePortAllocations = metrics.NewCounterVec(
54 &metrics.CounterOpts{
55 Namespace: namespace,
56 Subsystem: subsystem,
57 Name: "allocation_total",
58 Help: "Number of NodePort allocations",
59 StabilityLevel: metrics.ALPHA,
60 },
61 []string{"scope"},
62 )
63
64 nodePortAllocationErrors = metrics.NewCounterVec(
65 &metrics.CounterOpts{
66 Namespace: namespace,
67 Subsystem: subsystem,
68 Name: "allocation_errors_total",
69 Help: "Number of errors trying to allocate NodePort",
70 StabilityLevel: metrics.ALPHA,
71 },
72 []string{"scope"},
73 )
74 )
75
76 var registerMetricsOnce sync.Once
77
78 func registerMetrics() {
79 registerMetricsOnce.Do(func() {
80 legacyregistry.MustRegister(nodePortAllocated)
81 legacyregistry.MustRegister(nodePortAvailable)
82 legacyregistry.MustRegister(nodePortAllocations)
83 legacyregistry.MustRegister(nodePortAllocationErrors)
84 })
85 }
86
87
88 type metricsRecorderInterface interface {
89 setAllocated(allocated int)
90 setAvailable(available int)
91 incrementAllocations(scope string)
92 incrementAllocationErrors(scope string)
93 }
94
95
96 type metricsRecorder struct{}
97
98 func (m *metricsRecorder) setAllocated(allocated int) {
99 nodePortAllocated.Set(float64(allocated))
100 }
101
102 func (m *metricsRecorder) setAvailable(available int) {
103 nodePortAvailable.Set(float64(available))
104 }
105
106 func (m *metricsRecorder) incrementAllocations(scope string) {
107 nodePortAllocations.WithLabelValues(scope).Inc()
108 }
109
110 func (m *metricsRecorder) incrementAllocationErrors(scope string) {
111 nodePortAllocationErrors.WithLabelValues(scope).Inc()
112 }
113
114
115 type emptyMetricsRecorder struct{}
116
117 func (*emptyMetricsRecorder) setAllocated(allocated int) {}
118 func (*emptyMetricsRecorder) setAvailable(available int) {}
119 func (*emptyMetricsRecorder) incrementAllocations(scope string) {}
120 func (*emptyMetricsRecorder) incrementAllocationErrors(scope string) {}
121
View as plain text