forked from josh-ferrell/kyverno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get ns labels in the cluster mode when using the CLI (kyverno#10348
) * fix: get ns labels in the cluster mode when using the CLI Signed-off-by: Mariam Fahmy <[email protected]> * chore: fix chainsaw test Signed-off-by: Mariam Fahmy <[email protected]> * Update .vscode/launch.json Co-authored-by: shuting <[email protected]> Signed-off-by: Mariam Fahmy <[email protected]> --------- Signed-off-by: Mariam Fahmy <[email protected]> Signed-off-by: Mariam Fahmy <[email protected]> Co-authored-by: shuting <[email protected]>
- Loading branch information
1 parent
088c07f
commit 5678a60
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
test/conformance/chainsaw/cli/apply/apply-exception-with-ns-selector/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## Description | ||
|
||
This test makes sure that Kyverno CLI apply works as expected when an exception matches a pod with a namespace selector in case of cluster mode. (i.e. `--cluster` flag is set) | ||
|
||
## Steps | ||
|
||
1. - Create a namespace `ns-1` | ||
1. - Label the namespace `ns-1` with `kyverno.tess.io/mutateresource=false` | ||
1. - Create a pod `test-pod` in namespace `ns-1` | ||
1. - Create a policy that requires pod to run as non-root user. | ||
1. - Create an exception that matches any pod whose ns selector is `kyverno.tess.io/mutateresource=false` | ||
1. - Use `kyverno apply` command to apply the policy and the exception in a cluster mode. It is expected to have a `skip` as a result. | ||
|
||
## Reference Issue(s) | ||
|
||
https://github.com/kyverno/kyverno/issues/10260 |
28 changes: 28 additions & 0 deletions
28
test/conformance/chainsaw/cli/apply/apply-exception-with-ns-selector/chainsaw-test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apiVersion: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
creationTimestamp: null | ||
name: apply-exception-with-ns-selector | ||
spec: | ||
steps: | ||
- name: step-01 | ||
try: | ||
- script: | ||
content: kubectl create ns ns-1 | ||
- name: step-02 | ||
try: | ||
- script: | ||
content: kubectl label ns ns-1 kyverno.tess.io/mutateresource=false | ||
- name: step-03 | ||
try: | ||
- script: | ||
content: kubectl apply -f pod.yaml | ||
- name: step-04 | ||
try: | ||
- script: | ||
content: kyverno apply policy.yaml --exception exception.yaml --cluster --namespace ns-1 | ||
check: | ||
($stdout): |- | ||
Applying 3 policy rule(s) to 1 resource(s) with 1 exception(s)... | ||
pass: 0, fail: 0, warn: 0, error: 0, skip: 1 |
24 changes: 24 additions & 0 deletions
24
test/conformance/chainsaw/cli/apply/apply-exception-with-ns-selector/exception.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: kyverno.io/v2beta1 | ||
kind: PolicyException | ||
metadata: | ||
name: exception | ||
spec: | ||
background: false | ||
exceptions: | ||
- policyName: require-run-as-non-root-user | ||
ruleNames: | ||
- run-as-non-root-user | ||
match: | ||
any: | ||
- resources: | ||
namespaceSelector: | ||
matchExpressions: | ||
- key: kyverno.tess.io/mutateresource | ||
operator: In | ||
values: | ||
- "false" | ||
kinds: | ||
- Pod | ||
operations: | ||
- UPDATE | ||
- CREATE |
16 changes: 16 additions & 0 deletions
16
test/conformance/chainsaw/cli/apply/apply-exception-with-ns-selector/pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: test-pod | ||
namespace: ns-1 | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:latest | ||
# Grant necessary permissions using SecurityContext (optional) | ||
securityContext: | ||
runAsUser: 0 | ||
resources: | ||
limits: | ||
cpu: "1" | ||
memory: "1Gi" |
30 changes: 30 additions & 0 deletions
30
test/conformance/chainsaw/cli/apply/apply-exception-with-ns-selector/policy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: require-run-as-non-root-user | ||
spec: | ||
validationFailureAction: Enforce | ||
background: true | ||
rules: | ||
- name: run-as-non-root-user | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- Pod | ||
validate: | ||
message: >- | ||
Running the container as root user is not allowed. | ||
pattern: | ||
spec: | ||
=(securityContext): | ||
=(runAsUser): ">0" | ||
=(ephemeralContainers): | ||
- =(securityContext): | ||
=(runAsUser): ">0" | ||
=(initContainers): | ||
- =(securityContext): | ||
=(runAsUser): ">0" | ||
containers: | ||
- =(securityContext): | ||
=(runAsUser): ">0" |