Skip to content

Commit 134260e

Browse files
committed
Application: refactor finalizer logic
1 parent 1f22b35 commit 134260e

File tree

2 files changed

+37
-86
lines changed

2 files changed

+37
-86
lines changed

controllers/capabilities/application_controller.go

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7979
}
8080
reqLogger.V(1).Info(string(jsonData))
8181
}
82+
83+
// Ignore deleted Applications, this can happen when foregroundDeletion is enabled
84+
// https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#foreground-cascading-deletion
85+
if application.GetDeletionTimestamp() != nil {
86+
if controllerutil.ContainsFinalizer(application, applicationFinalizer) {
87+
err = r.removeApplicationFrom3scale(application)
88+
if err != nil {
89+
r.EventRecorder().Eventf(application, corev1.EventTypeWarning, "Failed to delete application", "%v", err)
90+
return ctrl.Result{}, err
91+
}
92+
93+
if controllerutil.RemoveFinalizer(application, applicationFinalizer) {
94+
if err = r.UpdateResource(application); err != nil {
95+
return ctrl.Result{}, err
96+
}
97+
}
98+
}
99+
return ctrl.Result{}, nil
100+
}
101+
82102
// get Account
83103
accountResource := &capabilitiesv1beta1.DeveloperAccount{}
84104
projectMeta := types.NamespacedName{
@@ -89,25 +109,11 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
89109
err = r.Client().Get(r.Context(), projectMeta, accountResource)
90110
if err != nil {
91111
if apierrors.IsNotFound(err) {
92-
// If the application CR is marked for deletion and the dev account associated doesn't exist, remove the application CR since there is nothing else to do with application.
93-
if application.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(application, applicationFinalizer) {
94-
controllerutil.RemoveFinalizer(application, applicationFinalizer)
95-
err = r.UpdateResource(application)
96-
if err != nil {
97-
return ctrl.Result{}, err
98-
}
99-
100-
return ctrl.Result{}, nil
101-
}
102112
reqLogger.Error(err, "error developer account not found ")
103113
statusReconciler := NewApplicationStatusReconciler(r.BaseReconciler, application, nil, "", err)
104114
statusResult, statusUpdateErr := statusReconciler.Reconcile()
105115
if statusUpdateErr != nil {
106-
if err != nil {
107-
return ctrl.Result{}, fmt.Errorf("failed to reconcile application: %v. Failed to update application status: %w", err, statusUpdateErr)
108-
}
109-
110-
return ctrl.Result{}, fmt.Errorf("failed to update application status: %w", statusUpdateErr)
116+
return ctrl.Result{}, fmt.Errorf("failed to reconcile application: %v. Failed to update application status: %w", err, statusUpdateErr)
111117
}
112118
if statusResult.Requeue {
113119
return statusResult, nil
@@ -127,28 +133,6 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
127133
return ctrl.Result{}, err
128134
}
129135

130-
// Ignore deleted Applications, this can happen when foregroundDeletion is enabled
131-
// https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#foreground-cascading-deletion
132-
if application.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(application, applicationFinalizer) {
133-
err = r.removeApplicationFrom3scale(application, req, *threescaleAPIClient)
134-
if err != nil {
135-
r.EventRecorder().Eventf(application, corev1.EventTypeWarning, "Failed to delete application", "%v", err)
136-
return ctrl.Result{}, err
137-
}
138-
139-
controllerutil.RemoveFinalizer(application, applicationFinalizer)
140-
err = r.UpdateResource(application)
141-
if err != nil {
142-
return ctrl.Result{}, err
143-
}
144-
145-
return ctrl.Result{}, nil
146-
}
147-
148-
if application.GetDeletionTimestamp() != nil {
149-
return ctrl.Result{}, nil
150-
}
151-
152136
metadataUpdated, err := r.reconcileMetadata(application, accountResource)
153137
if err != nil {
154138
r.EventRecorder().Eventf(application, corev1.EventTypeWarning, "Failed to update ownerReferences in application", "%v", err)
@@ -268,21 +252,23 @@ func (r *ApplicationReconciler) applicationReconciler(applicationResource *capab
268252
return statusReconciler, err
269253
}
270254

271-
func (r *ApplicationReconciler) removeApplicationFrom3scale(application *capabilitiesv1beta1.Application, req ctrl.Request, threescaleAPIClient threescaleapi.ThreeScaleClient) error {
255+
func (r *ApplicationReconciler) removeApplicationFrom3scale(application *capabilitiesv1beta1.Application) error {
272256
logger := r.Logger().WithValues("application", client.ObjectKey{Name: application.Name, Namespace: application.Namespace})
273257

274258
// get Account
275259
account := &capabilitiesv1beta1.DeveloperAccount{}
276260
projectMeta := types.NamespacedName{
277261
Name: application.Spec.AccountCR.Name,
278-
Namespace: req.Namespace,
262+
Namespace: application.Namespace,
279263
}
280264

281265
err := r.Client().Get(r.Context(), projectMeta, account)
282266
if err != nil {
283267
if apierrors.IsNotFound(err) {
284268
logger.Info("Account resource not found. Ignoring since object must have been deleted")
269+
return nil
285270
}
271+
return err
286272
}
287273

288274
// Attempt to remove application only if application.Status.ID is present
@@ -296,6 +282,18 @@ func (r *ApplicationReconciler) removeApplicationFrom3scale(application *capabil
296282
return fmt.Errorf("could not remove application because ID is missing in the satus of developer account %s", account.Name)
297283
}
298284

285+
// get providerAccountRef from account
286+
providerAccount, err := controllerhelper.LookupProviderAccount(r.Client(), account.Namespace, account.Spec.ProviderAccountRef, r.Logger())
287+
if err != nil {
288+
return err
289+
}
290+
291+
insecureSkipVerify := controllerhelper.GetInsecureSkipVerifyAnnotation(application.GetAnnotations())
292+
threescaleAPIClient, err := controllerhelper.PortaClient(providerAccount, insecureSkipVerify)
293+
if err != nil {
294+
return err
295+
}
296+
299297
err = threescaleAPIClient.DeleteApplication(*account.Status.ID, *application.Status.ID)
300298
if err != nil && !threescaleapi.IsNotFound(err) {
301299
return err

controllers/capabilities/application_controller_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -258,50 +258,3 @@ func TestApplicationReconciler_applicationReconciler(t *testing.T) {
258258
})
259259
}
260260
}
261-
262-
func TestApplicationReconciler_removeApplicationFrom3scale(t *testing.T) {
263-
// admin portal
264-
ap, _ := client.NewAdminPortalFromStr("https://3scale-admin.test.3scale.net")
265-
type fields struct {
266-
BaseReconciler *reconcilers.BaseReconciler
267-
}
268-
type args struct {
269-
application *capabilitiesv1beta1.Application
270-
req controllerruntime.Request
271-
threescaleAPIClient *client.ThreeScaleClient
272-
}
273-
tests := []struct {
274-
name string
275-
fields fields
276-
args args
277-
wantErr bool
278-
}{
279-
{
280-
name: "Delete Application successfully",
281-
fields: fields{
282-
BaseReconciler: getBaseReconciler(getApplicationCR(), getProviderAccount(), getApiManger(), getApplicationProductList(), getApplicationDeveloperAccount(), getProviderAccountRefSecret()),
283-
},
284-
args: args{
285-
application: getApplicationDeleteCR(),
286-
req: controllerruntime.Request{
287-
NamespacedName: types.NamespacedName{
288-
Name: "test",
289-
Namespace: "test",
290-
},
291-
},
292-
threescaleAPIClient: client.NewThreeScale(ap, "test", mockHttpClientApplication(getApplicationPlanListByProductJson(), getApplicationJson("live"))),
293-
},
294-
wantErr: false,
295-
},
296-
}
297-
for _, tt := range tests {
298-
t.Run(tt.name, func(t *testing.T) {
299-
r := &ApplicationReconciler{
300-
BaseReconciler: tt.fields.BaseReconciler,
301-
}
302-
if err := r.removeApplicationFrom3scale(tt.args.application, tt.args.req, *tt.args.threescaleAPIClient); (err != nil) != tt.wantErr {
303-
t.Errorf("removeApplicationFrom3scale() error = %v, wantErr %v", err, tt.wantErr)
304-
}
305-
})
306-
}
307-
}

0 commit comments

Comments
 (0)