@@ -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,48 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
116
117
})
117
118
})
118
119
})
120
+
121
+ When ("a CSV is created" , 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
+ _ , err = fetchCSV (crc , csv .Name , testNamespace , csvSucceededChecker )
135
+ Expect (err ).ToNot (HaveOccurred ())
136
+ })
137
+ AfterEach (func () {
138
+ if cleanupCSV != nil {
139
+ cleanupCSV ()
140
+ }
141
+ })
142
+ It ("emits a CSV metrics" , func () {
143
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
144
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
145
+ )
146
+ })
147
+ When ("the OLM pod restarts" , func () {
148
+ BeforeEach (func () {
149
+ restartDeploymentWithLabel (c , "app=olm-operator" )
150
+ })
151
+ It ("CSV metric is preserved" , func () {
152
+ Eventually (func () []Metric {
153
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))
154
+ }).Should (ContainElement (LikeMetric (
155
+ WithFamily ("csv_succeeded" ),
156
+ WithName (csv .Name ),
157
+ WithValue (1 ),
158
+ )))
159
+ })
160
+ })
161
+ })
119
162
})
120
163
121
164
Context ("Metrics emitted by objects during operator installation" , func () {
@@ -396,6 +439,51 @@ func getPodWithLabel(client operatorclient.ClientInterface, label string) *corev
396
439
return & podList .Items [0 ]
397
440
}
398
441
442
+ func getDeploymentWithLabel (client operatorclient.ClientInterface , label string ) * appsv1.Deployment {
443
+ listOptions := metav1.ListOptions {LabelSelector : label }
444
+ var deploymentList * appsv1.DeploymentList
445
+ EventuallyWithOffset (1 , func () (numDeps int , err error ) {
446
+ deploymentList , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).List (context .TODO (), listOptions )
447
+ if deploymentList != nil {
448
+ numDeps = len (deploymentList .Items )
449
+ }
450
+
451
+ return
452
+ }).Should (Equal (1 ), "expected exactly one Deployment" )
453
+
454
+ return & deploymentList .Items [0 ]
455
+ }
456
+
457
+ func restartDeploymentWithLabel (client operatorclient.ClientInterface , l string ) {
458
+ d := getDeploymentWithLabel (client , l )
459
+ z := int32 (0 )
460
+ oldZ := * d .Spec .Replicas
461
+ d .Spec .Replicas = & z
462
+ _ , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), d , metav1.UpdateOptions {})
463
+ Expect (err ).ToNot (HaveOccurred ())
464
+
465
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
466
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
467
+ if deployment != nil {
468
+ replicas = deployment .Status .Replicas
469
+ }
470
+ return
471
+ }).Should (Equal (int32 (0 )), "expected exactly 0 Deployments" )
472
+
473
+ updated := getDeploymentWithLabel (client , l )
474
+ updated .Spec .Replicas = & oldZ
475
+ _ , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), updated , metav1.UpdateOptions {})
476
+ Expect (err ).ToNot (HaveOccurred ())
477
+
478
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
479
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
480
+ if deployment != nil {
481
+ replicas = deployment .Status .Replicas
482
+ }
483
+ return
484
+ }).Should (Equal (oldZ ), "expected exactly 1 Deployment" )
485
+ }
486
+
399
487
func extractMetricPortFromPod (pod * corev1.Pod ) string {
400
488
for _ , container := range pod .Spec .Containers {
401
489
for _ , port := range container .Ports {
0 commit comments