Skip to content

Commit

Permalink
fix: fix mutating the "/metadata/serverAddress" section of a keda.s/v…
Browse files Browse the repository at this point in the history
…1alpha1/ScaledObject object (kyverno#5374)

* fix: fix mutating the "/metadata/serverAddress" section of a keda.sh/v1alpha1/ScaledObject object

Signed-off-by: Rodrigo Fior Kuntzer <[email protected]>

* fix: update test policy

Signed-off-by: Rodrigo Fior Kuntzer <[email protected]>

Signed-off-by: Rodrigo Fior Kuntzer <[email protected]>
Co-authored-by: shuting <[email protected]>
  • Loading branch information
rodrigorfk and realshuting authored Nov 28, 2022
1 parent a09d2a7 commit 08447c1
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/engine/mutate/patch/patchesUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/engine/mutate/patch/patchesUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/patchedResource1.yaml
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/patchedResource2.yaml
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/patchedResource3.yaml
Original file line number Diff line number Diff line change
@@ -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"
37 changes: 37 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/policy.yaml
Original file line number Diff line number Diff line change
@@ -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/"
39 changes: 39 additions & 0 deletions test/cli/test/mutate-keda-scaled-object/resources.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 08447c1

Please sign in to comment.