Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <jvilaca@redhat.com>
  • Loading branch information
machadovilaca committed May 25, 2023
1 parent ca2979e commit f1aead9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
8 changes: 5 additions & 3 deletions internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func logOperation(result OperationResult, resource client.Object, logger logr.Lo
}

func createOperationMetrics(result OperationResult, expected client.Object, found client.Object, logger logr.Logger) {
if result == OperationResultNone || areDifferentVersions(expected, found) {
if result == OperationResultNone || isVersionUpgrade(result, expected, found) {
return
}

Expand All @@ -414,8 +414,10 @@ func createOperationMetrics(result OperationResult, expected client.Object, foun
TemplatesRestored.Inc()
}

func areDifferentVersions(expected, found client.Object) bool {
return expected.GetLabels()[AppKubernetesVersionLabel] != found.GetLabels()[AppKubernetesVersionLabel]
func isVersionUpgrade(result OperationResult, expected client.Object, found client.Object) bool {
expectedVersion := expected.GetLabels()[AppKubernetesVersionLabel]
foundVersion := found.GetLabels()[AppKubernetesVersionLabel]
return result == OperationResultUpdated && expectedVersion != foundVersion
}

func isResourceOwned(request *Request, expectedObj, foundObj client.Object) (bool, error) {
Expand Down
59 changes: 30 additions & 29 deletions tests/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package tests

import (
"fmt"
"k8s.io/apimachinery/pkg/util/rand"
"net/http"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

templatev1 "github.com/openshift/api/template/v1"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
rbac "k8s.io/api/rbac/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"net/http"
"reflect"
"time"
"sigs.k8s.io/controller-runtime/pkg/client"

"kubevirt.io/ssp-operator/internal/common"
"kubevirt.io/ssp-operator/internal/operands/metrics"
Expand Down Expand Up @@ -155,50 +158,48 @@ var _ = Describe("Metrics", func() {
})

Context("SSP metrics", func() {
var templates *templatev1.TemplateList

getTemplates := func() *templatev1.TemplateList {
templates := &templatev1.TemplateList{}
err := apiClient.List(ctx, templates, &client.ListOptions{Namespace: getSsp().Namespace})
Expect(err).ToNot(HaveOccurred())

if len(templates.Items) == 0 {
Skip("no templates found")
}

return templates
}

BeforeEach(func() {
strategy.SkipSspUpdateTestsIfNeeded()
waitUntilDeployed()

templates = getTemplates()
})

It("[test_id:TODO]should increment total_restored_common_templates during normal reconcile", func() {
restoredCount := totalRestoredTemplatesCount()

testTemplate := createTestTemplate()

pauseSsp()

template := &templatev1.Template{}
Expect(apiClient.Get(ctx, testTemplate.GetKey(), template)).To(Succeed())
testTemplate.Update(template)
Expect(apiClient.Update(ctx, template)).To(Succeed())

unpauseSsp()
waitUntilDeployed()
templates.Items[0].ObjectLabels["test"] = "test"
Expect(apiClient.Update(ctx, &templates.Items[0])).To(Succeed())

Eventually(func() int {
return totalRestoredTemplatesCount()
}, 5*time.Minute, 10*time.Second).Should(BeNumerically(">", restoredCount))
}, 5*time.Minute, 10*time.Second).Should(Equal(restoredCount + 1))
})

It("[test_id:TODO]should not increment total_restored_common_templates during upgrades", func() {
restoredCount := totalRestoredTemplatesCount()

testTemplate := createTestTemplate()
testTemplate.ExpectedLabels[common.AppKubernetesVersionLabel] = "v" + rand.String(5)

pauseSsp()

template := &templatev1.Template{}
Expect(apiClient.Get(ctx, testTemplate.GetKey(), template)).To(Succeed())
testTemplate.Update(template)
Expect(apiClient.Update(ctx, template)).To(Succeed())

unpauseSsp()
waitUntilDeployed()
templates.Items[0].Labels[common.AppKubernetesVersionLabel] = "v" + rand.String(5)
templates.Items[0].ObjectLabels["test"] = "test"
Expect(apiClient.Update(ctx, &templates.Items[0])).To(Succeed())

Consistently(func() int {
return totalRestoredTemplatesCount()
}, 5*time.Minute, 10*time.Second).Should(Equal(restoredCount))
}, 2*time.Minute, 20*time.Second).Should(Equal(restoredCount))
})
})

Expand Down

0 comments on commit f1aead9

Please sign in to comment.