7
7
8
8
"github.com/sirupsen/logrus"
9
9
10
- corev1 "k8s.io/api/core/v1"
11
- "k8s.io/apimachinery/pkg/types"
12
10
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
13
11
14
12
"github.com/prometheus/client_golang/prometheus"
@@ -17,6 +15,8 @@ import (
17
15
ctrmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
18
16
)
19
17
18
+ const MetricNamespace = "version_checker"
19
+
20
20
// Metrics is used to expose container image version checks as prometheus
21
21
// metrics.
22
22
type Metrics struct {
@@ -39,12 +39,13 @@ type Metrics struct {
39
39
// func New(log *logrus.Entry, reg ctrmetrics.RegistererGatherer, kubeClient k8sclient.Client) *Metrics {
40
40
func New (log * logrus.Entry , reg ctrmetrics.RegistererGatherer , cache k8sclient.Reader ) * Metrics {
41
41
// Attempt to register, but ignore errors
42
+ // TODO: We should check for AlreadyRegisteredError err type here for better error handling
42
43
_ = reg .Register (collectors .NewProcessCollector (collectors.ProcessCollectorOpts {}))
43
44
_ = reg .Register (collectors .NewGoCollector ())
44
45
45
46
containerImageVersion := promauto .With (reg ).NewGaugeVec (
46
47
prometheus.GaugeOpts {
47
- Namespace : "version_checker" ,
48
+ Namespace : MetricNamespace ,
48
49
Name : "is_latest_version" ,
49
50
Help : "Where the container in use is using the latest upstream registry version" ,
50
51
},
@@ -54,7 +55,7 @@ func New(log *logrus.Entry, reg ctrmetrics.RegistererGatherer, cache k8sclient.R
54
55
)
55
56
containerImageChecked := promauto .With (reg ).NewGaugeVec (
56
57
prometheus.GaugeOpts {
57
- Namespace : "version_checker" ,
58
+ Namespace : MetricNamespace ,
58
59
Name : "last_checked" ,
59
60
Help : "Timestamp when the image was checked" ,
60
61
},
@@ -64,15 +65,15 @@ func New(log *logrus.Entry, reg ctrmetrics.RegistererGatherer, cache k8sclient.R
64
65
)
65
66
containerImageDuration := promauto .With (reg ).NewGaugeVec (
66
67
prometheus.GaugeOpts {
67
- Namespace : "version_checker" ,
68
+ Namespace : MetricNamespace ,
68
69
Name : "image_lookup_duration" ,
69
70
Help : "Time taken to lookup version." ,
70
71
},
71
72
[]string {"namespace" , "pod" , "container" , "image" },
72
73
)
73
74
containerImageErrors := promauto .With (reg ).NewCounterVec (
74
75
prometheus.CounterOpts {
75
- Namespace : "version_checker" ,
76
+ Namespace : MetricNamespace ,
76
77
Name : "image_failures_total" ,
77
78
Help : "Total number of errors where the version-checker was unable to get the latest upstream registry version" ,
78
79
},
@@ -104,12 +105,12 @@ func (m *Metrics) AddImage(namespace, pod, container, containerType, imageURL st
104
105
}
105
106
106
107
m .containerImageVersion .With (
107
- m . buildFullLabels (namespace , pod , container , containerType , imageURL , currentVersion , latestVersion ),
108
+ buildFullLabels (namespace , pod , container , containerType , imageURL , currentVersion , latestVersion ),
108
109
).Set (isLatestF )
109
110
110
111
// Bump last updated timestamp
111
112
m .containerImageChecked .With (
112
- m . buildLastUpdatedLabels (namespace , pod , container , containerType , imageURL ),
113
+ buildLastUpdatedLabels (namespace , pod , container , containerType , imageURL ),
113
114
).Set (float64 (time .Now ().Unix ()))
114
115
}
115
116
@@ -118,20 +119,14 @@ func (m *Metrics) RemoveImage(namespace, pod, container, containerType string) {
118
119
defer m .mu .Unlock ()
119
120
total := 0
120
121
121
- total += m .containerImageVersion .DeletePartialMatch (
122
- m .buildPartialLabels (namespace , pod ),
123
- )
124
- total += m .containerImageDuration .DeletePartialMatch (
125
- m .buildPartialLabels (namespace , pod ),
126
- )
122
+ labels := buildContainerPartialLabels (namespace , pod , container , containerType )
127
123
128
- total += m .containerImageChecked .DeletePartialMatch (
129
- m .buildPartialLabels (namespace , pod ),
130
- )
131
- total += m .containerImageErrors .DeletePartialMatch (
132
- m .buildPartialLabels (namespace , pod ),
133
- )
134
- m .log .Infof ("Removed %d metrics for image %s/%s/%s" , total , namespace , pod , container )
124
+ total += m .containerImageVersion .DeletePartialMatch (labels )
125
+ total += m .containerImageDuration .DeletePartialMatch (labels )
126
+ total += m .containerImageChecked .DeletePartialMatch (labels )
127
+ total += m .containerImageErrors .DeletePartialMatch (labels )
128
+
129
+ m .log .Infof ("Removed %d metrics for image %s/%s/%s (%s)" , total , namespace , pod , container , containerType )
135
130
}
136
131
137
132
func (m * Metrics ) RemovePod (namespace , pod string ) {
@@ -140,16 +135,16 @@ func (m *Metrics) RemovePod(namespace, pod string) {
140
135
141
136
total := 0
142
137
total += m .containerImageVersion .DeletePartialMatch (
143
- m . buildPartialLabels (namespace , pod ),
138
+ buildPodPartialLabels (namespace , pod ),
144
139
)
145
140
total += m .containerImageDuration .DeletePartialMatch (
146
- m . buildPartialLabels (namespace , pod ),
141
+ buildPodPartialLabels (namespace , pod ),
147
142
)
148
143
total += m .containerImageChecked .DeletePartialMatch (
149
- m . buildPartialLabels (namespace , pod ),
144
+ buildPodPartialLabels (namespace , pod ),
150
145
)
151
146
total += m .containerImageErrors .DeletePartialMatch (
152
- m . buildPartialLabels (namespace , pod ),
147
+ buildPodPartialLabels (namespace , pod ),
153
148
)
154
149
155
150
m .log .Infof ("Removed %d metrics for pod %s/%s" , total , namespace , pod )
@@ -182,39 +177,3 @@ func (m *Metrics) ReportError(namespace, pod, container, imageURL string) {
182
177
namespace , pod , container , imageURL ,
183
178
).Inc ()
184
179
}
185
-
186
- func (m * Metrics ) buildFullLabels (namespace , pod , container , containerType , imageURL , currentVersion , latestVersion string ) prometheus.Labels {
187
- return prometheus.Labels {
188
- "namespace" : namespace ,
189
- "pod" : pod ,
190
- "container_type" : containerType ,
191
- "container" : container ,
192
- "image" : imageURL ,
193
- "current_version" : currentVersion ,
194
- "latest_version" : latestVersion ,
195
- }
196
- }
197
-
198
- func (m * Metrics ) buildLastUpdatedLabels (namespace , pod , container , containerType , imageURL string ) prometheus.Labels {
199
- return prometheus.Labels {
200
- "namespace" : namespace ,
201
- "pod" : pod ,
202
- "container_type" : containerType ,
203
- "container" : container ,
204
- "image" : imageURL ,
205
- }
206
- }
207
-
208
- func (m * Metrics ) buildPartialLabels (namespace , pod string ) prometheus.Labels {
209
- return prometheus.Labels {
210
- "namespace" : namespace ,
211
- "pod" : pod ,
212
- }
213
- }
214
-
215
- // This _should_ leverage the Controllers Cache
216
- func (m * Metrics ) PodExists (ctx context.Context , ns , name string ) bool {
217
- pod := & corev1.Pod {}
218
- err := m .cache .Get (ctx , types.NamespacedName {Name : name , Namespace : ns }, pod )
219
- return err == nil && pod .GetDeletionTimestamp () == nil
220
- }
0 commit comments