Skip to content

Commit

Permalink
feat: introduce configurable TTLSecondsAfterFinished for tasks (#2404)
Browse files Browse the repository at this point in the history
Signed-off-by: Griffin <prakritimandal611@gmail.com>
  • Loading branch information
prakrit55 committed Nov 9, 2023
1 parent 4618def commit 8341dbf
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .github/scripts/.helm-tests/default/result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,13 @@ spec:
within this time frame, it will be considered to be failed.
pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
ttlSecondsAfterFinished:
default: 300
description: TTLSecondsAfterFinished controller makes a job eligible
to be cleaned up after it is finished. The timer starts when the
status shows up to be Complete or Failed.
format: int32
type: integer
type: object
status:
description: Status describes the current state of the KeptnTaskDefinition.
Expand Down
7 changes: 7 additions & 0 deletions .github/scripts/.helm-tests/lifecycle-only/result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4184,6 +4184,13 @@ spec:
within this time frame, it will be considered to be failed.
pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
ttlSecondsAfterFinished:
default: 300
description: TTLSecondsAfterFinished controller makes a job eligible
to be cleaned up after it is finished. The timer starts when the
status shows up to be Complete or Failed.
format: int32
type: integer
type: object
status:
description: Status describes the current state of the KeptnTaskDefinition.
Expand Down
7 changes: 7 additions & 0 deletions .github/scripts/.helm-tests/lifecycle-with-certs/result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,13 @@ spec:
within this time frame, it will be considered to be failed.
pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
ttlSecondsAfterFinished:
default: 300
description: TTLSecondsAfterFinished controller makes a job eligible
to be cleaned up after it is finished. The timer starts when the
status shows up to be Complete or Failed.
format: int32
type: integer
type: object
status:
description: Status describes the current state of the KeptnTaskDefinition.
Expand Down
1 change: 1 addition & 0 deletions docs/content/en/docs/crd-ref/lifecycle/v1alpha3/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ _Appears in:_
| `timeout` _[Duration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#duration-v1-meta)_ | Timeout specifies the maximum time to wait for the task to be completed successfully. If the task does not complete successfully within this time frame, it will be considered to be failed. |
| `serviceAccount` _[ServiceAccountSpec](#serviceaccountspec)_ | ServiceAccount specifies the service account to be used in jobs to authenticate with the Kubernetes API and access cluster resources. |
| `automountServiceAccountToken` _[AutomountServiceAccountTokenSpec](#automountserviceaccounttokenspec)_ | AutomountServiceAccountToken allows to enable K8s to assign cluster API credentials to a pod, if set to false the pod will decline the service account |
| `ttlSecondsAfterFinished` _integer_ | TTLSecondsAfterFinished controller makes a job eligible to be cleaned up after it is finished. The timer starts when the status shows up to be Complete or Failed. |


#### KeptnTaskDefinitionStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type KeptnTaskDefinitionSpec struct {
// AutomountServiceAccountToken allows to enable K8s to assign cluster API credentials to a pod, if set to false
// the pod will decline the service account
AutomountServiceAccountToken *AutomountServiceAccountTokenSpec `json:"automountServiceAccountToken,omitempty"`
// TTLSecondsAfterFinished controller makes a job eligible to be cleaned up after it is finished.
// The timer starts when the status shows up to be Complete or Failed.
// +kubebuilder:default:=300
TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty"`
}

type RuntimeSpec struct {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,13 @@ spec:
within this time frame, it will be considered to be failed.
pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
ttlSecondsAfterFinished:
default: 300
description: TTLSecondsAfterFinished controller makes a job eligible
to be cleaned up after it is finished. The timer starts when the
status shows up to be Complete or Failed.
format: int32
type: integer
type: object
status:
description: Status describes the current state of the KeptnTaskDefinition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,13 @@ spec:
within this time frame, it will be considered to be failed.
pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
ttlSecondsAfterFinished:
default: 300
description: TTLSecondsAfterFinished controller makes a job eligible
to be cleaned up after it is finished. The timer starts when the
status shows up to be Complete or Failed.
format: int32
type: integer
type: object
status:
description: Status describes the current state of the KeptnTaskDefinition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func (r *KeptnTaskReconciler) generateJob(ctx context.Context, task *klcv1alpha3
AutomountServiceAccountToken: definition.GetAutomountServiceAccountToken(),
},
},
BackoffLimit: task.Spec.Retries,
ActiveDeadlineSeconds: task.GetActiveDeadlineSeconds(),
BackoffLimit: task.Spec.Retries,
ActiveDeadlineSeconds: task.GetActiveDeadlineSeconds(),
TTLSecondsAfterFinished: definition.Spec.TTLSecondsAfterFinished,
},
}
err := controllerutil.SetControllerReference(task, job, r.Scheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func TestKeptnTaskReconciler_generateJob(t *testing.T) {
svcAccname := "svcAccname"
taskDefinitionName := "my-task-definition"
token := true
var ttlSecondsAfterFinished int32 = 100

taskDefinition := makeTaskDefinitionWithServiceAccount(taskDefinitionName, namespace, svcAccname, &token)
taskDefinition := makeTaskDefinitionWithServiceAccount(taskDefinitionName, namespace, svcAccname, &token, &ttlSecondsAfterFinished)
taskDefinition.Spec.ServiceAccount.Name = svcAccname
fakeClient := testcommon.NewTestClient(taskDefinition)
task := makeTask(taskName, namespace, taskDefinitionName)
Expand Down Expand Up @@ -249,6 +250,7 @@ func TestKeptnTaskReconciler_generateJob(t *testing.T) {
require.NotNil(t, resultingJob.Spec.Template.Spec.Containers)
require.Equal(t, resultingJob.Spec.Template.Spec.ServiceAccountName, svcAccname)
require.Equal(t, resultingJob.Spec.Template.Spec.AutomountServiceAccountToken, &token)
require.Equal(t, resultingJob.Spec.TTLSecondsAfterFinished, &ttlSecondsAfterFinished)
require.Equal(t, map[string]string{
"label1": "label2",
}, resultingJob.Labels)
Expand Down Expand Up @@ -334,7 +336,7 @@ func makeConfigMap(name, namespace string) *v1.ConfigMap {
}
}

func makeTaskDefinitionWithServiceAccount(name, namespace, serviceAccountName string, token *bool) *klcv1alpha3.KeptnTaskDefinition {
func makeTaskDefinitionWithServiceAccount(name, namespace, serviceAccountName string, token *bool, ttlSeconds *int32) *klcv1alpha3.KeptnTaskDefinition {
return &klcv1alpha3.KeptnTaskDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -356,6 +358,7 @@ func makeTaskDefinitionWithServiceAccount(name, namespace, serviceAccountName st
AutomountServiceAccountToken: &klcv1alpha3.AutomountServiceAccountTokenSpec{
Type: token,
},
TTLSecondsAfterFinished: ttlSeconds,
},
}
}
15 changes: 15 additions & 0 deletions test/integration/TTLSecondsAfterFinished-in-jobs/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: batch/v1
kind: Job
metadata:
annotations:
keptn.sh/app: test-app
keptn.sh/task-name: ttlsecondstask-job
keptn.sh/version: 1.0.0
keptn.sh/workload: my-test-workload
spec:
ttlSecondsAfterFinished: 100
status:
conditions:
- type: Complete
status: 'True'
succeeded: 1
28 changes: 28 additions & 0 deletions test/integration/TTLSecondsAfterFinished-in-jobs/00-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: ttlsecondsafterfinished-task
spec:
ttlSecondsAfterFinished: 100
retries: 0
timeout: 30s
container:
command:
name: rancher
image: rancher/cowsay:latest
args:
- 'hello world'
---
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTask
metadata:
name: ttlsecondstask-job
spec:
taskDefinition: ttlsecondsafterfinished-task
context:
appName: "test-app"
appVersion: "1.0.0"
objectType: ""
taskType: ""
workloadName: "my-test-workload"
workloadVersion: "1.0.0"

0 comments on commit 8341dbf

Please sign in to comment.