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): automatically delete temporary resources #248

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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"`
schristoff marked this conversation as resolved.
Show resolved Hide resolved

// 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
}
schristoff marked this conversation as resolved.
Show resolved Hide resolved

// 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{
schristoff marked this conversation as resolved.
Show resolved Hide resolved
{
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{
schristoff marked this conversation as resolved.
Show resolved Hide resolved
{
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(),
schristoff marked this conversation as resolved.
Show resolved Hide resolved
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