1
+ //go:build !bare
1
2
// +build !bare
2
3
3
4
package e2e
4
5
5
6
import (
6
7
"bytes"
7
8
"context"
9
+ "fmt"
8
10
"regexp"
11
+ "strconv"
9
12
"strings"
10
13
14
+ "github.com/blang/semver/v4"
11
15
. "github.com/onsi/ginkgo"
12
16
. "github.com/onsi/gomega"
13
17
io_prometheus_client "github.com/prometheus/client_model/go"
14
18
"github.com/prometheus/common/expfmt"
19
+ appsv1 "k8s.io/api/apps/v1"
15
20
corev1 "k8s.io/api/core/v1"
16
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
22
"k8s.io/apimachinery/pkg/util/net"
@@ -71,8 +76,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
71
76
})
72
77
73
78
It ("generates csv_abnormal metric for OLM pod" , func () {
74
-
75
- Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ), "8081" )).To (And (
79
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (And (
76
80
ContainElement (LikeMetric (
77
81
WithFamily ("csv_abnormal" ),
78
82
WithName (failingCSV .Name ),
@@ -100,13 +104,55 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
100
104
101
105
It ("deletes its associated CSV metrics" , func () {
102
106
// Verify that when the csv has been deleted, it deletes the corresponding CSV metrics
103
- Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ), "8081" )).ToNot (And (
107
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).ToNot (And (
104
108
ContainElement (LikeMetric (WithFamily ("csv_abnormal" ), WithName (failingCSV .Name ))),
105
109
ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (failingCSV .Name ))),
106
110
))
107
111
})
108
112
})
109
113
})
114
+
115
+ When ("a CSV is created" , func () {
116
+ var (
117
+ cleanupCSV cleanupFunc
118
+ csv v1alpha1.ClusterServiceVersion
119
+ )
120
+ BeforeEach (func () {
121
+ packageName := genName ("csv-test-" )
122
+ packageStable := fmt .Sprintf ("%s-stable" , packageName )
123
+ csv = newCSV (packageStable , testNamespace , "" , semver .MustParse ("0.1.0" ), nil , nil , nil )
124
+
125
+ var err error
126
+ _ , err = createCSV (c , crc , csv , testNamespace , false , false )
127
+ Expect (err ).ToNot (HaveOccurred ())
128
+ _ , err = fetchCSV (crc , csv .Name , testNamespace , csvSucceededChecker )
129
+ Expect (err ).ToNot (HaveOccurred ())
130
+ })
131
+ AfterEach (func () {
132
+ if cleanupCSV != nil {
133
+ cleanupCSV ()
134
+ }
135
+ })
136
+ It ("emits a CSV metrics" , func () {
137
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
138
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
139
+ )
140
+ })
141
+ When ("the OLM pod restarts" , func () {
142
+ BeforeEach (func () {
143
+ restartDeploymentWithLabel (c , "app=olm-operator" )
144
+ })
145
+ It ("CSV metric is preserved" , func () {
146
+ Eventually (func () []Metric {
147
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))
148
+ }).Should (ContainElement (LikeMetric (
149
+ WithFamily ("csv_succeeded" ),
150
+ WithName (csv .Name ),
151
+ WithValue (1 ),
152
+ )))
153
+ })
154
+ })
155
+ })
110
156
})
111
157
112
158
Context ("Metrics emitted by objects during operator installation" , func () {
@@ -130,7 +176,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
130
176
131
177
// Verify metrics have been emitted for subscription
132
178
Eventually (func () []Metric {
133
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
179
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
134
180
}).Should (ContainElement (LikeMetric (
135
181
WithFamily ("subscription_sync_total" ),
136
182
WithName ("metric-subscription-for-create" ),
@@ -145,7 +191,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
145
191
// Verify metrics have been emitted for dependency resolution
146
192
Eventually (func () bool {
147
193
return Eventually (func () []Metric {
148
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
194
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
149
195
}).Should (ContainElement (LikeMetric (
150
196
WithFamily ("olm_resolution_duration_seconds" ),
151
197
WithLabel ("outcome" , "failed" ),
@@ -160,7 +206,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
160
206
BeforeEach (func () {
161
207
subscriptionCleanup , subscription = createSubscription (GinkgoT (), crc , testNamespace , "metric-subscription-for-update" , testPackageName , stableChannel , v1alpha1 .ApprovalManual )
162
208
Eventually (func () []Metric {
163
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
209
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
164
210
}).Should (ContainElement (LikeMetric (WithFamily ("subscription_sync_total" ), WithLabel ("name" , "metric-subscription-for-update" ))))
165
211
Eventually (func () error {
166
212
s , err := crc .OperatorsV1alpha1 ().Subscriptions (subscription .GetNamespace ()).Get (context .TODO (), subscription .GetName (), metav1.GetOptions {})
@@ -181,7 +227,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
181
227
182
228
It ("deletes the old Subscription metric and emits the new metric" , func () {
183
229
Eventually (func () []Metric {
184
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
230
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
185
231
}).Should (And (
186
232
Not (ContainElement (LikeMetric (
187
233
WithFamily ("subscription_sync_total" ),
@@ -215,7 +261,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
215
261
216
262
It ("deletes the old subscription metric and emits the new metric(there is only one metric for the subscription)" , func () {
217
263
Eventually (func () []Metric {
218
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
264
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
219
265
}).Should (And (
220
266
Not (ContainElement (LikeMetric (
221
267
WithFamily ("subscription_sync_total" ),
@@ -245,7 +291,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
245
291
BeforeEach (func () {
246
292
subscriptionCleanup , subscription = createSubscription (GinkgoT (), crc , testNamespace , "metric-subscription-for-delete" , testPackageName , stableChannel , v1alpha1 .ApprovalManual )
247
293
Eventually (func () []Metric {
248
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
294
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
249
295
}).Should (ContainElement (LikeMetric (WithFamily ("subscription_sync_total" ), WithLabel ("name" , "metric-subscription-for-delete" ))))
250
296
if subscriptionCleanup != nil {
251
297
subscriptionCleanup ()
@@ -261,7 +307,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
261
307
262
308
It ("deletes the Subscription metric" , func () {
263
309
Eventually (func () []Metric {
264
- return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
310
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ))
265
311
}).ShouldNot (ContainElement (LikeMetric (WithFamily ("subscription_sync_total" ), WithName ("metric-subscription-for-delete" ))))
266
312
})
267
313
})
@@ -283,7 +329,63 @@ func getPodWithLabel(client operatorclient.ClientInterface, label string) *corev
283
329
return & podList .Items [0 ]
284
330
}
285
331
286
- func getMetricsFromPod (client operatorclient.ClientInterface , pod * corev1.Pod , port string ) []Metric {
332
+ func getDeploymentWithLabel (client operatorclient.ClientInterface , label string ) * appsv1.Deployment {
333
+ listOptions := metav1.ListOptions {LabelSelector : label }
334
+ var deploymentList * appsv1.DeploymentList
335
+ EventuallyWithOffset (1 , func () (numDeps int , err error ) {
336
+ deploymentList , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).List (context .TODO (), listOptions )
337
+ if deploymentList != nil {
338
+ numDeps = len (deploymentList .Items )
339
+ }
340
+
341
+ return
342
+ }).Should (Equal (1 ), "expected exactly one Deployment" )
343
+
344
+ return & deploymentList .Items [0 ]
345
+ }
346
+
347
+ func restartDeploymentWithLabel (client operatorclient.ClientInterface , l string ) {
348
+ d := getDeploymentWithLabel (client , l )
349
+ z := int32 (0 )
350
+ oldZ := * d .Spec .Replicas
351
+ d .Spec .Replicas = & z
352
+ _ , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), d , metav1.UpdateOptions {})
353
+ Expect (err ).ToNot (HaveOccurred ())
354
+
355
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
356
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
357
+ if deployment != nil {
358
+ replicas = deployment .Status .Replicas
359
+ }
360
+ return
361
+ }).Should (Equal (int32 (0 )), "expected exactly 0 Deployments" )
362
+
363
+ updated := getDeploymentWithLabel (client , l )
364
+ updated .Spec .Replicas = & oldZ
365
+ _ , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), updated , metav1.UpdateOptions {})
366
+ Expect (err ).ToNot (HaveOccurred ())
367
+
368
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
369
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
370
+ if deployment != nil {
371
+ replicas = deployment .Status .Replicas
372
+ }
373
+ return
374
+ }).Should (Equal (oldZ ), "expected exactly 1 Deployment" )
375
+ }
376
+
377
+ func extractMetricPortFromPod (pod * corev1.Pod ) string {
378
+ for _ , container := range pod .Spec .Containers {
379
+ for _ , port := range container .Ports {
380
+ if port .Name == "metrics" {
381
+ return strconv .Itoa (int (port .ContainerPort ))
382
+ }
383
+ }
384
+ }
385
+ return "-1"
386
+ }
387
+
388
+ func getMetricsFromPod (client operatorclient.ClientInterface , pod * corev1.Pod ) []Metric {
287
389
ctx .Ctx ().Logf ("querying pod %s/%s\n " , pod .GetNamespace (), pod .GetName ())
288
390
289
391
// assuming -tls-cert and -tls-key aren't used anywhere else as a parameter value
@@ -305,14 +407,13 @@ func getMetricsFromPod(client operatorclient.ClientInterface, pod *corev1.Pod, p
305
407
scheme = "http"
306
408
}
307
409
ctx .Ctx ().Logf ("Retrieving metrics using scheme %v\n " , scheme )
308
-
309
410
mfs := make (map [string ]* io_prometheus_client.MetricFamily )
310
411
EventuallyWithOffset (1 , func () error {
311
412
raw , err := client .KubernetesInterface ().CoreV1 ().RESTClient ().Get ().
312
413
Namespace (pod .GetNamespace ()).
313
414
Resource ("pods" ).
314
415
SubResource ("proxy" ).
315
- Name (net .JoinSchemeNamePort (scheme , pod .GetName (), port )).
416
+ Name (net .JoinSchemeNamePort (scheme , pod .GetName (), extractMetricPortFromPod ( pod ) )).
316
417
Suffix ("metrics" ).
317
418
Do (context .Background ()).Raw ()
318
419
if err != nil {
0 commit comments