Skip to content

Commit

Permalink
Delete without uninstall (#331)
Browse files Browse the repository at this point in the history
* add delete policy to uninstall

Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
  • Loading branch information
troy0820 authored Jun 25, 2024
1 parent 46f4010 commit 485d89f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -401,7 +405,11 @@ 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
}

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.
Expand Down
33 changes: 33 additions & 0 deletions controllers/installation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -63,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{
Expand Down

0 comments on commit 485d89f

Please sign in to comment.