-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This policy ensures all Kubernetes Jobs have a reasonable timeout set via activeDeadlineSeconds to prevent indefinitely running jobs and resource consumption issues. Key features: - Enforces activeDeadlineSeconds between 1 hour and 24 hours - Prevents Jobs from running indefinitely - Includes comprehensive Chainsaw tests - Helps with resource management and cost optimization Signed-off-by: Karthik babu Manam <[email protected]>
- Loading branch information
1 parent
ebc3671
commit b96a004
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
job-timeout-enforcer/.chainsaw-test/resources/invalid-job.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: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-no-timeout | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
--- | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-too-short | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
activeDeadlineSeconds: 1800 |
14 changes: 14 additions & 0 deletions
14
job-timeout-enforcer/.chainsaw-test/resources/valid-job.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,14 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: valid-job | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
activeDeadlineSeconds: 3600 |
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: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: test-job-timeout-enforcer | ||
spec: | ||
steps: | ||
- name: 01-apply-policy | ||
try: | ||
- apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
file: job-timeout-enforcer.yaml | ||
|
||
- name: 02-test-valid-job | ||
try: | ||
- file: resources/valid-job.yaml | ||
|
||
- name: 03-test-invalid-job | ||
try: | ||
- file: resources/invalid-job.yaml | ||
expect: | ||
violation: | ||
count: 2 | ||
match: | ||
- message: "Jobs must specify activeDeadlineSeconds between 3600 (1 hour) and 86400 (24 hours)" |
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 @@ | ||
--- | ||
name: job-timeout-enforcer | ||
version: 1.0.0 | ||
displayName: Enforce Job Timeouts | ||
createdAt: "2024-03-20T00:00:00.000Z" | ||
description: >- | ||
Jobs without timeouts can run indefinitely, consuming cluster resources and potentially | ||
indicating stuck workloads. This policy ensures all Jobs have an activeDeadlineSeconds | ||
set with a reasonable timeout value between 1 hour and 24 hours. | ||
install: |- | ||
```sh | ||
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/job-timeout-enforcer/job-timeout-enforcer.yaml | ||
``` | ||
keywords: | ||
- job | ||
- timeout | ||
- resource management | ||
readme: | | ||
# Enforce Job Timeouts | ||
Jobs without timeouts can run indefinitely, consuming cluster resources and potentially | ||
indicating stuck workloads. This policy ensures all Jobs have an activeDeadlineSeconds | ||
set with a reasonable timeout value between 1 hour and 24 hours. | ||
annotations: | ||
kyverno/category: Resource Management | ||
kyverno/severity: medium | ||
kyverno/subject: Job | ||
kyverno/kubernetesVersion: "1.23-1.28" |
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: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-no-timeout | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
--- | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-too-short | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
activeDeadlineSeconds: 1800 |