From 8341dbf256b23d342226b9c44a2057e4fd775854 Mon Sep 17 00:00:00 2001 From: Prakriti Mandal <98270250+prakrit55@users.noreply.github.com> Date: Thu, 9 Nov 2023 14:46:09 +0530 Subject: [PATCH] feat: introduce configurable TTLSecondsAfterFinished for tasks (#2404) Signed-off-by: Griffin --- .../scripts/.helm-tests/default/result.yaml | 7 +++++ .../.helm-tests/lifecycle-only/result.yaml | 7 +++++ .../lifecycle-with-certs/result.yaml | 7 +++++ .../docs/crd-ref/lifecycle/v1alpha3/_index.md | 1 + .../v1alpha3/keptntaskdefinition_types.go | 4 +++ .../v1alpha3/zz_generated.deepcopy.go | 5 ++++ .../templates/keptntaskdefinition-crd.yaml | 7 +++++ ...fecycle.keptn.sh_keptntaskdefinitions.yaml | 7 +++++ .../lifecycle/keptntask/job_utils.go | 5 ++-- .../lifecycle/keptntask/job_utils_test.go | 7 +++-- .../00-assert.yaml | 15 ++++++++++ .../00-install.yaml | 28 +++++++++++++++++++ 12 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 test/integration/TTLSecondsAfterFinished-in-jobs/00-assert.yaml create mode 100644 test/integration/TTLSecondsAfterFinished-in-jobs/00-install.yaml diff --git a/.github/scripts/.helm-tests/default/result.yaml b/.github/scripts/.helm-tests/default/result.yaml index a5f362c1f0..ec2e824f99 100644 --- a/.github/scripts/.helm-tests/default/result.yaml +++ b/.github/scripts/.helm-tests/default/result.yaml @@ -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. diff --git a/.github/scripts/.helm-tests/lifecycle-only/result.yaml b/.github/scripts/.helm-tests/lifecycle-only/result.yaml index 6a8c18d2a4..d2973db5ba 100644 --- a/.github/scripts/.helm-tests/lifecycle-only/result.yaml +++ b/.github/scripts/.helm-tests/lifecycle-only/result.yaml @@ -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. diff --git a/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml b/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml index c0f1a3ef6d..ba32da0ec4 100644 --- a/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml +++ b/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml @@ -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. diff --git a/docs/content/en/docs/crd-ref/lifecycle/v1alpha3/_index.md b/docs/content/en/docs/crd-ref/lifecycle/v1alpha3/_index.md index acf804f2d2..db9a25f3bf 100644 --- a/docs/content/en/docs/crd-ref/lifecycle/v1alpha3/_index.md +++ b/docs/content/en/docs/crd-ref/lifecycle/v1alpha3/_index.md @@ -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 diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go index e605a080fc..59097944d9 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go @@ -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 { diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go b/lifecycle-operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go index 45124e034e..c9412f06cf 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go @@ -902,6 +902,11 @@ func (in *KeptnTaskDefinitionSpec) DeepCopyInto(out *KeptnTaskDefinitionSpec) { *out = new(AutomountServiceAccountTokenSpec) (*in).DeepCopyInto(*out) } + if in.TTLSecondsAfterFinished != nil { + in, out := &in.TTLSecondsAfterFinished, &out.TTLSecondsAfterFinished + *out = new(int32) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KeptnTaskDefinitionSpec. diff --git a/lifecycle-operator/chart/templates/keptntaskdefinition-crd.yaml b/lifecycle-operator/chart/templates/keptntaskdefinition-crd.yaml index 49b7f900f5..bdace0e233 100644 --- a/lifecycle-operator/chart/templates/keptntaskdefinition-crd.yaml +++ b/lifecycle-operator/chart/templates/keptntaskdefinition-crd.yaml @@ -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. diff --git a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml index 8e4f0083c3..e395b33f6f 100644 --- a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml +++ b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml @@ -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. diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go index 03ac50e101..ec1462d5b3 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go @@ -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) diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils_test.go b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils_test.go index cad7818eea..ab8e8badc5 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils_test.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils_test.go @@ -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) @@ -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) @@ -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, @@ -356,6 +358,7 @@ func makeTaskDefinitionWithServiceAccount(name, namespace, serviceAccountName st AutomountServiceAccountToken: &klcv1alpha3.AutomountServiceAccountTokenSpec{ Type: token, }, + TTLSecondsAfterFinished: ttlSeconds, }, } } diff --git a/test/integration/TTLSecondsAfterFinished-in-jobs/00-assert.yaml b/test/integration/TTLSecondsAfterFinished-in-jobs/00-assert.yaml new file mode 100644 index 0000000000..45f9c1d144 --- /dev/null +++ b/test/integration/TTLSecondsAfterFinished-in-jobs/00-assert.yaml @@ -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 diff --git a/test/integration/TTLSecondsAfterFinished-in-jobs/00-install.yaml b/test/integration/TTLSecondsAfterFinished-in-jobs/00-install.yaml new file mode 100644 index 0000000000..f62e61056f --- /dev/null +++ b/test/integration/TTLSecondsAfterFinished-in-jobs/00-install.yaml @@ -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"