@@ -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
0 commit comments