diff --git a/controllers/agentaction_controller.go b/controllers/agentaction_controller.go index 9ede9f5a..e69286a2 100644 --- a/controllers/agentaction_controller.go +++ b/controllers/agentaction_controller.go @@ -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 @@ -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 { diff --git a/controllers/agentaction_controller_test.go b/controllers/agentaction_controller_test.go index e7b38d7b..1408c46e 100644 --- a/controllers/agentaction_controller_test.go +++ b/controllers/agentaction_controller_test.go @@ -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) { diff --git a/controllers/agentconfig_controller.go b/controllers/agentconfig_controller.go index 804b4a90..b02595ad 100644 --- a/controllers/agentconfig_controller.go +++ b/controllers/agentconfig_controller.go @@ -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) diff --git a/controllers/agentconfig_controller_test.go b/controllers/agentconfig_controller_test.go index 9390cac3..c0bc4d50 100644 --- a/controllers/agentconfig_controller_test.go +++ b/controllers/agentconfig_controller_test.go @@ -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: diff --git a/controllers/credentialset_controller.go b/controllers/credentialset_controller.go index cf1f3595..8fac3b52 100644 --- a/controllers/credentialset_controller.go +++ b/controllers/credentialset_controller.go @@ -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") diff --git a/controllers/credentialset_controller_test.go b/controllers/credentialset_controller_test.go index c40f9d53..f7b2bd4c 100644 --- a/controllers/credentialset_controller_test.go +++ b/controllers/credentialset_controller_test.go @@ -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) { diff --git a/controllers/installation_controller.go b/controllers/installation_controller.go index 9a846168..9837998f 100644 --- a/controllers/installation_controller.go +++ b/controllers/installation_controller.go @@ -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 ( @@ -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") diff --git a/controllers/installation_controller_test.go b/controllers/installation_controller_test.go index 4ebad15a..9ea9ddb6 100644 --- a/controllers/installation_controller_test.go +++ b/controllers/installation_controller_test.go @@ -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) { diff --git a/controllers/parameterset_controller.go b/controllers/parameterset_controller.go index 5bf87d1e..85f674bf 100644 --- a/controllers/parameterset_controller.go +++ b/controllers/parameterset_controller.go @@ -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") diff --git a/controllers/parameterset_controller_test.go b/controllers/parameterset_controller_test.go index b89e35f8..11ddc3f1 100644 --- a/controllers/parameterset_controller_test.go +++ b/controllers/parameterset_controller_test.go @@ -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) {