diff --git a/pkg/engine/mutate/patch/patchesUtils.go b/pkg/engine/mutate/patch/patchesUtils.go index 05491e3a9182..ece74cbaea60 100644 --- a/pkg/engine/mutate/patch/patchesUtils.go +++ b/pkg/engine/mutate/patch/patchesUtils.go @@ -130,6 +130,10 @@ func ignorePatch(path string) bool { return true } + if wildcard.Match("/spec/triggers/*/metadata/*", path) { + return false + } + if wildcard.Match("*/metadata", path) { return false } diff --git a/pkg/engine/mutate/patch/patchesUtils_test.go b/pkg/engine/mutate/patch/patchesUtils_test.go index ae7fd0c0dadd..c2182783d444 100644 --- a/pkg/engine/mutate/patch/patchesUtils_test.go +++ b/pkg/engine/mutate/patch/patchesUtils_test.go @@ -205,6 +205,10 @@ func Test_ignorePath(t *testing.T) { path: "/kind", ignore: false, }, + { + path: "/spec/triggers/0/metadata/serverAddress", + ignore: false, + }, } for _, test := range tests { diff --git a/test/cli/test/mutate-keda-scaled-object/kyverno-test.yaml b/test/cli/test/mutate-keda-scaled-object/kyverno-test.yaml new file mode 100644 index 000000000000..16956b1bcb1b --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/kyverno-test.yaml @@ -0,0 +1,24 @@ +name: mutate-keda-scaled-object +policies: + - policy.yaml +resources: + - resources.yaml +results: + - policy: keda-prometheus-serveraddress + rule: keda-prometheus-serveraddress + resources: [service-1] + patchedResource: patchedResource1.yaml + kind: ScaledObject + result: pass + - policy: keda-prometheus-serveraddress + rule: keda-prometheus-serveraddress + resources: [service-2] + patchedResource: patchedResource2.yaml + kind: ScaledObject + result: pass + - policy: keda-prometheus-serveraddress + rule: keda-prometheus-serveraddress + resources: [service-3] + patchedResource: patchedResource3.yaml + kind: ScaledObject + result: pass diff --git a/test/cli/test/mutate-keda-scaled-object/patchedResource1.yaml b/test/cli/test/mutate-keda-scaled-object/patchedResource1.yaml new file mode 100644 index 000000000000..a0aa297e0d9f --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/patchedResource1.yaml @@ -0,0 +1,13 @@ +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-1 + namespace: default +spec: + triggers: + - type: "prometheus" + metadata: + metricName: my-metric-name + query: sum(waiting_jobs_total{job='some-job'}) + serverAddress: http://prometheus.local/ + threshold: "100" diff --git a/test/cli/test/mutate-keda-scaled-object/patchedResource2.yaml b/test/cli/test/mutate-keda-scaled-object/patchedResource2.yaml new file mode 100644 index 000000000000..6096d08224bf --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/patchedResource2.yaml @@ -0,0 +1,13 @@ +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-2 + namespace: default +spec: + triggers: + - type: "prometheus" + metadata: + metricName: my-metric-name + query: sum(waiting_jobs_total{job='some-job'}) + serverAddress: http://prometheus.local/ + threshold: "50" diff --git a/test/cli/test/mutate-keda-scaled-object/patchedResource3.yaml b/test/cli/test/mutate-keda-scaled-object/patchedResource3.yaml new file mode 100644 index 000000000000..724357a8e62d --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/patchedResource3.yaml @@ -0,0 +1,12 @@ +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-3 + namespace: default +spec: + triggers: + - type: "aws-sqs-queue" + metadata: + queueURL: https://sqs.eu-west-1.amazonaws.com/account_id/QueueName + queueLength: "5" + awsRegion: "eu-west-1" diff --git a/test/cli/test/mutate-keda-scaled-object/policy.yaml b/test/cli/test/mutate-keda-scaled-object/policy.yaml new file mode 100644 index 000000000000..dd3fbe2585ca --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/policy.yaml @@ -0,0 +1,37 @@ +apiVersion : kyverno.io/v1 +kind: ClusterPolicy +metadata: + name: keda-prometheus-serveraddress + annotations: + policies.kyverno.io/title: Set KEDA Prometheus Scaler ServerAddress + policies.kyverno.io/subject: KedaPrometheusScaler + policies.kyverno.io/description: >- + The KEDA Prometheus Scaler requires the serverAddress attribute. +spec: + background: false + rules: + - name: keda-prometheus-serveraddress + match: + all: + - resources: + kinds: + - keda.sh/v1alpha1/ScaledObject + preconditions: + all: + - key: "{{request.operation}}" + operator: In + value: + - CREATE + - UPDATE + mutate: + foreach: + - list: "request.object.spec.triggers" + preconditions: + all: + - key: "{{element.type || ''}}" + operator: Equals + value: "prometheus" + patchesJson6902: |- + - path: /spec/triggers/{{elementIndex}}/metadata/serverAddress + op: add + value: "http://prometheus.local/" diff --git a/test/cli/test/mutate-keda-scaled-object/resources.yaml b/test/cli/test/mutate-keda-scaled-object/resources.yaml new file mode 100644 index 000000000000..66d4e667a63d --- /dev/null +++ b/test/cli/test/mutate-keda-scaled-object/resources.yaml @@ -0,0 +1,39 @@ +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-1 + namespace: default +spec: + triggers: + - type: "prometheus" + metadata: + metricName: my-metric-name + query: sum(waiting_jobs_total{job='some-job'}) + serverAddress: http://prometheus.internal.svc.cluster.local:9090 + threshold: "100" +--- +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-2 + namespace: default +spec: + triggers: + - type: "prometheus" + metadata: + metricName: my-metric-name + query: sum(waiting_jobs_total{job='some-job'}) + threshold: "50" +--- +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: service-3 + namespace: default +spec: + triggers: + - type: "aws-sqs-queue" + metadata: + queueURL: https://sqs.eu-west-1.amazonaws.com/account_id/QueueName + queueLength: "5" + awsRegion: "eu-west-1"