@@ -35,6 +35,7 @@ import (
3535 corev1 "k8s.io/api/core/v1"
3636 "k8s.io/apimachinery/pkg/api/errors"
3737 "k8s.io/apimachinery/pkg/types"
38+ "k8s.io/apimachinery/pkg/util/validation/field"
3839 ctrl "sigs.k8s.io/controller-runtime"
3940 "sigs.k8s.io/controller-runtime/pkg/client"
4041)
@@ -83,77 +84,83 @@ func (r *ApplicationAuthReconciler) Reconcile(ctx context.Context, req ctrl.Requ
8384 reqLogger .V (1 ).Info (string (jsonData ))
8485 }
8586
86- // Retrieve application CR, on failed retrieval update status and requeue
87- application := & capabilitiesv1beta1.Application {}
88- err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .ApplicationCRName , Namespace : applicationAuth .Namespace }, application )
89- if err != nil {
90- // If the product CR is not found, update status and requeue
91- if errors .IsNotFound (err ) {
92- reqLogger .Info ("Application CR not found. Ignoring since object must have been deleted" )
93- return r .reconcileStatus (applicationAuth , err , reqLogger )
94- }
87+ if ! applicationAuth .Status .Conditions .IsTrueFor (capabilitiesv1beta1 .ApplicationAuthReadyConditionType ) {
88+ // Retrieve application CR, on failed retrieval update status and requeue
89+ application := & capabilitiesv1beta1.Application {}
90+ err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .ApplicationCRName , Namespace : applicationAuth .Namespace }, application )
91+ if err != nil {
92+ // If the product CR is not found, update status and requeue
93+ if errors .IsNotFound (err ) {
94+ reqLogger .Info ("Application CR not found. Ignoring since object must have been deleted" )
95+ return r .reconcileStatus (applicationAuth , err , reqLogger )
96+ }
9597
96- // If API call error, return err
97- return ctrl.Result {}, err
98- }
98+ // If API call error, return err
99+ return ctrl.Result {}, err
100+ }
99101
100- // Retrieve DeveloperAccount CR, on failed retrieval update status and requeue
101- developerAccount := & capabilitiesv1beta1.DeveloperAccount {}
102- err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .AccountCR .Name , Namespace : applicationAuth .Namespace }, developerAccount )
103- if err != nil {
104- // If the product CR is not found, update status and requeue
105- if errors .IsNotFound (err ) {
106- reqLogger .Info ("DeveloperAccount CR not found. Ignoring since object must have been deleted" )
102+ // Make sure application is ready
103+ err = checkApplicationResources (applicationAuth , application )
104+ if err != nil {
107105 return r .reconcileStatus (applicationAuth , err , reqLogger )
108106 }
109107
110- // If API call error, return err
111- return ctrl.Result {}, err
112- }
108+ // Retrieve DeveloperAccount CR, on failed retrieval update status and requeue
109+ developerAccount := & capabilitiesv1beta1.DeveloperAccount {}
110+ err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .AccountCR .Name , Namespace : applicationAuth .Namespace }, developerAccount )
111+ if err != nil {
112+ // If the product CR is not found, update status and requeue
113+ if errors .IsNotFound (err ) {
114+ reqLogger .Info ("DeveloperAccount CR not found. Ignoring since object must have been deleted" )
115+ return r .reconcileStatus (applicationAuth , err , reqLogger )
116+ }
113117
114- // Retrieve Product CR, on failed retrieval update status and requeue
115- product := & capabilitiesv1beta1.Product {}
116- err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .ProductCR .Name , Namespace : applicationAuth .Namespace }, product )
117- if err != nil {
118- // If the product CR is not found, update status and requeue
119- if errors .IsNotFound (err ) {
120- reqLogger .Info ("Product CR not found. Ignoring since object must have been deleted" )
121- return r .reconcileStatus (applicationAuth , err , reqLogger )
118+ // If API call error, return err
119+ return ctrl.Result {}, err
122120 }
123121
124- // If API call error, return err
125- return ctrl.Result {}, err
126- }
122+ // Retrieve Product CR, on failed retrieval update status and requeue
123+ product := & capabilitiesv1beta1.Product {}
124+ err = r .Client ().Get (r .Context (), types.NamespacedName {Name : application .Spec .ProductCR .Name , Namespace : applicationAuth .Namespace }, product )
125+ if err != nil {
126+ // If the product CR is not found, update status and requeue
127+ if errors .IsNotFound (err ) {
128+ reqLogger .Info ("Product CR not found. Ignoring since object must have been deleted" )
129+ return r .reconcileStatus (applicationAuth , err , reqLogger )
130+ }
127131
128- // Retrieve providerAccountRef
129- providerAccount , err := controllerhelper .LookupProviderAccount (r .Client (), applicationAuth .GetNamespace (), applicationAuth .Spec .ProviderAccountRef , r .Logger ())
130- if err != nil {
131- return ctrl.Result {}, err
132- }
132+ // If API call error, return err
133+ return ctrl.Result {}, err
134+ }
133135
134- // connect to the 3scale porta client
135- insecureSkipVerify := controllerhelper .GetInsecureSkipVerifyAnnotation (applicationAuth .GetAnnotations ())
136- threescaleAPIClient , err := controllerhelper .PortaClient (providerAccount , insecureSkipVerify )
137- if err != nil {
138- return ctrl.Result {}, err
139- }
136+ // Retrieve providerAccountRef
137+ providerAccount , err := controllerhelper .LookupProviderAccount (r .Client (), applicationAuth .GetNamespace (), applicationAuth .Spec .ProviderAccountRef , r .Logger ())
138+ if err != nil {
139+ return ctrl.Result {}, err
140+ }
140141
141- // Retrieve auth secret, on failed retrieval update status and requeue
142- authSecretObj := & corev1.Secret {}
143- err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .AuthSecretRef .Name , Namespace : applicationAuth .Namespace }, authSecretObj )
144- if err != nil {
145- // If the product CR is not found, update status and requeue
146- if errors .IsNotFound (err ) {
147- reqLogger .Info ("ApplicationAuth secret not found. Ignoring since object must have been deleted" )
148- return r .reconcileStatus (applicationAuth , err , reqLogger )
142+ // connect to the 3scale porta client
143+ insecureSkipVerify := controllerhelper .GetInsecureSkipVerifyAnnotation (applicationAuth .GetAnnotations ())
144+ threescaleAPIClient , err := controllerhelper .PortaClient (providerAccount , insecureSkipVerify )
145+ if err != nil {
146+ return ctrl.Result {}, err
149147 }
150- return ctrl.Result {}, err
151- }
152148
153- // populate authSecret struct
154- authSecret := authSecretReferenceSource (r .Client (), applicationAuth .Namespace , applicationAuth .Spec .AuthSecretRef , reqLogger )
155- if ! applicationAuth .Status .Conditions .IsTrueFor (capabilitiesv1beta1 .ApplicationAuthReadyConditionType ) {
156- err := r .applicationAuthReconciler (applicationAuth , developerAccount , application , product , * authSecret , threescaleAPIClient )
149+ // Retrieve auth secret, on failed retrieval update status and requeue
150+ authSecretObj := & corev1.Secret {}
151+ err = r .Client ().Get (r .Context (), types.NamespacedName {Name : applicationAuth .Spec .AuthSecretRef .Name , Namespace : applicationAuth .Namespace }, authSecretObj )
152+ if err != nil {
153+ // If the product CR is not found, update status and requeue
154+ if errors .IsNotFound (err ) {
155+ reqLogger .Info ("ApplicationAuth secret not found. Ignoring since object must have been deleted" )
156+ return r .reconcileStatus (applicationAuth , err , reqLogger )
157+ }
158+ return ctrl.Result {}, err
159+ }
160+
161+ // populate authSecret struct
162+ authSecret := authSecretReferenceSource (r .Client (), applicationAuth .Namespace , applicationAuth .Spec .AuthSecretRef , reqLogger )
163+ err = r .applicationAuthReconciler (applicationAuth , * developerAccount .Status .ID , * application .Status .ID , product , * authSecret , threescaleAPIClient )
157164 if err != nil {
158165 return r .reconcileStatus (applicationAuth , err , reqLogger )
159166 }
@@ -171,8 +178,8 @@ func (r *ApplicationAuthReconciler) SetupWithManager(mgr ctrl.Manager) error {
171178
172179func (r * ApplicationAuthReconciler ) applicationAuthReconciler (
173180 applicationAuth * capabilitiesv1beta1.ApplicationAuth ,
174- developerAccount * capabilitiesv1beta1. DeveloperAccount ,
175- application * capabilitiesv1beta1. Application ,
181+ developerAccountID int64 ,
182+ applicationID int64 ,
176183 product * capabilitiesv1beta1.Product ,
177184 authSecret AuthSecret ,
178185 threescaleClient * threescaleapi.ThreeScaleClient ,
@@ -194,41 +201,34 @@ func (r *ApplicationAuthReconciler) applicationAuthReconciler(
194201 params := make (map [string ]string )
195202 params ["user_key" ] = authSecret .UserKey
196203 // edge case if the operator is stopped before reconcile finished need to nil check application.Status.ID
197- if application .Status .ID != nil {
198- _ , err := threescaleClient .UpdateApplication (* developerAccount .Status .ID , * application .Status .ID , params )
199- if err != nil {
200- return err
201- }
204+ _ , err := threescaleClient .UpdateApplication (developerAccountID , applicationID , params )
205+ if err != nil {
206+ return err
202207 }
203208 }
204209
205210 if authSecret .ApplicationKey != "" {
206- // edge case if the operator is stopped before reconcile finished need to nil check application.Status.ID
207- if application .Status .ID != nil {
208- foundApplication , err := threescaleClient .CreateApplicationKey (* developerAccount .Status .ID , * application .Status .ID , authSecret .ApplicationKey )
209- if err != nil {
210- return err
211- }
212-
213- authSecret .ApplicationID = foundApplication .ApplicationId
211+ foundApplication , err := threescaleClient .CreateApplicationKey (developerAccountID , applicationID , authSecret .ApplicationKey )
212+ if err != nil {
213+ return err
214214 }
215+
216+ authSecret .ApplicationID = foundApplication .ApplicationId
215217 }
216218
217219 if applicationAuth .Spec .GenerateSecret != nil && * applicationAuth .Spec .GenerateSecret {
218- if application .Status .ID != nil {
219- foundApplication , err := threescaleClient .CreateApplicationRandomKey (* developerAccount .Status .ID , * application .Status .ID )
220- if err != nil {
221- return err
222- }
223- authSecret .ApplicationID = foundApplication .ApplicationId
224- var foundApplicationKeys []threescaleapi.ApplicationKey
225- foundApplicationKeys , err = threescaleClient .ApplicationKeys (* developerAccount .Status .ID , * application .Status .ID )
226- if err != nil {
227- return err
228- }
229- lastKey := len (foundApplicationKeys ) - 1
230- authSecret .ApplicationKey = fmt .Sprint (foundApplicationKeys [lastKey ].Value )
220+ foundApplication , err := threescaleClient .CreateApplicationRandomKey (developerAccountID , applicationID )
221+ if err != nil {
222+ return err
223+ }
224+ authSecret .ApplicationID = foundApplication .ApplicationId
225+ var foundApplicationKeys []threescaleapi.ApplicationKey
226+ foundApplicationKeys , err = threescaleClient .ApplicationKeys (developerAccountID , applicationID )
227+ if err != nil {
228+ return err
231229 }
230+ lastKey := len (foundApplicationKeys ) - 1
231+ authSecret .ApplicationKey = fmt .Sprint (foundApplicationKeys [lastKey ].Value )
232232 }
233233
234234 // get the current values and update the secret
@@ -301,3 +301,21 @@ func (r *ApplicationAuthReconciler) reconcileStatus(resource *capabilitiesv1beta
301301
302302 return ctrl.Result {}, nil
303303}
304+
305+ func checkApplicationResources (applicationAuthResource * capabilitiesv1beta1.ApplicationAuth , applicationResource * capabilitiesv1beta1.Application ) error {
306+ errors := field.ErrorList {}
307+
308+ specFldPath := field .NewPath ("spec" )
309+ applicationFldPath := specFldPath .Child ("applicationCRName" )
310+
311+ if applicationResource .Status .ID == nil {
312+ errors = append (errors , field .Invalid (applicationFldPath , applicationAuthResource .Spec .ApplicationCRName , "applicationCR name doesnt have a valid application reference" ))
313+
314+ return & helper.SpecFieldError {
315+ ErrorType : helper .OrphanError ,
316+ FieldErrorList : errors ,
317+ }
318+ }
319+
320+ return nil
321+ }
0 commit comments