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

feat: add other policies in CEL expressions - Part 2 #960

Merged
merged 29 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c2cd926
copy enforce-pod-duration
Chandan-DK Mar 30, 2024
4b8d07c
convert enforce-pod-duration to cel
Chandan-DK Mar 30, 2024
2ba3e1c
add CI test for directories starting with e to l in other-cel folder
Chandan-DK Mar 30, 2024
e593e32
copy enforce-readwriteonce-pod
Chandan-DK Mar 31, 2024
c8b329a
convert enforce-readwriteonce-pod to cel
Chandan-DK Mar 31, 2024
c743e24
copy ensure-probes-different
Chandan-DK Mar 31, 2024
3fb487b
convert ensure-probes-different to cel
Chandan-DK Mar 31, 2024
7df7c8f
copy forbid-cpu-limits
Chandan-DK Apr 1, 2024
c0a44b6
convert forbid-cpu-limits to cel
Chandan-DK Apr 1, 2024
bcd7bf3
copy limit-containers-per-pod
Chandan-DK Apr 1, 2024
e3b216e
convert limit-containers-per-pod to cel
Chandan-DK Apr 1, 2024
6e3ea37
copy pdb-maxunavailable
Chandan-DK Apr 2, 2024
bbc2c33
convert pdb-maxunavailable to cel
Chandan-DK Apr 2, 2024
84630b5
keep pdb-maxunavailable in a separate PR
Chandan-DK Apr 3, 2024
18235cd
copy imagepullpolicy-always
Chandan-DK Apr 3, 2024
4b6c0a0
convert imagepullpolicy-always to cel
Chandan-DK Apr 3, 2024
c97304e
copy limit-hostpath-type-pv
Chandan-DK Apr 4, 2024
22509cb
convert limit-hostpath-type-pv to cel
Chandan-DK Apr 4, 2024
fc6223c
copy ensure-readonly-hostpath
Chandan-DK Apr 5, 2024
00115bb
convert ensure-readonly-hostpath to cel
Chandan-DK Apr 5, 2024
d47d9c1
copy ingress-host-match-tls
Chandan-DK Apr 6, 2024
dc33d14
convert ingress-host-match-tls to cel
Chandan-DK Apr 6, 2024
19d396d
rename files for clarity
Chandan-DK Apr 6, 2024
2858638
add new lines at the end of files
Chandan-DK Apr 6, 2024
af2936c
update artifacthub-pkg.yml for enforce-readwriteonce-pod
Chandan-DK Apr 22, 2024
1fabcd7
Merge branch 'main' into other-policies-cel-part-2
MariamFahmy98 Apr 25, 2024
ab2414f
Merge branch 'main' into other-policies-cel-part-2
Chandan-DK May 15, 2024
bafd9ab
Merge branch 'main' into other-policies-cel-part-2
Chandan-DK May 15, 2024
fe0ffb6
Merge branch 'main' into other-policies-cel-part-2
Chandan-DK May 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ jobs:
- ^nginx-ingress$
- ^openshift$
- ^other$/^a
- ^other-cel$/^a
- ^other$/^[b-d]
- ^other-cel$/^[b-d]
- ^other$/^[e-l]
- ^other-cel$/^[e-l]
- ^other$/^[m-q]
- ^other-cel$/^[m-q]
- ^other$/^re[c-q]
- ^other$/^res
- ^other$/^[s-z]
- ^other-cel$/^a
- ^other-cel$/^[b-d]
- ^other-cel$/^[m-q]
- ^pod-security$
- ^pod-security-cel$
- ^psa$
Expand Down
39 changes: 39 additions & 0 deletions other-cel/enforce-pod-duration/.chainsaw-test/chainsaw-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
creationTimestamp: null
name: enforce-pod-duration
spec:
steps:
- name: step-01
try:
- apply:
file: ../enforce-pod-duration.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: pod-lifetime
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: pods-good.yaml
- apply:
expect:
- check:
($error != null): true
file: pods-bad.yaml
- apply:
file: podcontrollers-good.yaml
- apply:
expect:
- check:
($error != null): true
file: podcontrollers-bad.yaml

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: baddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
annotations:
pod.kubernetes.io/lifetime: "8h5m"
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: badcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
metadata:
annotations:
pod.kubernetes.io/lifetime: "8h5m"
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
restartPolicy: OnFailure

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: gooddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: gooddeployment02
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
annotations:
pod.kubernetes.io/lifetime: "8h"
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: goodcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
metadata:
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
restartPolicy: OnFailure
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: goodcronjob02
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
metadata:
annotations:
pod.kubernetes.io/lifetime: "5m"
labels:
app: busybox
spec:
containers:
- image: busybox:1.35
name: busybox
restartPolicy: OnFailure

24 changes: 24 additions & 0 deletions other-cel/enforce-pod-duration/.chainsaw-test/pods-bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
foo: bar
pod.kubernetes.io/lifetime: "8h1m"
name: badpod01
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
annotations:
pod.kubernetes.io/lifetime: "24h"
foo: bar
name: badpod02
spec:
containers:
- image: busybox:1.35
name: busybox

44 changes: 44 additions & 0 deletions other-cel/enforce-pod-duration/.chainsaw-test/pods-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
annotations:
foo: bar
name: goodpod02
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
annotations:
foo: bar
pod.kubernetes.io/lifetime: "5m"
name: goodpod03
spec:
containers:
- image: busybox:1.35
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
annotations:
pod.kubernetes.io/lifetime: "8h"
foo: bar
name: goodpod04
spec:
containers:
- image: busybox:1.35
name: busybox

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: pod-lifetime
status:
ready: true

22 changes: 22 additions & 0 deletions other-cel/enforce-pod-duration/.kyverno-test/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: pod-lifetime
policies:
- ../enforce-pod-duration.yaml
resources:
- resources.yaml
results:
- kind: Pod
policy: pod-lifetime
resources:
- test-lifetime-fail
result: fail
rule: pods-lifetime
- kind: Pod
policy: pod-lifetime
resources:
- test-lifetime-pass
result: pass
rule: pods-lifetime

24 changes: 24 additions & 0 deletions other-cel/enforce-pod-duration/.kyverno-test/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v1
kind: Pod
metadata:
name: test-lifetime-pass
namespace: test
annotations:
pod.kubernetes.io/lifetime: 4h
spec:
containers:
- name: nginx
image: nginx:1.12
---
apiVersion: v1
kind: Pod
metadata:
name: test-lifetime-fail
namespace: test
annotations:
pod.kubernetes.io/lifetime: 24h
spec:
containers:
- name: nginx
image: nginx:1.12

24 changes: 24 additions & 0 deletions other-cel/enforce-pod-duration/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: enforce-pod-duration-cel
version: 1.0.0
displayName: Enforce pod duration in CEL expressions
description: >-
This validation is valuable when annotations are used to define durations, such as to ensure a Pod lifetime annotation does not exceed some site specific max threshold. Pod lifetime annotation can be no greater than 8 hours.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other-cel/enforce-pod-duration/enforce-pod-duration.yaml
```
keywords:
- kyverno
- Sample
- CEL Expressions
readme: |
This validation is valuable when annotations are used to define durations, such as to ensure a Pod lifetime annotation does not exceed some site specific max threshold. Pod lifetime annotation can be no greater than 8 hours.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Sample in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Pod"
digest: b2f1fec7c8b199024c813b1ddb3d52f27f889d082c0c94f4824c499cd6b278bb
createdAt: "2024-03-30T18:18:11Z"

35 changes: 35 additions & 0 deletions other-cel/enforce-pod-duration/enforce-pod-duration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: pod-lifetime
annotations:
policies.kyverno.io/title: Enforce pod duration in CEL expressions
policies.kyverno.io/category: Sample in CEL
policies.kyverno.io/minversion: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/subject: Pod
policies.kyverno.io/description: >-
This validation is valuable when annotations are used to define durations,
such as to ensure a Pod lifetime annotation does not exceed some site specific max threshold.
Pod lifetime annotation can be no greater than 8 hours.
spec:
validationFailureAction: Audit
background: true
rules:
- name: pods-lifetime
match:
any:
- resources:
kinds:
- Pod
validate:
cel:
variables:
- name: hasLifetimeAnnotation
expression: "has(object.metadata.annotations) && 'pod.kubernetes.io/lifetime' in object.metadata.annotations"
- name: lifetimeAnnotationValue
expression: "variables.hasLifetimeAnnotation ? object.metadata.annotations['pod.kubernetes.io/lifetime'] : '0s'"
expressions:
- expression: "!(duration(variables.lifetimeAnnotationValue) > duration('8h'))"
message: "Pod lifetime exceeds limit of 8h"

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: enforce-readwriteonce-pod
policies:
- ../enforce-readwriteonce-pod.yaml
resources:
- resource.yaml
results:
- kind: PersistentVolumeClaim
policy: readwriteonce-pod
resources:
- badpvc
result: fail
rule: readwrite-pvc-single-pod
- kind: PersistentVolumeClaim
policy: readwriteonce-pod
resources:
- goodpvc
result: pass
rule: readwrite-pvc-single-pod

Loading
Loading