|
| 1 | +// Copyright (c) 2025 Red Hat, Inc. |
| 2 | +// Copyright Contributors to the Open Cluster Management project |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + |
| 15 | + "open-cluster-management.io/config-policy-controller/test/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("Test config policy ratelimiting", Ordered, func() { |
| 19 | + const ( |
| 20 | + policyName = "case46-cfgpolicy" |
| 21 | + policyYaml = "../resources/case46_ratelimit/" + policyName + ".yaml" |
| 22 | + configMapName = "case46-configmap-to-watch" |
| 23 | + configMapYaml = "../resources/case46_ratelimit/" + configMapName + ".yaml" |
| 24 | + managedCMName = "case46-configmap-from-policy" |
| 25 | + ) |
| 26 | + |
| 27 | + metricCheck := func(metricName string, label string, value string) (float64, error) { |
| 28 | + metric := utils.GetMetrics( |
| 29 | + metricName, fmt.Sprintf(`%s=\"%s\"`, label, value)) |
| 30 | + if len(metric) == 0 { |
| 31 | + return 0, fmt.Errorf("failed to retrieve any %s metric", metricName) |
| 32 | + } |
| 33 | + metricVal, err := strconv.ParseFloat(metric[0], 64) |
| 34 | + if err != nil { |
| 35 | + return 0, fmt.Errorf("error converting metric: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + return metricVal, nil |
| 39 | + } |
| 40 | + |
| 41 | + BeforeAll(func() { |
| 42 | + By("Creating " + policyYaml) |
| 43 | + utils.Kubectl("apply", "-f", policyYaml, "-n", testNamespace) |
| 44 | + By("Creating " + configMapYaml) |
| 45 | + utils.Kubectl("apply", "-f", configMapYaml) // The YAML specifies namespace "default" |
| 46 | + }) |
| 47 | + |
| 48 | + It("should initially have a small number of evaluations", func() { |
| 49 | + Eventually( |
| 50 | + metricCheck, 10, 2, |
| 51 | + ).WithArguments("config_policy_evaluation_total", "name", policyName).Should(BeNumerically("<", 4)) |
| 52 | + |
| 53 | + Consistently( |
| 54 | + metricCheck, 10, 2, |
| 55 | + ).WithArguments("config_policy_evaluation_total", "name", policyName).Should(BeNumerically("<", 4)) |
| 56 | + }) |
| 57 | + |
| 58 | + value := 0 |
| 59 | + |
| 60 | + It("should limit the number of evaluations when a watched object changes frequently", func() { |
| 61 | + start := time.Now() |
| 62 | + |
| 63 | + By("Updating the watched configmap frequently for 10 seconds") |
| 64 | + for start.Add(10 * time.Second).After(time.Now()) { |
| 65 | + value++ |
| 66 | + utils.Kubectl("patch", "configmap", configMapName, "--type=json", "-p", |
| 67 | + `[{"op": "replace", "path": "/data/foo", "value": "`+strconv.Itoa(value)+`"}]`) |
| 68 | + time.Sleep(150 * time.Millisecond) |
| 69 | + } |
| 70 | + |
| 71 | + Consistently( |
| 72 | + metricCheck, 10, 2, |
| 73 | + ).WithArguments("config_policy_evaluation_total", "name", policyName).Should(BeNumerically("<", 12)) |
| 74 | + }) |
| 75 | + |
| 76 | + It("should have updated the object to the final value", func() { |
| 77 | + By("Verifying the configmap has bar=" + strconv.Itoa(value)) |
| 78 | + Eventually(func(g Gomega) { |
| 79 | + cm := utils.GetWithTimeout(clientManagedDynamic, gvrConfigMap, |
| 80 | + managedCMName, "default", true, defaultTimeoutSeconds) |
| 81 | + g.Expect(cm).NotTo(BeNil()) |
| 82 | + |
| 83 | + val, found, err := unstructured.NestedString(cm.Object, "data", "bar") |
| 84 | + g.Expect(err).NotTo(HaveOccurred()) |
| 85 | + g.Expect(found).To(BeTrue()) |
| 86 | + g.Expect(val).Should(Equal(strconv.Itoa(value))) |
| 87 | + }, defaultTimeoutSeconds, 1).Should(Succeed()) |
| 88 | + }) |
| 89 | + |
| 90 | + AfterAll(func() { |
| 91 | + utils.KubectlDelete("-n", testNamespace, "-f", policyYaml) |
| 92 | + utils.KubectlDelete("-f", configMapYaml) |
| 93 | + utils.KubectlDelete("configmap", "-n", "default", "case46-configmap-from-policy") |
| 94 | + }) |
| 95 | +}) |
0 commit comments