@@ -67,59 +67,47 @@ func New(mgr ctrl.Manager) (*Reconciler, error) {
6767 }, nil
6868}
6969
70- // buildEnqueueMapFunc returns the map function used to enqueue reconcile requests
71- // when watched resources change.
72- func (r * Reconciler ) buildEnqueueMapFunc () func (ctx context.Context , obj client.Object ) []reconcile.Request {
73- return func (_ context.Context , obj client.Object ) []reconcile.Request {
74- r .log .V (4 ).Info ("received reconcile event" , "object" , fmt .Sprintf ("%T" , obj ), "name" , obj .GetName (), "namespace" , obj .GetNamespace ())
70+ // Reconcile function to compare the state specified by the IstioCSR object against the actual cluster state,
71+ // and to make the cluster state reflect the state specified by the user.
72+ func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
73+ r .log .V (1 ).Info ("reconciling" , "request" , req )
7574
76- objLabels := obj .GetLabels ()
77- if objLabels == nil {
78- r .log .V (4 ).Info ("object not of interest, ignoring reconcile event" , "object" , fmt .Sprintf ("%T" , obj ), "name" , obj .GetName (), "namespace" , obj .GetNamespace ())
79- return []reconcile.Request {}
75+ // Fetch the istiocsr.openshift.operator.io CR
76+ istiocsr := & v1alpha1.IstioCSR {}
77+ if err := r .Get (ctx , req .NamespacedName , istiocsr ); err != nil {
78+ if errors .IsNotFound (err ) {
79+ // NotFound errors, since they can't be fixed by an immediate
80+ // requeue (have to wait for a new notification), and can be processed
81+ // on deleted requests.
82+ r .log .V (1 ).Info ("istiocsr.openshift.operator.io object not found, skipping reconciliation" , "request" , req )
83+ return ctrl.Result {}, nil
8084 }
85+ return ctrl.Result {}, fmt .Errorf ("failed to fetch istiocsr.openshift.operator.io %q during reconciliation: %w" , req .NamespacedName , err )
86+ }
8187
82- // will look for custom label set on objects not created in istiocsr namespace, and if it exists,
83- // namespace in the reconcile request will be set same, else since label check matches is an object
84- // created by controller, and we safely assume, it's in the istiocsr namespace.
85- namespace := objLabels [istiocsrNamespaceMappingLabelName ]
86- if namespace == "" {
87- namespace = obj .GetNamespace ()
88+ if ! istiocsr .DeletionTimestamp .IsZero () {
89+ r .log .V (1 ).Info ("istiocsr.openshift.operator.io is marked for deletion" , "namespace" , req .NamespacedName )
90+
91+ if requeue , err := r .cleanUp (istiocsr ); err != nil {
92+ return ctrl.Result {}, fmt .Errorf ("clean up failed for %q istiocsr.openshift.operator.io instance deletion: %w" , req .NamespacedName , err )
93+ } else if requeue {
94+ return ctrl.Result {RequeueAfter : defaultRequeueTime }, nil
8895 }
8996
90- if ok , ns := r .enqueueableNamespace (objLabels , obj , namespace ); ok && ns != "" {
91- return []reconcile.Request {
92- {
93- NamespacedName : types.NamespacedName {
94- Name : istiocsrObjectName ,
95- Namespace : ns ,
96- },
97- },
98- }
97+ if err := r .removeFinalizer (ctx , istiocsr , finalizer ); err != nil {
98+ return ctrl.Result {}, err
9999 }
100100
101- r .log .V (4 ).Info ("object not of interest, ignoring reconcile event " , "object " , fmt . Sprintf ( "%T" , obj ), "name" , obj . GetName (), "namespace" , obj . GetNamespace () )
102- return []reconcile. Request {}
101+ r .log .V (1 ).Info ("removed finalizer, cleanup complete " , "request " , req . NamespacedName )
102+ return ctrl. Result {}, nil
103103 }
104- }
105104
106- // enqueueableNamespace checks whether the object's labels indicate it should
107- // trigger a reconcile and returns the resolved namespace. The defaultNS is used
108- // when no watch-label override is present.
109- func (r * Reconciler ) enqueueableNamespace (objLabels map [string ]string , obj client.Object , defaultNS string ) (bool , string ) {
110- if objLabels [common .ManagedResourceLabelKey ] == RequestEnqueueLabelValue {
111- return true , defaultNS
112- }
113- value := objLabels [IstiocsrResourceWatchLabelName ]
114- if value == "" {
115- return false , ""
116- }
117- key := strings .Split (value , "_" )
118- if len (key ) != 2 {
119- r .log .Error (errInvalidLabelFormat , "%s label value(%s) not in expected format on %s resource" , IstiocsrResourceWatchLabelName , value , obj .GetName ())
120- return false , ""
105+ // Set finalizers on the istiocsr.openshift.operator.io resource
106+ if err := r .addFinalizer (ctx , istiocsr ); err != nil {
107+ return ctrl.Result {}, fmt .Errorf ("failed to update %q istiocsr.openshift.operator.io with finalizers: %w" , req .NamespacedName , err )
121108 }
122- return true , key [0 ]
109+
110+ return r .processReconcileRequest (istiocsr , req .NamespacedName )
123111}
124112
125113// SetupWithManager sets up the controller with the Manager.
@@ -165,54 +153,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
165153 Watches (& corev1.ConfigMap {}, handler .EnqueueRequestsFromMapFunc (mapFunc ), controllerConfigMapWatchPredicates ).
166154 WatchesMetadata (& corev1.Secret {}, handler .EnqueueRequestsFromMapFunc (mapFunc ), controllerWatchResourcePredicates ).
167155 Watches (& networkingv1.NetworkPolicy {}, handler .EnqueueRequestsFromMapFunc (mapFunc ), controllerManagedResourcePredicates ).
168- Watches (& certmanagerv1.Issuer {}, handler .EnqueueRequestsFromMapFunc (mapFunc ), controllerWatchResourcePredicates ).
169- Watches (& certmanagerv1.ClusterIssuer {}, handler .EnqueueRequestsFromMapFunc (mapFunc ), controllerWatchResourcePredicates ).
170156 Complete (r )
171157}
172158
173- // Reconcile function to compare the state specified by the IstioCSR object against the actual cluster state,
174- // and to make the cluster state reflect the state specified by the user.
175- func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
176- r .log .V (1 ).Info ("reconciling" , "request" , req )
177-
178- // Fetch the istiocsr.openshift.operator.io CR
179- istiocsr := & v1alpha1.IstioCSR {}
180- if err := r .Get (ctx , req .NamespacedName , istiocsr ); err != nil {
181- if errors .IsNotFound (err ) {
182- // NotFound errors, since they can't be fixed by an immediate
183- // requeue (have to wait for a new notification), and can be processed
184- // on deleted requests.
185- r .log .V (1 ).Info ("istiocsr.openshift.operator.io object not found, skipping reconciliation" , "request" , req )
186- return ctrl.Result {}, nil
187- }
188- return ctrl.Result {}, fmt .Errorf ("failed to fetch istiocsr.openshift.operator.io %q during reconciliation: %w" , req .NamespacedName , err )
189- }
190-
191- if ! istiocsr .DeletionTimestamp .IsZero () {
192- r .log .V (1 ).Info ("istiocsr.openshift.operator.io is marked for deletion" , "namespace" , req .NamespacedName )
193-
194- if requeue , err := r .cleanUp (istiocsr ); err != nil {
195- return ctrl.Result {}, fmt .Errorf ("clean up failed for %q istiocsr.openshift.operator.io instance deletion: %w" , req .NamespacedName , err )
196- } else if requeue {
197- return ctrl.Result {RequeueAfter : defaultRequeueTime }, nil
198- }
199-
200- if err := r .removeFinalizer (ctx , istiocsr , finalizer ); err != nil {
201- return ctrl.Result {}, err
202- }
203-
204- r .log .V (1 ).Info ("removed finalizer, cleanup complete" , "request" , req .NamespacedName )
205- return ctrl.Result {}, nil
206- }
207-
208- // Set finalizers on the istiocsr.openshift.operator.io resource
209- if err := r .addFinalizer (ctx , istiocsr ); err != nil {
210- return ctrl.Result {}, fmt .Errorf ("failed to update %q istiocsr.openshift.operator.io with finalizers: %w" , req .NamespacedName , err )
211- }
212-
213- return r .processReconcileRequest (istiocsr , req .NamespacedName )
214- }
215-
216159func (r * Reconciler ) processReconcileRequest (istiocsr * v1alpha1.IstioCSR , req types.NamespacedName ) (ctrl.Result , error ) {
217160 istioCSRCreateRecon := false
218161 if ! containsProcessedAnnotation (istiocsr ) && reflect .DeepEqual (istiocsr .Status , v1alpha1.IstioCSRStatus {}) {
@@ -254,3 +197,58 @@ func (r *Reconciler) cleanUp(istiocsr *v1alpha1.IstioCSR) (bool, error) {
254197 r .eventRecorder .Eventf (istiocsr , corev1 .EventTypeWarning , "RemoveDeployment" , "%s/%s istiocsr marked for deletion, remove reference in istiod deployment and remove all resources created for istiocsr deployment" , istiocsr .GetNamespace (), istiocsr .GetName ())
255198 return false , nil
256199}
200+
201+ // buildEnqueueMapFunc returns the map function used to enqueue reconcile requests
202+ // when watched resources change.
203+ func (r * Reconciler ) buildEnqueueMapFunc () func (ctx context.Context , obj client.Object ) []reconcile.Request {
204+ return func (_ context.Context , obj client.Object ) []reconcile.Request {
205+ r .log .V (4 ).Info ("received reconcile event" , "object" , fmt .Sprintf ("%T" , obj ), "name" , obj .GetName (), "namespace" , obj .GetNamespace ())
206+
207+ objLabels := obj .GetLabels ()
208+ if objLabels == nil {
209+ r .log .V (4 ).Info ("object not of interest, ignoring reconcile event" , "object" , fmt .Sprintf ("%T" , obj ), "name" , obj .GetName (), "namespace" , obj .GetNamespace ())
210+ return []reconcile.Request {}
211+ }
212+
213+ // will look for custom label set on objects not created in istiocsr namespace, and if it exists,
214+ // namespace in the reconcile request will be set same, else since label check matches is an object
215+ // created by controller, and we safely assume, it's in the istiocsr namespace.
216+ namespace := objLabels [istiocsrNamespaceMappingLabelName ]
217+ if namespace == "" {
218+ namespace = obj .GetNamespace ()
219+ }
220+
221+ if ok , ns := r .enqueueableNamespace (objLabels , obj , namespace ); ok && ns != "" {
222+ return []reconcile.Request {
223+ {
224+ NamespacedName : types.NamespacedName {
225+ Name : istiocsrObjectName ,
226+ Namespace : ns ,
227+ },
228+ },
229+ }
230+ }
231+
232+ r .log .V (4 ).Info ("object not of interest, ignoring reconcile event" , "object" , fmt .Sprintf ("%T" , obj ), "name" , obj .GetName (), "namespace" , obj .GetNamespace ())
233+ return []reconcile.Request {}
234+ }
235+ }
236+
237+ // enqueueableNamespace checks whether the object's labels indicate it should
238+ // trigger a reconcile and returns the resolved namespace. The defaultNS is used
239+ // when no watch-label override is present.
240+ func (r * Reconciler ) enqueueableNamespace (objLabels map [string ]string , obj client.Object , defaultNS string ) (bool , string ) {
241+ if objLabels [common .ManagedResourceLabelKey ] == RequestEnqueueLabelValue {
242+ return true , defaultNS
243+ }
244+ value := objLabels [IstiocsrResourceWatchLabelName ]
245+ if value == "" {
246+ return false , ""
247+ }
248+ key := strings .Split (value , "_" )
249+ if len (key ) != 2 {
250+ r .log .Error (errInvalidLabelFormat , "%s label value(%s) not in expected format on %s resource" , IstiocsrResourceWatchLabelName , value , obj .GetName ())
251+ return false , ""
252+ }
253+ return true , key [0 ]
254+ }
0 commit comments