Skip to content

Commit

Permalink
Add optional Job TTL to keep jobs for a given period of time (#42)
Browse files Browse the repository at this point in the history
- Defaults to 1 hour after Job completion
- Configurable in Sower Config (`TTLSecondsAfterFinished`)
  • Loading branch information
lbeckman314 authored Jun 27, 2024
1 parent 2496cb2 commit 41621f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type SowerConfig struct {
RestartPolicy k8sv1.RestartPolicy `json:"restart_policy"`
ServiceAccountName *string `json:"serviceAccountName"`
ActiveDeadlineSeconds *int64 `json:"activeDeadlineSeconds"`
TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished"`
}

func loadSowerConfigs(config string) []SowerConfig {
Expand Down
8 changes: 8 additions & 0 deletions handlers/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func createK8sJob(currentAction string, inputData string, accessFormat string, a
fmt.Println("input data: ", inputData)
var deadline int64 = 7200
var backoff int32 = 1
var ttl int32 = 3600 // Keep job pods for 1 hour by default
labels := make(map[string]string)
labels["app"] = "sowerjob"
labels["username"] = username
Expand Down Expand Up @@ -209,10 +210,12 @@ func createK8sJob(currentAction string, inputData string, accessFormat string, a
// Optional: Parallelism:,
// Optional: Completions:,
// Optional: ActiveDeadlineSeconds:,
// Optional: TTLSecondsAfterFinished:,
// Optional: Selector:,
// Optional: ManualSelector:,
BackoffLimit: &backoff,
ActiveDeadlineSeconds: &deadline,
TTLSecondsAfterFinished: &ttl,
Template: k8sv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -259,6 +262,11 @@ func createK8sJob(currentAction string, inputData string, accessFormat string, a
batchJob.Spec.ActiveDeadlineSeconds = &deadline
}

if conf.TTLSecondsAfterFinished != nil {
var ttl int32 = *conf.TTLSecondsAfterFinished
batchJob.Spec.TTLSecondsAfterFinished = &ttl
}

newJob, err := jobsClient.Create(context.TODO(), batchJob, metav1.CreateOptions{})
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 41621f9

Please sign in to comment.