-
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. Signed-off-by: Karthik babu Manam <[email protected]>
- Loading branch information
1 parent
ebc3671
commit 775f2ff
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
name: job-timeout-enforcer | ||
version: 1.0.0 | ||
displayName: Enforce Job Timeouts | ||
createdAt: "2024-03-19T00: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. This helps prevent | ||
resource leaks and identifies stuck Jobs early. | ||
install: |- | ||
```shell | ||
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/resource-lifecycle/job-timeout-enforcer/job-timeout-enforcer.yaml | ||
``` | ||
keywords: | ||
- kyverno | ||
- resource lifecycle | ||
- job | ||
- timeout | ||
readme: | | ||
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. This helps prevent | ||
resource leaks and identifies stuck Jobs early. | ||
Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/ | ||
annotations: | ||
kyverno/category: "Resource Lifecycle" | ||
kyverno/kubernetesVersion: "1.23-1.28" | ||
kyverno/subject: "Job" | ||
digest: b247e22ba7353f3e2fcdc09cdbf158fcb5bb92bd897cefb006b781593a9fd337 # sha256sum job-timeout-enforcer.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,32 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: job-timeout-enforcer | ||
annotations: | ||
policies.kyverno.io/title: Enforce Job Timeouts | ||
policies.kyverno.io/category: Resource Lifecycle | ||
policies.kyverno.io/severity: medium | ||
policies.kyverno.io/subject: Job | ||
policies.kyverno.io/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. This helps prevent | ||
resource leaks and identifies stuck Jobs early. | ||
kyverno.io/kyverno-version: 1.6.0 | ||
policies.kyverno.io/minversion: 1.6.0 | ||
kyverno.io/kubernetes-version: "1.23-1.28" | ||
spec: | ||
validationFailureAction: Audit | ||
background: true | ||
rules: | ||
- name: validate-job-timeout | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- Job | ||
validate: | ||
message: "Jobs must specify activeDeadlineSeconds between 3600 (1 hour) and 86400 (24 hours)" | ||
pattern: | ||
spec: | ||
activeDeadlineSeconds: ">= 3600 && <= 86400" |
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,45 @@ | ||
# Valid job | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: valid-job | ||
spec: | ||
activeDeadlineSeconds: 7200 # 2 hours | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
|
||
--- | ||
# Invalid job (no timeout) | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-no-timeout | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never | ||
|
||
--- | ||
# Invalid job (timeout too long) | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: invalid-job-long-timeout | ||
spec: | ||
activeDeadlineSeconds: 100000 | ||
template: | ||
spec: | ||
containers: | ||
- name: pi | ||
image: perl:5.34.0 | ||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | ||
restartPolicy: Never |