Skip to content

Commit

Permalink
handle delete
Browse files Browse the repository at this point in the history
Signed-off-by: Troy Connor <[email protected]>
  • Loading branch information
troy0820 committed Sep 6, 2023
1 parent 433e481 commit f0678bb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 80 deletions.
10 changes: 10 additions & 0 deletions controllers/agentaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// +kubebuilder:rbac:groups=getporter.org,resources=agentconfigs,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -65,6 +66,15 @@ func (r *AgentActionReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{Requeue: false}, err
}

if action.DeletionTimestamp != nil {
if controllerutil.ContainsFinalizer(action, porterv1.FinalizerName) {
controllerutil.RemoveFinalizer(action, porterv1.FinalizerName)
if err := r.Update(ctx, action); err != nil {
return ctrl.Result{}, err
}
}
}

log = log.WithValues("resourceVersion", action.ResourceVersion, "generation", action.Generation, "observedGeneration", action.Status.ObservedGeneration)

if action.Generation != action.Status.ObservedGeneration {
Expand Down
4 changes: 1 addition & 3 deletions controllers/agentaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,8 @@ func TestAgentActionReconciler_Reconcile(t *testing.T) {
assert.Empty(t, action.Status.Conditions, "Conditions should have been reset")

// Delete the action
controller.Delete(ctx, &action)
assert.NoError(t, controller.Delete(ctx, &action))

// Verify that reconcile doesn't error out after it's deleted
triggerReconcile()
}

func TestAgentActionReconciler_createAgentVolume(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions controllers/agentconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func (r *AgentConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
return ctrl.Result{}, err
}

if agentCfgData.DeletionTimestamp != nil {
if controllerutil.ContainsFinalizer(agentCfgData, porterv1.FinalizerName) {
controllerutil.RemoveFinalizer(agentCfgData, porterv1.FinalizerName)
if err := r.Update(ctx, agentCfgData); err != nil {
return ctrl.Result{}, err
}
}
}

agentCfg := porterv1.NewAgentConfigAdapter(*agentCfgData)

log = log.WithValues("resourceVersion", agentCfg.ResourceVersion, "generation", agentCfg.Generation, "observedGeneration", agentCfg.Status.ObservedGeneration, "status", agentCfg.Status.Ready)
Expand Down
16 changes: 1 addition & 15 deletions controllers/agentconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,7 @@ func TestAgentConfigReconciler_Reconcile(t *testing.T) {
agentCfgData.Spec.PluginConfigFile = &porterv1.PluginFileSpec{Plugins: map[string]porterv1.Plugin{"kubernetes": {}}}
agentCfgData.DeletionTimestamp = &now
require.NoError(t, controller.Delete(ctx, &agentCfgData))
triggerReconcile()

// remove the agent config from the pvc's owner reference list
triggerReconcile()
// Verify that pvc and pv no longer has the agent config in their owner reference list
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: agentCfg.Namespace, Name: agentCfg.GetPluginsPVCName()}, renamedPVC))

// this trigger will then remove the agent config's finalizer
triggerReconcile()
// Verify that the agent config was removed
err = controller.Get(ctx, client.ObjectKeyFromObject(&agentCfg.AgentConfig), &agentCfg.AgentConfig)
require.True(t, apierrors.IsNotFound(err), "expected the agent config was deleted")

// Verify that reconcile doesn't error out after it's deleted
triggerReconcile()
// end of lifecycle, tracker doesn't keep object showing it's been deleted
}

// This tests the following AgentConfig update scenarios:
Expand Down
8 changes: 8 additions & 0 deletions controllers/credentialset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (r *CredentialSetReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
return ctrl.Result{}, err
}
if cs.DeletionTimestamp != nil {
if controllerutil.ContainsFinalizer(cs, porterv1.FinalizerName) {
controllerutil.RemoveFinalizer(cs, porterv1.FinalizerName)
if err := r.Update(ctx, cs); err != nil {
return ctrl.Result{}, err
}
}
}

log = log.WithValues("resourceVersion", cs.ResourceVersion, "generation", cs.Generation)
log.V(Log5Trace).Info("Reconciling credential set")
Expand Down
21 changes: 0 additions & 21 deletions controllers/credentialset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,6 @@ func TestCredentialSetReconiler_Reconcile(t *testing.T) {
cs.Generation = 3
cs.DeletionTimestamp = &now
require.NoError(t, controller.Delete(ctx, &cs))

triggerReconcile()

// Verify that an action was created to delete it
require.NotNil(t, cs.Status.Action, "expected Action to be set")
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: cs.Namespace, Name: cs.Status.Action.Name}, &action))
assert.Equal(t, "2", action.Labels[porterv1.LabelResourceGeneration], "The wrong action is set on the status")

// Complete the delete action
action.Status.Phase = porterv1.PhaseSucceeded
action.Status.Conditions = []metav1.Condition{{Type: string(porterv1.ConditionComplete), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))

triggerReconcile()

// Verify that the credential set was removed
err := controller.Get(ctx, client.ObjectKeyFromObject(&cs), &cs)
require.True(t, apierrors.IsNotFound(err), "expected the credential set was deleted")

// Verify that the reconcile doesn't error out after its deleted
triggerReconcile()
}

func TestCredentialSetReconciler_createAgentAction(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

const (
Expand Down Expand Up @@ -64,6 +65,15 @@ func (r *InstallationReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, err
}

if inst.DeletionTimestamp != nil {
if controllerutil.ContainsFinalizer(inst, porterv1.FinalizerName) {
controllerutil.RemoveFinalizer(inst, porterv1.FinalizerName)
if err := r.Update(ctx, inst); err != nil {
return ctrl.Result{}, err
}
}
}

log = log.WithValues("resourceVersion", inst.ResourceVersion, "generation", inst.Generation, "observedGeneration", inst.Status.ObservedGeneration)
log.V(Log5Trace).Info("Reconciling installation")

Expand Down
22 changes: 1 addition & 21 deletions controllers/installation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,7 @@ func TestInstallationReconciler_Reconcile(t *testing.T) {
inst.Generation = 3
inst.DeletionTimestamp = &now
require.NoError(t, controller.Delete(ctx, &inst))

triggerReconcile()

// Verify that an action was created to uninstall it
require.NotNil(t, inst.Status.Action, "expected Action to be set")
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: inst.Namespace, Name: inst.Status.Action.Name}, &action))
assert.Equal(t, "2", action.Labels[porterv1.LabelResourceGeneration], "The wrong action is set on the status")

// Complete the uninstall action
action.Status.Phase = porterv1.PhaseSucceeded
action.Status.Conditions = []metav1.Condition{{Type: string(porterv1.ConditionComplete), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))

triggerReconcile()

// Verify that the installation was removed
err := controller.Get(ctx, client.ObjectKeyFromObject(&inst), &inst)
require.True(t, apierrors.IsNotFound(err), "expected the installation was deleted")

// Verify that reconcile doesn't error out after it's deleted
triggerReconcile()
//end of the lifecycle
}

func TestInstallationReconciler_createAgentAction(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions controllers/parameterset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func (r *ParameterSetReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
return ctrl.Result{}, err
}
if ps.DeletionTimestamp != nil {
if controllerutil.ContainsFinalizer(ps, porterv1.FinalizerName) {
controllerutil.RemoveFinalizer(ps, porterv1.FinalizerName)
if err := r.Update(ctx, ps); err != nil {
return ctrl.Result{}, err
}
}
}

log = log.WithValues("resourceVersion", ps.ResourceVersion, "generation", ps.Generation)
log.V(Log5Trace).Info("Reconciling parameter set")
Expand Down
21 changes: 1 addition & 20 deletions controllers/parameterset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,27 +166,8 @@ func TestParameterSetReconiler_Reconcile(t *testing.T) {
ps.Generation = 3
ps.DeletionTimestamp = &now
require.NoError(t, controller.Delete(ctx, &ps))
//end of the lifecycle

triggerReconcile()

// Verify that an action was created to delete it
require.NotNil(t, ps.Status.Action, "expected Action to be set")
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: ps.Namespace, Name: ps.Status.Action.Name}, &action))
assert.Equal(t, "2", action.Labels[porterv1.LabelResourceGeneration], "The wrong resource generation is set for the agent action")

// Complete the delete action
action.Status.Phase = porterv1.PhaseSucceeded
action.Status.Conditions = []metav1.Condition{{Type: string(porterv1.ConditionComplete), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))

triggerReconcile()

// Verify that the parameter set was removed
err := controller.Get(ctx, client.ObjectKeyFromObject(&ps), &ps)
require.True(t, apierrors.IsNotFound(err), "expected the parameter set was deleted")

// Verify that the reconcile doesn't error out after its deleted
triggerReconcile()
}

func TestParameterSetReconciler_createAgentAction(t *testing.T) {
Expand Down

0 comments on commit f0678bb

Please sign in to comment.