@@ -14,6 +14,7 @@ import (
14
14
. "github.com/onsi/gomega"
15
15
io_prometheus_client "github.com/prometheus/client_model/go"
16
16
"github.com/prometheus/common/expfmt"
17
+ appsv1 "k8s.io/api/apps/v1"
17
18
corev1 "k8s.io/api/core/v1"
18
19
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
20
"k8s.io/apimachinery/pkg/util/net"
@@ -108,6 +109,44 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
108
109
})
109
110
})
110
111
})
112
+
113
+ When ("a CSV is created" , func () {
114
+ var (
115
+ cleanupCSV cleanupFunc
116
+ csv v1alpha1.ClusterServiceVersion
117
+ )
118
+ BeforeEach (func () {
119
+ packageName := genName ("csv-test-" )
120
+ packageStable := fmt .Sprintf ("%s-stable" , packageName )
121
+ csv = newCSV (packageStable , testNamespace , "" , semver .MustParse ("0.1.0" ), nil , nil , nil )
122
+
123
+ var err error
124
+ _ , err = createCSV (c , crc , csv , testNamespace , false , false )
125
+ Expect (err ).ToNot (HaveOccurred ())
126
+ _ , err = fetchCSV (crc , csv .Name , testNamespace , csvSucceededChecker )
127
+ Expect (err ).ToNot (HaveOccurred ())
128
+ })
129
+ AfterEach (func () {
130
+ if cleanupCSV != nil {
131
+ cleanupCSV ()
132
+ }
133
+ })
134
+ It ("emits a CSV metrics" , func () {
135
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
136
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
137
+ )
138
+ })
139
+ When ("the OLM pod restarts" , func () {
140
+ BeforeEach (func () {
141
+ restartDeploymentWithLabel (c , "app=olm-operator" )
142
+ })
143
+ It ("CSV metric is preserved" , func () {
144
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ))).To (
145
+ ContainElement (LikeMetric (WithFamily ("csv_succeeded" ), WithName (csv .Name ), WithValue (1 ))),
146
+ )
147
+ })
148
+ })
149
+ })
111
150
})
112
151
113
152
Context ("Metrics emitted by objects during operator installation" , func () {
@@ -284,6 +323,51 @@ func getPodWithLabel(client operatorclient.ClientInterface, label string) *corev
284
323
return & podList .Items [0 ]
285
324
}
286
325
326
+ func getDeploymentWithLabel (client operatorclient.ClientInterface , label string ) * appsv1.Deployment {
327
+ listOptions := metav1.ListOptions {LabelSelector : label }
328
+ var deploymentList * appsv1.DeploymentList
329
+ EventuallyWithOffset (1 , func () (numDeps int , err error ) {
330
+ deploymentList , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).List (context .TODO (), listOptions )
331
+ if deploymentList != nil {
332
+ numDeps = len (deploymentList .Items )
333
+ }
334
+
335
+ return
336
+ }).Should (Equal (1 ), "expected exactly one Deployment" )
337
+
338
+ return & deploymentList .Items [0 ]
339
+ }
340
+
341
+ func restartDeploymentWithLabel (client operatorclient.ClientInterface , l string ) {
342
+ d := getDeploymentWithLabel (client , l )
343
+ z := int32 (0 )
344
+ oldZ := * d .Spec .Replicas
345
+ d .Spec .Replicas = & z
346
+ _ , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), d , metav1.UpdateOptions {})
347
+ Expect (err ).ToNot (HaveOccurred ())
348
+
349
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
350
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
351
+ if deployment != nil {
352
+ replicas = deployment .Status .Replicas
353
+ }
354
+ return
355
+ }).Should (Equal (int32 (0 )), "expected exactly 0 Deployments" )
356
+
357
+ updated := getDeploymentWithLabel (client , l )
358
+ updated .Spec .Replicas = & oldZ
359
+ _ , err = client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Update (context .TODO (), updated , metav1.UpdateOptions {})
360
+ Expect (err ).ToNot (HaveOccurred ())
361
+
362
+ EventuallyWithOffset (1 , func () (replicas int32 , err error ) {
363
+ deployment , err := client .KubernetesInterface ().AppsV1 ().Deployments (operatorNamespace ).Get (context .TODO (), d .Name , metav1.GetOptions {})
364
+ if deployment != nil {
365
+ replicas = deployment .Status .Replicas
366
+ }
367
+ return
368
+ }).Should (Equal (oldZ ), "expected exactly 1 Deployment" )
369
+ }
370
+
287
371
func extractMetricPortFromPod (pod * corev1.Pod ) string {
288
372
for _ , container := range pod .Spec .Containers {
289
373
for _ , port := range container .Ports {
0 commit comments