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 4 #964

Merged
merged 32 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
182c23e
add CI test for other-cel re(c-q) directories
Chandan-DK Apr 9, 2024
3379669
copy require-annotations
Chandan-DK Apr 9, 2024
8906909
add kyverno tests for require-annotations
Chandan-DK Apr 9, 2024
35af3d8
convert require-annotations to cel
Chandan-DK Apr 9, 2024
424424f
copy require-deployments-have-multiple-replicas
Chandan-DK Apr 9, 2024
b426610
add fail kyverno tests for require-deployments-have-multiple-replica
Chandan-DK Apr 9, 2024
fbbe2cc
convert require-deployments-have-multiple-replicas to cel
Chandan-DK Apr 9, 2024
4a1cfb0
copy require-image-checksum
Chandan-DK Apr 10, 2024
4acf9ed
convert require-image-checksum to cel
Chandan-DK Apr 10, 2024
9ac723b
copy require-ingress-https
Chandan-DK Apr 10, 2024
27cf3ab
add kyverno tests for require-ingress-https
Chandan-DK Apr 10, 2024
ed1ac4d
convert require-ingress-https to cel
Chandan-DK Apr 10, 2024
946d40d
Merge branch 'main' into other-policies-cel-part-4
Chandan-DK Apr 10, 2024
2c32b04
copy require-pod-priorityclassname
Chandan-DK Apr 11, 2024
626a29e
convert require-pod-priorityclassname to cel
Chandan-DK Apr 11, 2024
e0381af
copy require-qos-burstable
Chandan-DK Apr 11, 2024
dbed4d6
convert require-qos-burstable to cel
Chandan-DK Apr 11, 2024
6f5e93c
copy require-storageclass
Chandan-DK Apr 11, 2024
d82679f
convert require-storageclass to cel
Chandan-DK Apr 11, 2024
32c6863
rename files for clarity
Chandan-DK Apr 12, 2024
8ee9d76
add a new line at the end of files
Chandan-DK Apr 12, 2024
4faf096
update digest
Chandan-DK Apr 12, 2024
bd9c111
add corresponding kyverno tests in other folder
Chandan-DK Apr 15, 2024
fbd4cb0
use has() in cel expression for readability
Chandan-DK Apr 23, 2024
eed80c6
remove extra whitespace and line
Chandan-DK Apr 23, 2024
59d6541
Merge branch 'main' into other-policies-cel-part-4
MariamFahmy98 May 13, 2024
cab2f56
Merge branch 'main' into other-policies-cel-part-4
Chandan-DK May 15, 2024
ed87d82
resolve conflicts
Chandan-DK May 15, 2024
0e117fa
Merge branch 'main' into other-policies-cel-part-4
Chandan-DK May 15, 2024
ea44c2b
remove duplicate tests
Chandan-DK May 16, 2024
77118ba
Merge branch 'main' into other-policies-cel-part-4
MariamFahmy98 May 22, 2024
665d5e5
Merge branch 'main' into other-policies-cel-part-4
MariamFahmy98 May 30, 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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
- ^other$/^[m-q]
- ^other-cel$/^[m-q]
- ^other$/^re[c-q]
- ^other-cel$/^re[c-q]
- ^other$/^res
- ^other$/^[s-z]
- ^pod-security$
Expand Down
39 changes: 39 additions & 0 deletions other-cel/require-annotations/.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: require-annotations
spec:
steps:
- name: step-01
try:
- apply:
file: ../require-annotations.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-annotations
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: pod-good.yaml
- apply:
expect:
- check:
($error != null): true
file: pod-bad.yaml
- apply:
file: podcontroller-good.yaml
- apply:
expect:
- check:
($error != null): true
file: podcontroller-bad.yaml

20 changes: 20 additions & 0 deletions other-cel/require-annotations/.chainsaw-test/pod-bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
corp.org/department: ""
name: badpod01
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod02
spec:
containers:
- name: busybox
image: busybox:1.35

11 changes: 11 additions & 0 deletions other-cel/require-annotations/.chainsaw-test/pod-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
corp.org/department: "foo"
name: goodpod01
spec:
containers:
- name: busybox
image: busybox:1.35

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: gooddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
annotations:
corp.org/department: "foo"
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: goodcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
metadata:
annotations:
corp.org/department: "foo"
spec:
containers:
- name: busybox
image: busybox:1.35
restartPolicy: OnFailure

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

50 changes: 50 additions & 0 deletions other-cel/require-annotations/.kyverno-test/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: require-annotations
policies:
- ../require-annotations.yaml
resources:
- ../.chainsaw-test/pod-bad.yaml
- ../.chainsaw-test/pod-good.yaml
- ../.chainsaw-test/podcontroller-bad.yaml
- ../.chainsaw-test/podcontroller-good.yaml
results:
- kind: Pod
policy: require-annotations
resources:
- badpod01
- badpod02
result: fail
rule: check-for-annotation
- kind: Pod
policy: require-annotations
resources:
- goodpod01
result: pass
rule: check-for-annotation
- kind: Deployment
policy: require-annotations
resources:
- baddeployment01
result: fail
rule: check-for-annotation
- kind: CronJob
policy: require-annotations
resources:
- badcronjob01
result: fail
rule: check-for-annotation
- kind: Deployment
policy: require-annotations
resources:
- gooddeployment01
result: pass
rule: check-for-annotation
- kind: CronJob
policy: require-annotations
resources:
- goodcronjob01
result: pass
rule: check-for-annotation

24 changes: 24 additions & 0 deletions other-cel/require-annotations/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: require-annotations-cel
version: 1.0.0
displayName: Require Annotations in CEL expressions
description: >-
Define and use annotations that identify semantic attributes of your application or Deployment. A common set of annotations allows tools to work collaboratively, describing objects in a common manner that all tools can understand. The recommended annotations describe applications in a way that can be queried. This policy validates that the annotation `corp.org/department` is specified with some value.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other-cel/require-annotations/require-annotations.yaml
```
keywords:
- kyverno
- Other
- CEL Expressions
readme: |
Define and use annotations that identify semantic attributes of your application or Deployment. A common set of annotations allows tools to work collaboratively, describing objects in a common manner that all tools can understand. The recommended annotations describe applications in a way that can be queried. This policy validates that the annotation `corp.org/department` is specified with some value.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Other in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Pod, Annotation"
digest: dc8408d4a7a929f2f142b174a2ea06148f4dbd65ab16d70870a2213919dadf9d
createdAt: "2024-04-09T15:56:35Z"

34 changes: 34 additions & 0 deletions other-cel/require-annotations/require-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-annotations
annotations:
policies.kyverno.io/title: Require Annotations in CEL expressions
policies.kyverno.io/category: Other in CEL
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Pod, Annotation
kyverno.io/kyverno-version: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/description: >-
Define and use annotations that identify semantic attributes of your application or Deployment.
A common set of annotations allows tools to work collaboratively, describing objects in a common manner that
all tools can understand. The recommended annotations describe applications in a way that can be
queried. This policy validates that the annotation `corp.org/department` is specified with some value.
spec:
validationFailureAction: Audit
background: true
rules:
- name: check-for-annotation
match:
any:
- resources:
kinds:
- Pod
validate:
cel:
expressions:
- expression: >-
has(object.metadata.annotations) &&
'corp.org/department' in object.metadata.annotations && object.metadata.annotations['corp.org/department'] != ''
message: "The annotation `corp.org/department` is required."

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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: require-deployments-have-multiple-replicas
spec:
steps:
- name: step-01
try:
- apply:
file: ../require-deployments-have-multiple-replicas.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: deployment-has-multiple-replicas
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: deploy-good.yaml
- apply:
expect:
- check:
($error != null): true
file: deploy-bad.yaml

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: baddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
- name: busybox02
image: busybox:1.35
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: baddeployment02
spec:
replicas: 0
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
- name: busybox02
image: busybox:1.35
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: baddeployment03
spec:
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
- name: busybox02
image: busybox:1.35

Loading
Loading