Skip to content

Commit

Permalink
add deletion policy annotation on all installations during creation a…
Browse files Browse the repository at this point in the history
…nd update

Signed-off-by: Troy Connor <[email protected]>
  • Loading branch information
troy0820 committed May 21, 2024
1 parent 90ff3e1 commit f67ff3a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api/v1/installation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
)

const (
Prefix = "getporter.org/"
AnnotationRetry = Prefix + "retry"
Prefix = "getporter.org/"
AnnotationRetry = Prefix + "retry"
PorterDeletePolicyAnnotation = "getporter.org/deletion-policy"
PorterDeletePolicyDelete = "Delete"
PorterDeletePolicyOrphan = "Orphan"
)

// We marshal installation spec to yaml when converting to a porter object
Expand Down
31 changes: 31 additions & 0 deletions controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func (r *InstallationReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, err
}

log.V(Log4Debug).Info("Reconciliation complete: A porter agent has been dispatched to apply changes to the installation.")
// NOTE: If this is nil, it will use the default policy of Delete
err = r.applyDeletionPolicy(ctx, log, inst, inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation])
if err != nil {
return ctrl.Result{}, err
}

log.V(Log4Debug).Info("Reconciliation complete: A porter agent has been dispatched to apply changes to the installation.")
if r.PorterGRPCClient != nil {
return r.CheckOrCreateInstallationOutputsCR(ctx, log, inst)
Expand Down Expand Up @@ -404,3 +411,27 @@ func (r *InstallationReconciler) retry(ctx context.Context, log logr.Logger, ins
log.V(Log4Debug).Info("Retried associated porter agent action", "name", "retry", action.Name, retry)
return nil
}

func (r *InstallationReconciler) applyDeletionPolicy(ctx context.Context, log logr.Logger, inst *v1.Installation, policy string) error {
log.V(Log5Trace).Info("updating deletion policy")
annotations := inst.GetAnnotations()
if len(annotations) < 1 {
annotations = map[string]string{}
}

if _, ok := annotations[v1.PorterDeletePolicyAnnotation]; !ok {
annotations[v1.PorterDeletePolicyAnnotation] = v1.PorterDeletePolicyDelete
inst.SetAnnotations(annotations)
return r.Update(ctx, inst)
}

if policy != v1.PorterDeletePolicyOrphan && policy != v1.PorterDeletePolicyDelete {
annotations[v1.PorterDeletePolicyAnnotation] = v1.PorterDeletePolicyDelete
inst.SetAnnotations(annotations)
return r.Update(ctx, inst)
}

annotations[v1.PorterDeletePolicyAnnotation] = policy
inst.SetAnnotations(annotations)
return r.Update(ctx, inst)
}
64 changes: 64 additions & 0 deletions controllers/installation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,67 @@ func TestIsHandled(t *testing.T) {
_, _, err := r.isHandled(ctx, logr.Discard(), inst)
assert.Error(t, err)
}

func TestApplyDeletionPolicyWithNoAnnotation(t *testing.T) {
tests := map[string]struct {
wantPolicy string
policy string
}{
"empty-string": {policy: "", wantPolicy: v1.PorterDeletePolicyDelete},
"policy-orphan": {policy: v1.PorterDeletePolicyOrphan, wantPolicy: v1.PorterDeletePolicyOrphan},
"policy-delete": {policy: v1.PorterDeletePolicyDelete, wantPolicy: v1.PorterDeletePolicyDelete},
}
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}

scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(inst).WithStatusSubresource(inst).Build()
rc := &InstallationReconciler{
Client: fakeClient,
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := rc.applyDeletionPolicy(ctx, logr.Discard(), inst, test.policy)
assert.NoError(t, err)
assert.Equal(t, inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation], test.wantPolicy)
})
}
}

func TestApplyDeletionPolicyWithAnnotation(t *testing.T) {
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
Annotations: map[string]string{
v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyDelete,
},
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}

scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(inst).WithStatusSubresource(inst).Build()
rc := &InstallationReconciler{
Client: fakeClient,
}

err := rc.applyDeletionPolicy(ctx, logr.Discard(), inst, "")
assert.NoError(t, err)
assert.Equal(t, inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation], v1.PorterDeletePolicyDelete)
}

0 comments on commit f67ff3a

Please sign in to comment.