diff --git a/api/v1/agentconfig_types.go b/api/v1/agentconfig_types.go index ba37c452..3101bdf3 100644 --- a/api/v1/agentconfig_types.go +++ b/api/v1/agentconfig_types.go @@ -65,6 +65,11 @@ type AgentConfigSpec struct { // +optional VolumeSize string `json:"volumeSize,omitempty" mapstructure:"volumeSize,omitempty"` + // TTLSecondsAfterFinished set the time limit of the lifetime of a Job + // that has finished execution. + // +kubebuilder:default:=600 + TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty" mapstructure:"ttlSecondsAftterFinished,omitempty"` + // PullPolicy specifies when to pull the Porter Agent image. The default // is to use PullAlways when the tag is canary or latest, and PullIfNotPresent // otherwise. @@ -327,11 +332,16 @@ func (c AgentConfigSpecAdapter) GetInstallationServiceAccount() string { return c.original.InstallationServiceAccount } -// SetRetryAnnotation flags the resource to retry its last operation. +// GetRetryLimit flags the resource to retry its last operation. func (c *AgentConfigSpecAdapter) GetRetryLimit() *int32 { return c.original.RetryLimit } +// GetTTLSecondsAfterFinished returns the config value of TTLSecondsAfterFinished +func (c *AgentConfigSpecAdapter) GetTTLSecondsAfterFinished() *int32 { + return c.original.TTLSecondsAfterFinished +} + func (c AgentConfigSpecAdapter) ToPorterDocument() ([]byte, error) { raw := struct { SchemaType string `yaml:"schemaType"` diff --git a/api/v1/agentconfig_types_test.go b/api/v1/agentconfig_types_test.go index a67f8924..5e75f6f7 100644 --- a/api/v1/agentconfig_types_test.go +++ b/api/v1/agentconfig_types_test.go @@ -370,6 +370,30 @@ func TestAgentConfigSpecAdapter_GetRetryLimit(t *testing.T) { } } +func TestAgentConfigSpecAdapter_GetTTLSecondsAfterFinished(t *testing.T) { + var testdataNonZero int32 = 2 + var testdataZero int32 = 0 + testcases := []struct { + name string + TTLSecondsAfterFinished *int32 + expected *int32 + }{ + {name: "non-zero value", TTLSecondsAfterFinished: &testdataNonZero, expected: &testdataNonZero}, + {name: "set to 0", TTLSecondsAfterFinished: &testdataZero, expected: &testdataZero}, + {name: "not defined", TTLSecondsAfterFinished: nil, expected: nil}, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + adapter := NewAgentConfigSpecAdapter(AgentConfigSpec{ + TTLSecondsAfterFinished: tc.TTLSecondsAfterFinished, + }) + result := adapter.GetTTLSecondsAfterFinished() + require.Equal(t, tc.expected, result) + }) + } +} + func TestHashString(t *testing.T) { str := hashString("fake-string") assert.Equal(t, "ab19e45285992b247dd281213f803479", str) diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index d305c869..876232be 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -249,6 +249,11 @@ func (in *AgentConfigList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *AgentConfigSpec) DeepCopyInto(out *AgentConfigSpec) { *out = *in + if in.TTLSecondsAfterFinished != nil { + in, out := &in.TTLSecondsAfterFinished, &out.TTLSecondsAfterFinished + *out = new(int32) + **out = **in + } if in.RetryLimit != nil { in, out := &in.RetryLimit, &out.RetryLimit *out = new(int32) diff --git a/controllers/agentaction_controller.go b/controllers/agentaction_controller.go index bacf152a..9ede9f5a 100644 --- a/controllers/agentaction_controller.go +++ b/controllers/agentaction_controller.go @@ -420,8 +420,9 @@ func (r *AgentActionReconciler) createAgentJob(ctx context.Context, log logr.Log }, }, Spec: batchv1.JobSpec{ - Completions: ptr.To(int32(1)), - BackoffLimit: agentCfg.GetRetryLimit(), + Completions: ptr.To(int32(1)), + BackoffLimit: agentCfg.GetRetryLimit(), + TTLSecondsAfterFinished: agentCfg.GetTTLSecondsAfterFinished(), Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ GenerateName: action.Name + "-", diff --git a/tests/integration/suite_test.go b/tests/integration/suite_test.go index 827704a5..969559f1 100644 --- a/tests/integration/suite_test.go +++ b/tests/integration/suite_test.go @@ -16,7 +16,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" @@ -48,7 +48,7 @@ var _ = BeforeSuite(func(done Done) { By("bootstrapping test environment") testEnv = &envtest.Environment{ - UseExistingCluster: pointer.Bool(true), + UseExistingCluster: ptr.To(true), CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, }