Skip to content

Commit

Permalink
(feat): automatically delete temporary resources
Browse files Browse the repository at this point in the history
Co-authored-by: Troy Connor  <troy0820@users.noreply.github.com>
Signed-off-by: schristoff <28318173+schristoff@users.noreply.github.com>
  • Loading branch information
schristoff and troy0820 committed Aug 17, 2023
1 parent 3d6902a commit 9e05026
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
18 changes: 7 additions & 11 deletions api/v1/agentconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type AgentConfigSpec struct {

// 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
Expand Down Expand Up @@ -331,11 +332,16 @@ func (c AgentConfigSpecAdapter) GetInstallationServiceAccount() string {
return c.original.InstallationServiceAccount
}

// SetRetryAnnotation flags the resource to retry its last operation.
// GetRetryLimit returns the config value of RetryLimit
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 All @@ -350,16 +356,6 @@ func (c AgentConfigSpecAdapter) ToPorterDocument() ([]byte, error) {
return yaml.Marshal(raw)
}

// GetTTLSecondsAfterFinished returns the config value of
// TTLSecondsAfterFinished defaults to 600
func (c AgentConfigSpecAdapter) GetTTLSecondsAfterFinished() *int32 {
if c.original.TTLSecondsAfterFinished == nil {
defaultTTLSeconds := int32(600)
c.original.TTLSecondsAfterFinished = &defaultTTLSeconds
}
return c.original.TTLSecondsAfterFinished
}

// PluginConfigList is the list implementation of the Plugins map.
// The list is sorted based on the plugin names alphabetically.
type PluginsConfigList struct {
Expand Down
22 changes: 22 additions & 0 deletions api/v1/agentconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,28 @@ func TestAgentConfigSpecAdapter_GetRetryLimit(t *testing.T) {
}
}

func TestAgentConfigSpecAdapter_GetTTLSecondsAfterFinished(t *testing.T) {
// var testdataDefault int32 = 600
var testdatauserSubmitted int32 = 700
testCases := []struct {
name string
TTLSecondsAfterFinished *int32
expected *int32
}{
// {name: "default", TTLSecondsAfterFinished: nil, expected: &testdataDefault},
{name: "user submitted", TTLSecondsAfterFinished: &testdatauserSubmitted, expected: &testdatauserSubmitted},
}
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.

6 changes: 6 additions & 0 deletions config/crd/bases/getporter.org_agentconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ spec:
Porter will request when running the Porter Agent. It is used to
determine what the storage class will be for the volume requested
type: string
ttlSecondsAfterFinished:
default: 600
description: TTLSecondsAfterFinished set the time limit of the lifetime
of a Job that has finished execution.
format: int32
type: integer
volumeSize:
description: VolumeSize is the size of the persistent volume that
Porter will request when running the Porter Agent. It is used to
Expand Down
2 changes: 2 additions & 0 deletions controllers/agentaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ func TestAgentActionReconciler_createAgentVolume(t *testing.T) {
}
spec := porterv1.NewAgentConfigSpecAdapter(agentCfg)
pvc, err := controller.createAgentVolume(context.Background(), logr.Discard(), action, spec)

require.NoError(t, err)

// Verify the pvc properties
if test.created {
assert.Equal(t, "porter-hello-", pvc.GenerateName, "incorrect pvc name")
assert.Equal(t, []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, pvc.Spec.AccessModes, "incorrect pvc access modes")
assert.Equal(t, pvc.Spec.Resources.Requests[corev1.ResourceStorage], resource.MustParse("128Mi"))
assert.Equal(t, pvc.OwnerReferences[0].Name, action.Name)
} else {
assert.Equal(t, "existing-", pvc.GenerateName, "incorrect pvc name")
assert.Equal(t, []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany}, pvc.Spec.AccessModes, "incorrect pvc access modes")
Expand Down

0 comments on commit 9e05026

Please sign in to comment.