Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add TTLSecondsAfterFinished with a default of 600 seconds #260

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/v1/agentconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down
24 changes: 24 additions & 0 deletions api/v1/agentconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

5 changes: 3 additions & 2 deletions controllers/agentaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "-",
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")},
}

Expand Down