@@ -26,13 +26,13 @@ import (
2626
2727 argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
2828 "github.com/argoproj-labs/argocd-operator/common"
29+ "github.com/argoproj-labs/argocd-operator/controllers/argoutil"
2930
3031 corev1 "k8s.io/api/core/v1"
3132 "k8s.io/apimachinery/pkg/api/errors"
3233 "k8s.io/apimachinery/pkg/labels"
3334 "k8s.io/apimachinery/pkg/runtime"
3435 "k8s.io/apimachinery/pkg/types"
35-
3636 "k8s.io/client-go/kubernetes"
3737
3838 ctrl "sigs.k8s.io/controller-runtime"
@@ -83,6 +83,8 @@ type ReconcileArgoCD struct {
8383
8484 K8sClient kubernetes.Interface
8585 LocalUsers * LocalUsersInfo
86+ // FipsConfigChecker checks if the deployment needs FIPS specific environment variables set.
87+ FipsConfigChecker argoutil.FipsConfigChecker
8688}
8789
8890var log = logr .Log .WithName ("controller_argocd" )
@@ -124,22 +126,32 @@ var ActiveInstanceMap = make(map[string]string)
124126// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 125127func (r * ReconcileArgoCD ) Reconcile (ctx context.Context , request ctrl.Request ) (ctrl.Result , error ) {
126128
127- result , argocd , err := r .internalReconcile (ctx , request )
129+ result , argocd , argocdStatus , err := r .internalReconcile (ctx , request )
128130
129131 message := ""
130132 if err != nil {
131133 message = err .Error ()
134+ argocdStatus .Phase = "Failed" // Any error should reset phase back to Failed
135+ }
136+
137+ log .Info ("reconciling status" )
138+ if reconcileStatusErr := r .reconcileStatus (argocd , argocdStatus ); reconcileStatusErr != nil {
139+ log .Error (reconcileStatusErr , "Unable to reconcile status" )
140+ argocdStatus .Phase = "Failed"
141+ message = "unable to reconcile ArgoCD CR .status field"
132142 }
133143
134- if error := updateStatusConditionOfArgoCD (ctx , createCondition (message ), argocd , r .Client , log ); error != nil {
135- log .Error (error , "unable to update status of ArgoCD" )
136- return reconcile.Result {}, error
144+ if updateStatusErr := updateStatusConditionOfArgoCD (ctx , createCondition (message ), argocd , argocdStatus , r .Client , log ); updateStatusErr != nil {
145+ log .Error (updateStatusErr , "unable to update status of ArgoCD" )
146+ return reconcile.Result {}, updateStatusErr
137147 }
138148
139149 return result , err
140150}
141151
142- func (r * ReconcileArgoCD ) internalReconcile (ctx context.Context , request ctrl.Request ) (ctrl.Result , * argoproj.ArgoCD , error ) {
152+ func (r * ReconcileArgoCD ) internalReconcile (ctx context.Context , request ctrl.Request ) (ctrl.Result , * argoproj.ArgoCD , * argoproj.ArgoCDStatus , error ) {
153+
154+ argoCDStatus := & argoproj.ArgoCDStatus {} // Start with a blank canvas
143155
144156 reconcileStartTS := time .Now ()
145157 defer func () {
@@ -156,10 +168,10 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
156168 // Request object not found, could have been deleted after reconcile request.
157169 // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
158170 // Return and don't requeue
159- return reconcile.Result {}, argocd , nil
171+ return reconcile.Result {}, argocd , argoCDStatus , nil
160172 }
161173 // Error reading the object - requeue the request.
162- return reconcile.Result {}, argocd , err
174+ return reconcile.Result {}, argocd , argoCDStatus , err
163175 }
164176
165177 // If the number of notification replicas is greater than 1, display a warning.
@@ -171,14 +183,14 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
171183 labelSelector , err := labels .Parse (r .LabelSelector )
172184 if err != nil {
173185 message := fmt .Sprintf ("error parsing the labelSelector '%s'." , labelSelector )
174- reqLogger .Info ( message )
175- return reconcile.Result {}, argocd , fmt .Errorf ("%s error: %w" , message , err )
186+ reqLogger .Error ( err , message )
187+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("%s error: %w" , message , err )
176188 }
177189
178190 // Match the value of labelSelector from ReconcileArgoCD to labels from the argocd instance
179191 if ! labelSelector .Matches (labels .Set (argocd .Labels )) {
180- reqLogger .Info ( fmt .Sprintf ("the ArgoCD instance '%s' does not match the label selector '%s' and skipping for reconciliation" , request .NamespacedName , r .LabelSelector ))
181- return reconcile.Result {}, argocd , fmt .Errorf ("the ArgoCD instance '%s' does not match the label selector '%s' and skipping for reconciliation" , request .NamespacedName , r .LabelSelector )
192+ reqLogger .Error ( nil , fmt .Sprintf ("the ArgoCD instance '%s' does not match the label selector '%s' and skipping for reconciliation" , request .NamespacedName , r .LabelSelector ))
193+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("the ArgoCD instance '%s' does not match the label selector '%s' and skipping for reconciliation" , request .NamespacedName , r .LabelSelector )
182194 }
183195
184196 newPhase := argocd .Status .Phase
@@ -207,6 +219,8 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
207219
208220 if argocd .GetDeletionTimestamp () != nil {
209221
222+ argoCDStatus .Phase = "Unknown" // Set to Unknown since we are in the process of deleting ArgoCD CR
223+
210224 // Argo CD instance marked for deletion; remove entry from activeInstances map and decrement active instance count
211225 // by phase as well as total
212226 delete (ActiveInstanceMap , argocd .Namespace )
@@ -220,74 +234,69 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
220234
221235 if argocd .IsDeletionFinalizerPresent () {
222236 if err := r .deleteClusterResources (argocd ); err != nil {
223- return reconcile.Result {}, argocd , fmt .Errorf ("failed to delete ClusterResources: %w" , err )
237+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("failed to delete ClusterResources: %w" , err )
224238 }
225239
226240 if isRemoveManagedByLabelOnArgoCDDeletion () {
227241 if err := r .removeManagedByLabelFromNamespaces (argocd .Namespace ); err != nil {
228- return reconcile.Result {}, argocd , fmt .Errorf ("failed to remove label from namespace[%v], error: %w" , argocd .Namespace , err )
242+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("failed to remove label from namespace[%v], error: %w" , argocd .Namespace , err )
229243 }
230244 }
231245
232246 if err := r .removeUnmanagedSourceNamespaceResources (argocd ); err != nil {
233- return reconcile.Result {}, argocd , fmt .Errorf ("failed to remove resources from sourceNamespaces, error: %w" , err )
247+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("failed to remove resources from sourceNamespaces, error: %w" , err )
234248 }
235249
236250 if err := r .removeUnmanagedApplicationSetSourceNamespaceResources (argocd ); err != nil {
237- return reconcile.Result {}, argocd , fmt .Errorf ("failed to remove resources from applicationSetSourceNamespaces, error: %w" , err )
251+ return reconcile.Result {}, argocd , argoCDStatus , fmt .Errorf ("failed to remove resources from applicationSetSourceNamespaces, error: %w" , err )
238252 }
239253
240254 if err := r .removeDeletionFinalizer (argocd ); err != nil {
241- return reconcile.Result {}, argocd , err
255+ return reconcile.Result {}, argocd , argoCDStatus , err
242256 }
243257
244258 // remove namespace of deleted Argo CD instance from deprecationEventEmissionTracker (if exists) so that if another instance
245259 // is created in the same namespace in the future, that instance is appropriately tracked
246260 delete (DeprecationEventEmissionTracker , argocd .Namespace )
247261 }
248262
249- return reconcile.Result {}, argocd , nil
263+ return reconcile.Result {}, argocd , argoCDStatus , nil
250264 }
251265
252266 if ! argocd .IsDeletionFinalizerPresent () {
253267 if err := r .addDeletionFinalizer (argocd ); err != nil {
254- return reconcile.Result {}, argocd , err
268+ return reconcile.Result {}, argocd , argoCDStatus , err
255269 }
256270 }
257271
258- // get the latest version of argocd instance before reconciling
259- if err = r .Get (ctx , request .NamespacedName , argocd ); err != nil {
260- return reconcile.Result {}, argocd , err
261- }
262-
263272 if err = r .setManagedNamespaces (argocd ); err != nil {
264- return reconcile.Result {}, argocd , err
273+ return reconcile.Result {}, argocd , argoCDStatus , err
265274 }
266275
267276 if err = r .setManagedSourceNamespaces (argocd ); err != nil {
268- return reconcile.Result {}, argocd , err
277+ return reconcile.Result {}, argocd , argoCDStatus , err
269278 }
270279
271280 if err = r .setManagedApplicationSetSourceNamespaces (argocd ); err != nil {
272- return reconcile.Result {}, argocd , err
281+ return reconcile.Result {}, argocd , argoCDStatus , err
273282 }
274283
275284 // Handle NamespaceManagement reconciliation and check if Namespace Management is enabled via the Subscription env variable.
276285 if isNamespaceManagementEnabled () {
277286 if err := r .reconcileNamespaceManagement (argocd ); err != nil {
278- return reconcile.Result {}, argocd , err
287+ return reconcile.Result {}, argocd , argoCDStatus , err
279288 }
280289 } else if argocd .Spec .NamespaceManagement != nil {
281290 k8sClient := r .K8sClient
282291 if err := r .disableNamespaceManagement (argocd , k8sClient ); err != nil {
283292 log .Error (err , "Failed to disable NamespaceManagement feature" )
284- return reconcile.Result {}, argocd , err
293+ return reconcile.Result {}, argocd , argoCDStatus , err
285294 }
286295 } else if len (argocd .Spec .NamespaceManagement ) == 0 {
287296 // Handle cleanup of NamespaceManagement RBAC when the feature is removed from the ArgoCD CR.
288297 nsMgmtList := & argoproj.NamespaceManagementList {}
289298 if err := r .List (context .TODO (), nsMgmtList ); err != nil {
290- return reconcile.Result {}, argocd , err
299+ return reconcile.Result {}, argocd , argoCDStatus , err
291300 }
292301
293302 k8sClient := r .K8sClient
@@ -301,7 +310,7 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
301310 namespace := & corev1.Namespace {}
302311 if err := r .Get (ctx , types.NamespacedName {Name : nsMgmt .Namespace }, namespace ); err != nil {
303312 log .Error (err , fmt .Sprintf ("unable to fetch namespace %s" , nsMgmt .Namespace ))
304- return reconcile.Result {}, argocd , err
313+ return reconcile.Result {}, argocd , argoCDStatus , err
305314 }
306315
307316 // Skip RBAC deletion if the namespace has the "managed-by" label
@@ -313,13 +322,13 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
313322 // Remove roles and rolebindings
314323 if err := deleteRBACsForNamespace (nsMgmt .Namespace , k8sClient ); err != nil {
315324 log .Error (err , fmt .Sprintf ("Failed to delete RBACs for namespace: %s" , nsMgmt .Namespace ))
316- return reconcile.Result {}, argocd , err
325+ return reconcile.Result {}, argocd , argoCDStatus , err
317326 }
318327 log .Info (fmt .Sprintf ("Successfully removed RBACs for namespace: %s" , nsMgmt .Namespace ))
319328
320329 if err := deleteManagedNamespaceFromClusterSecret (argocd .Namespace , nsMgmt .Namespace , k8sClient ); err != nil {
321330 log .Error (err , fmt .Sprintf ("Unable to delete namespace %s from cluster secret" , nsMgmt .Namespace ))
322- return reconcile.Result {}, argocd , err
331+ return reconcile.Result {}, argocd , argoCDStatus , err
323332 }
324333
325334 }
@@ -328,13 +337,13 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
328337 // Process DropMetadata for namespace-based label cleanup
329338 r .processDropMetadataForCleanup (argocd )
330339
331- if err := r .reconcileResources (argocd ); err != nil {
340+ if err := r .reconcileResources (argocd , argoCDStatus ); err != nil {
332341 // Error reconciling ArgoCD sub-resources - requeue the request.
333- return reconcile.Result {}, argocd , err
342+ return reconcile.Result {}, argocd , argoCDStatus , err
334343 }
335344
336345 // Return and don't requeue
337- return reconcile.Result {}, argocd , nil
346+ return reconcile.Result {}, argocd , argoCDStatus , nil
338347}
339348
340349// SetupWithManager sets up the controller with the Manager.
0 commit comments