From f86040feaa9021c7f1b47d70955f99dbb22e47ad Mon Sep 17 00:00:00 2001 From: Troy Connor Date: Mon, 10 Jun 2024 17:42:49 -0400 Subject: [PATCH 1/4] add delete policy to uninstall Signed-off-by: Troy Connor --- controllers/installation_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/installation_controller.go b/controllers/installation_controller.go index db5f9700..94ff4c8a 100644 --- a/controllers/installation_controller.go +++ b/controllers/installation_controller.go @@ -401,7 +401,7 @@ func (r *InstallationReconciler) saveStatus(ctx context.Context, log logr.Logger func (r *InstallationReconciler) shouldUninstall(inst *v1.Installation) bool { // ignore a deleted CRD with no finalizers - return isDeleted(inst) && isFinalizerSet(inst) + return isDeleted(inst) && isFinalizerSet(inst) && inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation] == v1.PorterDeletePolicyDelete } // Sync the retry annotation from the installation to the agent action to trigger another run. From cdd0e4dd57fbb522dc36d9daa983c253e1a1bded Mon Sep 17 00:00:00 2001 From: Troy Connor Date: Thu, 13 Jun 2024 17:08:44 -0400 Subject: [PATCH 2/4] should orphan remove finalizer, stop reconciling. Signed-off-by: Troy Connor --- controllers/installation_controller.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/controllers/installation_controller.go b/controllers/installation_controller.go index 94ff4c8a..5ae2a587 100644 --- a/controllers/installation_controller.go +++ b/controllers/installation_controller.go @@ -133,6 +133,10 @@ func (r *InstallationReconciler) Reconcile(ctx context.Context, req ctrl.Request err = r.uninstallInstallation(ctx, log, inst) log.V(Log4Debug).Info("Reconciliation complete: A porter agent has been dispatched to uninstall the installation.") return ctrl.Result{}, err + } else if r.shouldOrphan(inst) { + log.V(Log4Debug).Info("Reconciliation complete: Your installation is being deleted. Please clean up installation resources") + err = removeFinalizer(ctx, log, r.Client, inst) + return ctrl.Result{}, err } else if isDeleted(inst) { // This is installation without a finalizer that was deleted We remove the // finalizer after we successfully uninstall (or someone is manually cleaning @@ -404,6 +408,10 @@ func (r *InstallationReconciler) shouldUninstall(inst *v1.Installation) bool { return isDeleted(inst) && isFinalizerSet(inst) && inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation] == v1.PorterDeletePolicyDelete } +func (r *InstallationReconciler) shouldOrphan(inst *v1.Installation) bool { + return isDeleted(inst) && isFinalizerSet(inst) && inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation] == v1.PorterDeletePolicyOrphan +} + // Sync the retry annotation from the installation to the agent action to trigger another run. func (r *InstallationReconciler) retry(ctx context.Context, log logr.Logger, inst *v1.Installation, action *v1.AgentAction) error { log.V(Log5Trace).Info("Initializing installation status") From bc765b5a5e4a6a57b9ee352f0221ca151da04400 Mon Sep 17 00:00:00 2001 From: Troy Connor Date: Thu, 20 Jun 2024 09:39:31 -0400 Subject: [PATCH 3/4] add delete policy to test Signed-off-by: Troy Connor --- controllers/installation_controller_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/installation_controller_test.go b/controllers/installation_controller_test.go index 57442064..149c4436 100644 --- a/controllers/installation_controller_test.go +++ b/controllers/installation_controller_test.go @@ -49,6 +49,7 @@ func TestShouldInstall(t *testing.T) { Namespace: "fake-ns", Finalizers: []string{v1.FinalizerName}, DeletionTimestamp: test.delTimeStamp, + Annotations: map[string]string{v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyDelete}, }, } rec := setupInstallationController(inst) From 452914a43c87553f7d8bc9612361af3bb5e81429 Mon Sep 17 00:00:00 2001 From: Troy Connor Date: Thu, 20 Jun 2024 11:04:49 -0400 Subject: [PATCH 4/4] add test for shouldOrphan Signed-off-by: Troy Connor --- controllers/installation_controller_test.go | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/controllers/installation_controller_test.go b/controllers/installation_controller_test.go index 149c4436..8ad20c3f 100644 --- a/controllers/installation_controller_test.go +++ b/controllers/installation_controller_test.go @@ -64,6 +64,38 @@ func TestShouldInstall(t *testing.T) { } } +func TestShouldOrphan(t *testing.T) { + now := metav1.Now() + tests := map[string]struct { + wantTrue bool + delTimeStamp *metav1.Time + }{ + "true": {wantTrue: true, delTimeStamp: &now}, + "false": {wantTrue: false, delTimeStamp: nil}, + } + for name, test := range tests { + t.Run(name, func(t *testing.T) { + inst := &v1.Installation{ + ObjectMeta: metav1.ObjectMeta{ + Name: "fake-name", + Namespace: "fake-ns", + Finalizers: []string{v1.FinalizerName}, + DeletionTimestamp: test.delTimeStamp, + Annotations: map[string]string{v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyOrphan}, + }, + } + rec := setupInstallationController(inst) + isTrue := rec.shouldOrphan(inst) + if test.wantTrue { + assert.True(t, isTrue) + } + if !test.wantTrue { + assert.False(t, isTrue) + } + }) + } +} + func TestUninstallInstallation(t *testing.T) { ctx := context.Background() inst := &v1.Installation{