Skip to content

Commit

Permalink
Add mutate policy for PAAS to add imagePullSecrets for osc-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
treydock committed May 5, 2024
1 parent 81c5e96 commit a6ff45f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
34 changes: 34 additions & 0 deletions charts/kyverno-policies/templates/add-image-pull-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-image-pull-secret
spec:
validationFailureAction: Enforce
background: true
rules:
- name: paas-osc-registry
match:
any:
- resources:
kinds:
- Pod
namespaceSelector:
matchExpressions:
- key: osc.edu/role
operator: In
values:
- paas
context:
- name: secret
apiCall:
urlPath: "/api/v1/namespaces/{{`{{ request.namespace }}`}}/secrets/osc-registry"
jmesPath: "metadata.name || ''"
preconditions:
- key: "{{`{{ secret || '' }}`}}"
operator: NotEquals
value: ""
mutate:
patchStrategicMerge:
spec:
imagePullSecrets:
- name: "{{`{{ secret }}`}}"
22 changes: 22 additions & 0 deletions tests/kyverno-policies/add-image-pull-secret/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: add-image-pull-secret
policies:
- policy.yaml
resources:
- resources.yaml
variables: variables.yaml
results:
- policy: add-image-pull-secret
rule: paas-osc-registry
resources:
- test-paas
patchedResource: paas-mutated.yaml
kind: Pod
result: pass
- policy: add-image-pull-secret
rule: paas-osc-registry
resources:
- test-paas-skip
- test-paas-skip-no-secret
kind: Pod
result: skip
15 changes: 15 additions & 0 deletions tests/kyverno-policies/add-image-pull-secret/paas-mutated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas
namespace: paas
spec:
imagePullSecrets:
- name: osc-registry
containers:
- name: nginx
image: nginx:latest
initContainers:
- name: init
image: busybox
33 changes: 33 additions & 0 deletions tests/kyverno-policies/add-image-pull-secret/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas
namespace: paas
spec:
containers:
- name: nginx
image: nginx:latest
initContainers:
- name: init
image: busybox
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas-skip-no-secret
namespace: paas
spec:
containers:
- name: nginx
image: nginx:latest
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas-skip
namespace: user-test
spec:
containers:
- name: nginx
image: nginx:latest
22 changes: 22 additions & 0 deletions tests/kyverno-policies/add-image-pull-secret/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Values
metadata:
name: values
policies:
- name: add-image-pull-secret
resources:
- name: test-paas
values:
secret: osc-registry
- name: test-paas-skip
values:
secret: osc-registry
- name: test-paas-skip-no-secret
namespaceSelector:
- name: user-test
labels:
foo: bar
- name: paas
labels:
osc.edu/role: paas
osc.edu/service-account: test

0 comments on commit a6ff45f

Please sign in to comment.