mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
fix cleanup var 'target.*' (#5888)
Signed-off-by: ShutingZhao <shuting@nirmata.com> Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
parent
e9f338df58
commit
c24e25fb56
24 changed files with 226 additions and 11 deletions
|
@ -111,7 +111,7 @@ func (h *handlers) executePolicy(ctx context.Context, logger logr.Logger, policy
|
||||||
// check conditions
|
// check conditions
|
||||||
if spec.Conditions != nil {
|
if spec.Conditions != nil {
|
||||||
enginectx := enginecontext.NewContext()
|
enginectx := enginecontext.NewContext()
|
||||||
if err := enginectx.AddResource(resource.Object); err != nil {
|
if err := enginectx.AddTargetResource(resource.Object); err != nil {
|
||||||
debug.Error(err, "failed to add resource in context")
|
debug.Error(err, "failed to add resource in context")
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -102,4 +102,4 @@ func validateVariables(logger logr.Logger, policy kyvernov2alpha1.CleanupPolicyI
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowedVariables = regexp.MustCompile(`request\.|images\.|([a-z_0-9]+\()[^{}]`)
|
var allowedVariables = regexp.MustCompile(`target\.|images\.|([a-z_0-9]+\()[^{}]`)
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
|
@ -0,0 +1,36 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: example
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: test-cleanup-pod
|
||||||
|
rules:
|
||||||
|
- apiGroups:
|
||||||
|
- ""
|
||||||
|
resources:
|
||||||
|
- pods
|
||||||
|
verbs:
|
||||||
|
- delete
|
||||||
|
- list
|
||||||
|
- get
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRoleBinding
|
||||||
|
metadata:
|
||||||
|
name: test-cleanup-pod
|
||||||
|
roleRef:
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
kind: ClusterRole
|
||||||
|
name: test-cleanup-pod
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: kyverno-cleanup-controller
|
||||||
|
namespace: kyverno
|
|
@ -0,0 +1,20 @@
|
||||||
|
apiVersion: kyverno.io/v2alpha1
|
||||||
|
kind: ClusterCleanupPolicy
|
||||||
|
metadata:
|
||||||
|
name: cleanup-pod
|
||||||
|
spec:
|
||||||
|
match:
|
||||||
|
any:
|
||||||
|
- resources:
|
||||||
|
kinds:
|
||||||
|
- Pod
|
||||||
|
conditions:
|
||||||
|
all:
|
||||||
|
- key: "{{ target.metadata.name }}"
|
||||||
|
operator: Equals
|
||||||
|
value: example
|
||||||
|
- key: "{{ target.metadata.namespace }}"
|
||||||
|
operator: Equals
|
||||||
|
value: default
|
||||||
|
## execute every minute
|
||||||
|
schedule: "*/1 * * * *"
|
|
@ -0,0 +1,20 @@
|
||||||
|
apiVersion: kyverno.io/v2alpha1
|
||||||
|
kind: ClusterCleanupPolicy
|
||||||
|
metadata:
|
||||||
|
name: cleanup-pod
|
||||||
|
spec:
|
||||||
|
match:
|
||||||
|
any:
|
||||||
|
- resources:
|
||||||
|
kinds:
|
||||||
|
- Pod
|
||||||
|
conditions:
|
||||||
|
all:
|
||||||
|
- key: "{{ target.metadata.name }}"
|
||||||
|
operator: Equals
|
||||||
|
value: example
|
||||||
|
- key: "{{ target.metadata.namespace }}"
|
||||||
|
operator: Equals
|
||||||
|
value: default
|
||||||
|
## execute every minute
|
||||||
|
schedule: "*/1 * * * *"
|
|
@ -0,0 +1,5 @@
|
||||||
|
# A command can only run a single command, not a pipeline and not a script. The program called must exist on the system where the test is run.
|
||||||
|
apiVersion: kuttl.dev/v1beta1
|
||||||
|
kind: TestStep
|
||||||
|
commands:
|
||||||
|
- command: sleep 65
|
|
@ -0,0 +1,9 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: example
|
|
@ -0,0 +1,5 @@
|
||||||
|
# A clean-up is presently required because kuttl does not do a reliable job of cleaning up both cluster-scoped objects.
|
||||||
|
apiVersion: kuttl.dev/v1beta1
|
||||||
|
kind: TestStep
|
||||||
|
commands:
|
||||||
|
- command: kubectl delete -f 00-manifests.yaml,01-policy.yaml --force --wait=true --ignore-not-found=true
|
|
@ -0,0 +1,10 @@
|
||||||
|
# ## Description
|
||||||
|
|
||||||
|
This test cleans up pods via a cluster cleanup policy.
|
||||||
|
|
||||||
|
## Expected Behavior
|
||||||
|
|
||||||
|
The pod `default/example` is cleaned up successfully.
|
||||||
|
|
||||||
|
|
||||||
|
## Reference Issue(s)
|
|
@ -0,0 +1,5 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
|
@ -0,0 +1,36 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: example
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: test-cleanup-pod
|
||||||
|
rules:
|
||||||
|
- apiGroups:
|
||||||
|
- ""
|
||||||
|
resources:
|
||||||
|
- pods
|
||||||
|
verbs:
|
||||||
|
- delete
|
||||||
|
- list
|
||||||
|
- get
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRoleBinding
|
||||||
|
metadata:
|
||||||
|
name: test-cleanup-pod
|
||||||
|
roleRef:
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
kind: ClusterRole
|
||||||
|
name: test-cleanup-pod
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: kyverno-cleanup-controller
|
||||||
|
namespace: kyverno
|
|
@ -0,0 +1,17 @@
|
||||||
|
apiVersion: kyverno.io/v2alpha1
|
||||||
|
kind: CleanupPolicy
|
||||||
|
metadata:
|
||||||
|
name: cleanup-pod
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
match:
|
||||||
|
any:
|
||||||
|
- resources:
|
||||||
|
kinds:
|
||||||
|
- Pod
|
||||||
|
conditions:
|
||||||
|
any:
|
||||||
|
- key: "{{ target.metadata.name }}"
|
||||||
|
operator: Equals
|
||||||
|
value: example
|
||||||
|
schedule: "*/1 * * * *"
|
|
@ -0,0 +1,18 @@
|
||||||
|
apiVersion: kyverno.io/v2alpha1
|
||||||
|
kind: CleanupPolicy
|
||||||
|
metadata:
|
||||||
|
name: cleanup-pod
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
match:
|
||||||
|
any:
|
||||||
|
- resources:
|
||||||
|
kinds:
|
||||||
|
- Pod
|
||||||
|
conditions:
|
||||||
|
any:
|
||||||
|
- key: "{{ target.metadata.name }}"
|
||||||
|
operator: Equals
|
||||||
|
value: example
|
||||||
|
## execute every minute
|
||||||
|
schedule: "*/1 * * * *"
|
|
@ -0,0 +1,5 @@
|
||||||
|
# A command can only run a single command, not a pipeline and not a script. The program called must exist on the system where the test is run.
|
||||||
|
apiVersion: kuttl.dev/v1beta1
|
||||||
|
kind: TestStep
|
||||||
|
commands:
|
||||||
|
- command: sleep 65
|
|
@ -0,0 +1,9 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:latest
|
||||||
|
name: example
|
|
@ -0,0 +1,5 @@
|
||||||
|
# A clean-up is presently required because kuttl does not do a reliable job of cleaning up both cluster-scoped objects.
|
||||||
|
apiVersion: kuttl.dev/v1beta1
|
||||||
|
kind: TestStep
|
||||||
|
commands:
|
||||||
|
- command: kubectl delete -f 00-manifests.yaml,01-policy.yaml --force --wait=true --ignore-not-found=true
|
10
test/conformance/kuttl/cleanup/policy/cleanup-pod/README.md
Normal file
10
test/conformance/kuttl/cleanup/policy/cleanup-pod/README.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# ## Description
|
||||||
|
|
||||||
|
This test cleans up pods via a namespaced cleanup policy.
|
||||||
|
|
||||||
|
## Expected Behavior
|
||||||
|
|
||||||
|
The pod `default/example` is cleaned up successfully.
|
||||||
|
|
||||||
|
|
||||||
|
## Reference Issue(s)
|
|
@ -11,7 +11,7 @@ spec:
|
||||||
- Pod
|
- Pod
|
||||||
conditions:
|
conditions:
|
||||||
any:
|
any:
|
||||||
- key: "{{ request.name }}"
|
- key: "{{ target.metadata.name }}"
|
||||||
operator: Equals
|
operator: Equals
|
||||||
value: example
|
value: example
|
||||||
schedule: "* * * * *"
|
schedule: "*/2 * * * *"
|
|
@ -11,7 +11,7 @@ spec:
|
||||||
- Pod
|
- Pod
|
||||||
conditions:
|
conditions:
|
||||||
any:
|
any:
|
||||||
- key: "{{ request.name }}"
|
- key: "{{ target.metadata.name }}"
|
||||||
operator: Equals
|
operator: Equals
|
||||||
value: example
|
value: example
|
||||||
schedule: "* * * * *"
|
schedule: "*/2 * * * *"
|
|
@ -10,7 +10,7 @@ spec:
|
||||||
- Pod
|
- Pod
|
||||||
conditions:
|
conditions:
|
||||||
any:
|
any:
|
||||||
- key: "{{ request.name }}"
|
- key: "{{ target.metadata.name }}"
|
||||||
operator: Equals
|
operator: Equals
|
||||||
value: example
|
value: example
|
||||||
schedule: "* * * * *"
|
schedule: "*/2 * * * *"
|
|
@ -10,7 +10,7 @@ spec:
|
||||||
- Pod
|
- Pod
|
||||||
conditions:
|
conditions:
|
||||||
any:
|
any:
|
||||||
- key: "{{ request.name }}"
|
- key: "{{ target.metadata.name }}"
|
||||||
operator: Equals
|
operator: Equals
|
||||||
value: example
|
value: example
|
||||||
schedule: "* * * * *"
|
schedule: "*/2 * * * *"
|
|
@ -11,7 +11,7 @@ spec:
|
||||||
- Pod
|
- Pod
|
||||||
conditions:
|
conditions:
|
||||||
any:
|
any:
|
||||||
- key: "{{ request.name }}"
|
- key: "{{ target.metadata.name }}"
|
||||||
operator: Equals
|
operator: Equals
|
||||||
value: example
|
value: example
|
||||||
schedule: "invalid-schedule"
|
schedule: "invalid-schedule"
|
Loading…
Add table
Reference in a new issue