Skip to content

Commit

Permalink
feat: update deprecated fields in disallow-latest-tag policy (#1130)
Browse files Browse the repository at this point in the history
Signed-off-by: Indrranil Pawar <indrranil7@gmail.com>
  • Loading branch information
Indrranil committed Dec 29, 2024
1 parent ebc3671 commit 034ae86
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,60 @@ metadata:
name: disallow-latest-tag
spec:
steps:
- name: step-01
try:
- apply:
file: ../disallow-latest-tag.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-latest-tag
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: good-pod.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-latest-fail-first.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-latest-success-first.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-no-tag.yaml
- name: step-01
try:
- apply:
file: ../disallow-latest-tag.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-latest-tag
spec:
rules:
- name: require-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Container images must have a tag specified"
pattern:
spec:
containers:
- image: "!*:latest"
- name: validate-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Images must have a specific tag"
pattern:
spec:
containers:
- image: "!*:latest"
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: good-pod.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-latest-fail-first.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-latest-success-first.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-pod-no-tag.yaml
76 changes: 39 additions & 37 deletions best-practices/disallow-latest-tag/disallow-latest-tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,44 @@ metadata:
a specific version of an application Pod. This policy validates that the image
specifies a tag and that it is not called `latest`.
spec:
validationFailureAction: Audit
background: true
rules:
- name: require-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
message: "An image tag is required."
foreach:
- list: "request.object.spec.containers"
pattern:
image: "*:*"
- list: "request.object.spec.initContainers"
pattern:
image: "*:*"
- list: "request.object.spec.ephemeralContainers"
pattern:
image: "*:*"
- name: validate-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Using a mutable image tag e.g. 'latest' is not allowed."
foreach:
- list: "request.object.spec.containers"
pattern:
image: "!*:latest"
- list: "request.object.spec.initContainers"
pattern:
image: "!*:latest"
- list: "request.object.spec.ephemeralContainers"
pattern:
image: "!*:latest"
- name: require-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
failureAction: Audit
message: "An image tag is required."
foreach:
- list: "request.object.spec.containers"
pattern:
image: "*:*"
- list: "request.object.spec.initContainers"
pattern:
image: "*:*"
- list: "request.object.spec.ephemeralContainers"
pattern:
image: "*:*"

- name: validate-image-tag
match:
any:
- resources:
kinds:
- Pod
validate:
failureAction: Audit
message: "Using a mutable image tag e.g. 'latest' is not allowed."
foreach:
- list: "request.object.spec.containers"
pattern:
image: "!*:latest"
- list: "request.object.spec.initContainers"
pattern:
image: "!*:latest"
- list: "request.object.spec.ephemeralContainers"
pattern:
image: "!*:latest"

0 comments on commit 034ae86

Please sign in to comment.