Skip to content

Commit

Permalink
(feat): automatically delete temporary resources
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff <28318173+schristoff@users.noreply.github.com>
  • Loading branch information
schristoff committed Aug 11, 2023
1 parent 3a42448 commit 3d6902a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
14 changes: 14 additions & 0 deletions api/v1/agentconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ 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.
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 @@ -346,6 +350,16 @@ 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

Check warning on line 360 in api/v1/agentconfig_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1/agentconfig_types.go#L355-L360

Added lines #L355 - L360 were not covered by tests
}

// PluginConfigList is the list implementation of the Plugins map.
// The list is sorted based on the plugin names alphabetically.
type PluginsConfigList struct {
Expand Down
35 changes: 33 additions & 2 deletions controllers/agentaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ func (r *AgentActionReconciler) createAgentVolume(ctx context.Context, log logr.
GenerateName: action.Name + "-",
Namespace: action.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: action.APIVersion,
Kind: action.Kind,
Name: action.Name,
UID: action.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Expand Down Expand Up @@ -302,6 +312,16 @@ func (r *AgentActionReconciler) createConfigSecret(ctx context.Context, log logr
GenerateName: action.Name + "-",
Namespace: action.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: action.APIVersion,
Kind: action.Kind,
Name: action.Name,
UID: action.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Type: corev1.SecretTypeOpaque,
Immutable: ptr.To(true),
Expand Down Expand Up @@ -338,6 +358,16 @@ func (r *AgentActionReconciler) createWorkdirSecret(ctx context.Context, log log
GenerateName: action.Name + "-",
Namespace: action.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: action.APIVersion,
Kind: action.Kind,
Name: action.Name,
UID: action.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Type: corev1.SecretTypeOpaque,
Immutable: ptr.To(true),
Expand Down Expand Up @@ -420,8 +450,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

0 comments on commit 3d6902a

Please sign in to comment.