@@ -16,6 +16,7 @@ import (
16
16
. "github.com/onsi/gomega"
17
17
io_prometheus_client "github.com/prometheus/client_model/go"
18
18
"github.com/prometheus/common/expfmt"
19
+ appsv1 "k8s.io/api/apps/v1"
19
20
corev1 "k8s.io/api/core/v1"
20
21
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
21
22
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -116,6 +117,45 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
116
117
})
117
118
})
118
119
})
120
+
121
+ When ("the OLM pod restars" , func () {
122
+ var (
123
+ cleanupCSV cleanupFunc
124
+ csv v1alpha1.ClusterServiceVersion
125
+ )
126
+ BeforeEach (func () {
127
+ packageName := genName ("csv-test-" )
128
+ packageStable := fmt .Sprintf ("%s-stable" , packageName )
129
+ csv = newCSV (packageStable , testNamespace , "" , semver .MustParse ("0.1.0" ), nil , nil , nil )
130
+
131
+ var err error
132
+ _ , err = createCSV (c , crc , csv , testNamespace , false , false )
133
+ Expect (err ).ToNot (HaveOccurred ())
134
+
135
+ _ , err = fetchCSV (crc , csv .Name , testNamespace , csvSucceededChecker )
136
+ Expect (err ).ToNot (HaveOccurred ())
137
+
138
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
139
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
140
+ )
141
+ })
142
+ AfterEach (func () {
143
+ if cleanupCSV != nil {
144
+ cleanupCSV ()
145
+ }
146
+ })
147
+ It ("csv metric is preserved" , func () {
148
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
149
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
150
+ )
151
+
152
+ restartDeploymentWithLabel (c , "app=olm-operator" )
153
+
154
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
155
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
156
+ )
157
+ })
158
+ })
119
159
})
120
160
121
161
Context ("Metrics emitted by objects during operator installation" , func () {
@@ -396,6 +436,51 @@ func getPodWithLabel(client operatorclient.ClientInterface, label string) *corev
396
436
return & podList .Items [0 ]
397
437
}
398
438
439
+ func getDeploymentWithLabel (client operatorclient.ClientInterface , label string ) * appsv1.Deployment {
440
+ listOptions := metav1.ListOptions {LabelSelector : label }
441
+ var deploymentList * appsv1.DeploymentList
442
+ EventuallyWithOffset (1 , func () (numDeps int , err error ) {
443
+ deploymentList , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).List (context .TODO (), listOptions )
444
+ if deploymentList != nil {
445
+ numDeps = len (deploymentList .Items )
446
+ }
447
+
448
+ return
449
+ }).Should (Equal (1 ), "expected exactly one Deployment" )
450
+
451
+ return & deploymentList .Items [0 ]
452
+ }
453
+
454
+ func restartDeploymentWithLabel (client operatorclient.ClientInterface , l string ) {
455
+ d := getDeploymentWithLabel (client , l )
456
+ z := int32 (0 )
457
+ oldZ := * d .Spec .Replicas
458
+ d .Spec .Replicas = & z
459
+ _ , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), d , metav1.UpdateOptions {})
460
+ Expect (err ).ToNot (HaveOccurred ())
461
+
462
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
463
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
464
+ if deployment != nil {
465
+ replicas = deployment .Status .Replicas
466
+ }
467
+ return
468
+ }).Should (Equal (int32 (0 )), "expected exactly 0 Deployments" )
469
+
470
+ updated := getDeploymentWithLabel (client , l )
471
+ updated .Spec .Replicas = & oldZ
472
+ _ , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), updated , metav1.UpdateOptions {})
473
+ Expect (err ).ToNot (HaveOccurred ())
474
+
475
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
476
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
477
+ if deployment != nil {
478
+ replicas = deployment .Status .Replicas
479
+ }
480
+ return
481
+ }).Should (Equal (oldZ ), "expected exactly 1 Deployment" )
482
+ }
483
+
399
484
func extractMetricPortFromPod (pod * corev1.Pod ) string {
400
485
for _ , container := range pod .Spec .Containers {
401
486
for _ , port := range container .Ports {
0 commit comments