[Sample] Mutating Configmaps

Problem Statement

i want to create a config map mutating policy which replaces any occurance of ap-south-1 to ap-south-2 in the data field of config map. Please help me create a valid policy which does this task

Solution Description

Using the following policy, which is not working

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: replace-ap-south-1-with-ap-south-2
spec:
  rules:
    - name: replace-region-in-configmap
      match:
        resources:
          kinds:
            - ConfigMap
      mutate:
        foreach:
          - list: "request.object.data | keys(@)"
            patchStrategicMerge:
              data:
                "{{ element }}": "{{ request.object.data[element] | replace('ap-south-1', 'ap-south-2') }}"

Example "Good" Resource

Example "Bad" Resource

Other Comments

No response

Slack discussion

No response

Troubleshooting