Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] policies with both clone AND mutate rules (with targets set) do not clone #9571

Closed
2 tasks done
thesuperzapper opened this issue Jan 30, 2024 · 5 comments
Closed
2 tasks done
Labels
bug Something isn't working generation Issues pertaining to the generate ability.

Comments

@thesuperzapper
Copy link
Contributor

Kyverno Version

1.11.4

Description

In Kyverno 1.11.4 (and probably others too), if a ClusterPolicy contains both a clone and mutate rule (with targets set), the clone rule in the policy is never applied.

Note the clone rule WILL apply if the mutate rule does not set mutate.targets.

Steps to reproduce:

  1. Create some namespaces for the clone:
apiVersion: v1
kind: Namespace
metadata:
  name: source-namespace
apiVersion: v1
kind: Namespace
metadata:
  name: target-namespace-1
  1. Create the source resource:
apiVersion: v1
kind: Secret
metadata:
  name: source-secret-1
  namespace: source-namespace
data:
  key1: "dmFsdWUx"
  key2: "dmFsdWUy"
  1. Create some other resource to target in the mutate rule:
apiVersion: v1
kind: ConfigMap
metadata:
  name: random-configmap
  namespace: source-namespace
data:
  key1: "value1"
  key2: "value2"
  1. Create the ClusterPolicy
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: clone-secret-1-policy
spec:
  generateExisting: true
  rules:
    - name: clone-secret
      match:
        any:
          - resources:
              kinds:
                - Namespace
              names:
                - "target-namespace-1"
      generate:
        apiVersion: v1
        kind: Secret
        name: "target-secret-1"
        namespace: "{{ request.object.metadata.name }}"
        synchronize: true
        clone:
          namespace: "source-namespace"
          name: "source-secret-1"

    - name: random-mutate-trigger
      match:
        any:
          ## match can be any resource, and the issue still occurs
          - resources:
              kinds:
                - Secret
              names:
                - "source-secret-1"
              namespaces:
                - "source-namespace"
      mutate:
        ## targets can be anything (even empty), and the issue still occurs
        targets:
          - apiVersion: v1
            kind: ConfigMap
            name: "random-configmap"
            namespace: "source-namespace"

        ## patch can be anything (even empty), and the issue still occurs
        patchStrategicMerge:
          metadata:
            annotations:
              my_annotation: "{{ time_now_utc() }}"
  1. Notice that Secret/target-secret-1 is NOT created in target-namespace-1
  2. Trigger the mutate rule by patching its match resource:
apiVersion: v1
kind: Secret
metadata:
  name: source-secret-1
  namespace: source-namespace
data:
  key1: "dmFsdWUx"
  key2: "dmFsdWUy"
  key3: "dmFsdWUz"
  1. Notice that ConfigMap/random-configmap was annotated with my_annotation: "2024-01-30T21:50:40Z"
  2. Notice that Secret/target-secret-1 is still NOT created in target-namespace-1

Slack discussion

No response

Troubleshooting

  • I have read and followed the documentation AND the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.
@thesuperzapper thesuperzapper added bug Something isn't working triage Default label assigned to all new issues indicating label curation is needed to fully organize. labels Jan 30, 2024
@thesuperzapper
Copy link
Contributor Author

@chipzoller @realshuting we should either document this as a known issue, or try and fix it in the next release.

It's very common to want a policy that both "clones" a Secret, and also triggers restarts of Deployments that use that secret (listening to updates on the source secret, to avoid the fact that Kyverno ignores its own updates).

But right now (1.11.4), these rules have to be in separate policies, or the clone wont apply.

@realshuting
Copy link
Member

You need to set mutateExistingOnPolicyUpdate: true to trigger the second rule on policy creation. Verified with the latest main and it worked as expected.

✗ k get cpol -o yaml | grep mutateExistingOnPolicyUpdate              
    mutateExistingOnPolicyUpdate: true

✗ k get secret -n target-namespace-1
NAME              TYPE     DATA   AGE
target-secret-1   Opaque   2      3s

✗ k get cm random-configmap -n source-namespace -o yaml | grep my_annotation
    my_annotation: "2024-02-01T06:34:13Z"

@thesuperzapper
Copy link
Contributor Author

thesuperzapper commented Feb 1, 2024

@realshuting I am very confused by your response. I think we need to reopen this issue.

The issue is not about whether the mutate rule fires on creation of the policy, it's about the clone rule not firing at all.

Specifically, in 1.11.4 if there is a clone rule in a policy in addition to a mutate rule (with a target defined), then the clone rule does not ever trigger.

Are you saying that this issue has been resolved already?

@realshuting
Copy link
Member

I was testing against the latest main and it has no issue cloning the secret. Let me verify on 1.11.x.

@realshuting
Copy link
Member

realshuting commented Feb 1, 2024

I can confirm that the clone rule in the combined policy doesn't work in 1.11.4.

It looks like the issue has been fixed in main and will be available in 1.12, can you please verify the same?

@realshuting realshuting added generation Issues pertaining to the generate ability. and removed triage Default label assigned to all new issues indicating label curation is needed to fully organize. labels Feb 1, 2024
@realshuting realshuting added this to the Kyverno Release 1.12.0 milestone Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working generation Issues pertaining to the generate ability.
Projects
None yet
Development

No branches or pull requests

2 participants