@@ -60,8 +60,6 @@ const (
6060// +kubebuilder:rbac:groups=capabilities.3scale.net,resources=applicationauths/status,verbs=get;update;patch
6161
6262func (r * ApplicationAuthReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
63- _ = context .Background ()
64- // _ = r.Log.WithValues("applicationauth", req.NamespacedName)
6563 reqLogger := r .Logger ().WithValues ("applicationauth" , req .NamespacedName )
6664 reqLogger .Info ("Reconcile Application Authentication" , "Operator version" , version .Version )
6765
@@ -84,74 +82,43 @@ func (r *ApplicationAuthReconciler) Reconcile(ctx context.Context, req ctrl.Requ
8482 }
8583 reqLogger .V (1 ).Info (string (jsonData ))
8684 }
87- // get the application
88- application := & capabilitiesv1beta1.Application {}
8985
9086 // Retrieve application CR, on failed retrieval update status and requeue
87+ application := & capabilitiesv1beta1.Application {}
9188 err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .ApplicationCRName , Namespace : applicationAuth .Namespace }, application )
9289 if err != nil {
9390 // If the product CR is not found, update status and requeue
9491 if errors .IsNotFound (err ) {
95- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
9692 reqLogger .Info ("Application CR not found. Ignoring since object must have been deleted" )
97- statusResult , statusErr := statusReconciler .Reconcile ()
98- // Reconcile status first as the reconcilerError might need to be updated to the status section of the CR before requeueing
99- if statusErr != nil {
100- return ctrl.Result {}, statusErr
101- }
102- if statusResult .Requeue {
103- reqLogger .Info ("Reconciling status not finished. Requeueing." )
104- return statusResult , nil
105- }
93+ return r .reconcileStatus (applicationAuth , err , reqLogger )
10694 }
10795
10896 // If API call error, return err
10997 return ctrl.Result {}, err
11098 }
11199
112- // get the product
100+ // Retrieve DeveloperAccount CR, on failed retrieval update status and requeue
113101 developerAccount := & capabilitiesv1beta1.DeveloperAccount {}
114-
115- // Retrieve product CR, on failed retrieval update status and requeue
116102 err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .AccountCR .Name , Namespace : applicationAuth .Namespace }, developerAccount )
117103 if err != nil {
118104 // If the product CR is not found, update status and requeue
119105 if errors .IsNotFound (err ) {
120- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
121106 reqLogger .Info ("DeveloperAccount CR not found. Ignoring since object must have been deleted" )
122- statusResult , statusErr := statusReconciler .Reconcile ()
123- // Reconcile status first as the reconcilerError might need to be updated to the status section of the CR before requeueing
124- if statusErr != nil {
125- return ctrl.Result {}, statusErr
126- }
127- if statusResult .Requeue {
128- reqLogger .Info ("Reconciling status not finished. Requeueing." )
129- return statusResult , nil
130- }
107+ return r .reconcileStatus (applicationAuth , err , reqLogger )
131108 }
132109
133110 // If API call error, return err
134111 return ctrl.Result {}, err
135112 }
136- // get the application
137- product := & capabilitiesv1beta1.Product {}
138113
139- // Retrieve application CR, on failed retrieval update status and requeue
114+ // Retrieve Product CR, on failed retrieval update status and requeue
115+ product := & capabilitiesv1beta1.Product {}
140116 err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .ProductCR .Name , Namespace : applicationAuth .Namespace }, product )
141117 if err != nil {
142118 // If the product CR is not found, update status and requeue
143119 if errors .IsNotFound (err ) {
144- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
145- reqLogger .Info ("Application CR not found. Ignoring since object must have been deleted" )
146- statusResult , statusErr := statusReconciler .Reconcile ()
147- // Reconcile status first as the reconcilerError might need to be updated to the status section of the CR before requeueing
148- if statusErr != nil {
149- return ctrl.Result {}, statusErr
150- }
151- if statusResult .Requeue {
152- reqLogger .Info ("Reconciling status not finished. Requeueing." )
153- return statusResult , nil
154- }
120+ reqLogger .Info ("Product CR not found. Ignoring since object must have been deleted" )
121+ return r .reconcileStatus (applicationAuth , err , reqLogger )
155122 }
156123
157124 // If API call error, return err
@@ -171,52 +138,29 @@ func (r *ApplicationAuthReconciler) Reconcile(ctx context.Context, req ctrl.Requ
171138 return ctrl.Result {}, err
172139 }
173140
174- // get the authSecret
175- authSecretObj := & corev1.Secret {}
176-
177141 // Retrieve auth secret, on failed retrieval update status and requeue
142+ authSecretObj := & corev1.Secret {}
178143 err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .AuthSecretRef .Name , Namespace : applicationAuth .Namespace }, authSecretObj )
179144 if err != nil {
180145 // If the product CR is not found, update status and requeue
181146 if errors .IsNotFound (err ) {
182- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
183147 reqLogger .Info ("ApplicationAuth secret not found. Ignoring since object must have been deleted" )
184- statusResult , statusErr := statusReconciler .Reconcile ()
185- // Reconcile status first as the reconcilerError might need to be updated to the status section of the CR before requeueing
186- if statusErr != nil {
187- return ctrl.Result {}, statusErr
188- }
189- if statusResult .Requeue {
190- reqLogger .Info ("Reconciling status not finished. Requeueing." )
191- return statusResult , nil
192- }
148+ return r .reconcileStatus (applicationAuth , err , reqLogger )
193149 }
194150 return ctrl.Result {}, err
195151 }
152+
196153 // populate authSecret struct
197154 authSecret := authSecretReferenceSource (r .Client (), applicationAuth .Namespace , applicationAuth .Spec .AuthSecretRef , reqLogger )
198-
199155 if ! applicationAuth .Status .Conditions .IsTrueFor (capabilitiesv1beta1 .ApplicationAuthReadyConditionType ) {
200- statusReconciler , reconcileErr := r .applicationAuthReconciler (applicationAuth , developerAccount , application , product , * authSecret , threescaleAPIClient )
201- if statusReconciler != nil {
202- statusResult , statusErr := statusReconciler .Reconcile ()
203-
204- if statusErr != nil {
205- return ctrl.Result {}, statusErr
206- }
207- if statusResult .Requeue {
208- reqLogger .Info ("Reconciling status not finished. Requeueing." )
209- return statusResult , nil
210- }
211- // If reconcile error but no status update required, requeue.
212- if reconcileErr != nil {
213- return helper .ReconcileErrorHandler (reconcileErr , reqLogger ), nil
214- }
156+ err := r .applicationAuthReconciler (applicationAuth , developerAccount , application , product , * authSecret , threescaleAPIClient )
157+ if err != nil {
158+ return r .reconcileStatus (applicationAuth , err , reqLogger )
215159 }
216160 }
217161 // final return
218162 reqLogger .Info ("Successfully reconciled" )
219- return ctrl. Result {} , nil
163+ return r . reconcileStatus ( applicationAuth , nil , reqLogger )
220164}
221165
222166func (r * ApplicationAuthReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
@@ -232,7 +176,7 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
232176 product * capabilitiesv1beta1.Product ,
233177 authSecret AuthSecret ,
234178 threescaleClient * threescaleapi.ThreeScaleClient ,
235- ) ( * ApplicationAuthStatusReconciler , error ) {
179+ ) error {
236180 // generate sha base of timestamp
237181 timestamp := time .Now ().Unix ()
238182 // Write the timestamp string and encode to hash
@@ -253,8 +197,7 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
253197 if application .Status .ID != nil {
254198 _ , err := threescaleClient .UpdateApplication (* developerAccount .Status .ID , * application .Status .ID , params )
255199 if err != nil {
256- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
257- return statusReconciler , err
200+ return err
258201 }
259202 }
260203 }
@@ -264,8 +207,7 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
264207 if application .Status .ID != nil {
265208 foundApplication , err := threescaleClient .CreateApplicationKey (* developerAccount .Status .ID , * application .Status .ID , authSecret .ApplicationKey )
266209 if err != nil {
267- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
268- return statusReconciler , err
210+ return err
269211 }
270212
271213 authSecret .ApplicationID = foundApplication .ApplicationId
@@ -276,15 +218,13 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
276218 if application .Status .ID != nil {
277219 foundApplication , err := threescaleClient .CreateApplicationRandomKey (* developerAccount .Status .ID , * application .Status .ID )
278220 if err != nil {
279- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
280- return statusReconciler , err
221+ return err
281222 }
282223 authSecret .ApplicationID = foundApplication .ApplicationId
283224 var foundApplicationKeys []threescaleapi.ApplicationKey
284225 foundApplicationKeys , err = threescaleClient .ApplicationKeys (* developerAccount .Status .ID , * application .Status .ID )
285226 if err != nil {
286- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
287- return statusReconciler , err
227+ return err
288228 }
289229 lastKey := len (foundApplicationKeys ) - 1
290230 authSecret .ApplicationKey = fmt .Sprint (foundApplicationKeys [lastKey ].Value )
@@ -300,8 +240,7 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
300240 if err != nil {
301241 // Handle errors gracefully, e.g., log and return or retry
302242 r .Logger ().Error (err , "Failed to get existing ApplicationAuthSecret" )
303- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
304- return statusReconciler , err
243+ return err
305244 }
306245 newData := ApplicationAuthSecret .Data
307246 newValues := map [string ][]byte {
@@ -317,11 +256,10 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
317256 err = r .Client ().Update (r .Context (), ApplicationAuthSecret )
318257 if err != nil {
319258 r .Logger ().Error (err , "Failed to update ApplicationAuthSecret" )
320- statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , applicationAuth , err )
321- return statusReconciler , err
259+ return err
322260 }
323- statusReconciler := NewApplicationAuthStatusReconciler ( r . BaseReconciler , applicationAuth , nil )
324- return statusReconciler , nil
261+
262+ return nil
325263}
326264
327265func authSecretReferenceSource (cl client.Client , ns string , authSectretRef * corev1.LocalObjectReference , logger logr.Logger ) * AuthSecret {
@@ -342,3 +280,24 @@ func authSecretReferenceSource(cl client.Client, ns string, authSectretRef *core
342280
343281 return nil
344282}
283+
284+ func (r * ApplicationAuthReconciler ) reconcileStatus (resource * capabilitiesv1beta1.ApplicationAuth , err error , logger logr.Logger ) (ctrl.Result , error ) {
285+ statusReconciler := NewApplicationAuthStatusReconciler (r .BaseReconciler , resource , err )
286+ statusResult , statusErr := statusReconciler .Reconcile ()
287+
288+ if err != nil {
289+ return ctrl.Result {}, err
290+ }
291+
292+ // Reconcile status first as the reconcilerError might need to be updated to the status section of the CR before requeueing
293+ if statusErr != nil {
294+ return ctrl.Result {}, statusErr
295+ }
296+
297+ if statusResult .Requeue {
298+ logger .Info ("Reconciling status not finished. Requeueing." )
299+ return statusResult , nil
300+ }
301+
302+ return ctrl.Result {}, nil
303+ }
0 commit comments