Skip to content

Commit da5ac4e

Browse files
author
Anand Kumar
committed
fix: reorder Reconciler methods for funcorder linter
Place exported methods (Reconcile, SetupWithManager) before unexported (buildEnqueueMapFunc, enqueueableNamespace, processReconcileRequest, cleanUp). Made-with: Cursor
1 parent 094d073 commit da5ac4e

1 file changed

Lines changed: 86 additions & 86 deletions

File tree

pkg/controller/istiocsr/controller.go

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
@@ -168,49 +156,6 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
168156
Complete(r)
169157
}
170158

171-
// Reconcile function to compare the state specified by the IstioCSR object against the actual cluster state,
172-
// and to make the cluster state reflect the state specified by the user.
173-
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
174-
r.log.V(1).Info("reconciling", "request", req)
175-
176-
// Fetch the istiocsr.openshift.operator.io CR
177-
istiocsr := &v1alpha1.IstioCSR{}
178-
if err := r.Get(ctx, req.NamespacedName, istiocsr); err != nil {
179-
if errors.IsNotFound(err) {
180-
// NotFound errors, since they can't be fixed by an immediate
181-
// requeue (have to wait for a new notification), and can be processed
182-
// on deleted requests.
183-
r.log.V(1).Info("istiocsr.openshift.operator.io object not found, skipping reconciliation", "request", req)
184-
return ctrl.Result{}, nil
185-
}
186-
return ctrl.Result{}, fmt.Errorf("failed to fetch istiocsr.openshift.operator.io %q during reconciliation: %w", req.NamespacedName, err)
187-
}
188-
189-
if !istiocsr.DeletionTimestamp.IsZero() {
190-
r.log.V(1).Info("istiocsr.openshift.operator.io is marked for deletion", "namespace", req.NamespacedName)
191-
192-
if requeue, err := r.cleanUp(istiocsr); err != nil {
193-
return ctrl.Result{}, fmt.Errorf("clean up failed for %q istiocsr.openshift.operator.io instance deletion: %w", req.NamespacedName, err)
194-
} else if requeue {
195-
return ctrl.Result{RequeueAfter: defaultRequeueTime}, nil
196-
}
197-
198-
if err := r.removeFinalizer(ctx, istiocsr, finalizer); err != nil {
199-
return ctrl.Result{}, err
200-
}
201-
202-
r.log.V(1).Info("removed finalizer, cleanup complete", "request", req.NamespacedName)
203-
return ctrl.Result{}, nil
204-
}
205-
206-
// Set finalizers on the istiocsr.openshift.operator.io resource
207-
if err := r.addFinalizer(ctx, istiocsr); err != nil {
208-
return ctrl.Result{}, fmt.Errorf("failed to update %q istiocsr.openshift.operator.io with finalizers: %w", req.NamespacedName, err)
209-
}
210-
211-
return r.processReconcileRequest(istiocsr, req.NamespacedName)
212-
}
213-
214159
func (r *Reconciler) processReconcileRequest(istiocsr *v1alpha1.IstioCSR, req types.NamespacedName) (ctrl.Result, error) {
215160
istioCSRCreateRecon := false
216161
if !containsProcessedAnnotation(istiocsr) && reflect.DeepEqual(istiocsr.Status, v1alpha1.IstioCSRStatus{}) {
@@ -252,3 +197,58 @@ func (r *Reconciler) cleanUp(istiocsr *v1alpha1.IstioCSR) (bool, error) {
252197
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())
253198
return false, nil
254199
}
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

Comments
 (0)