@@ -94,7 +94,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
9494}
9595
9696func (r * Reconciler ) updateStatusSuccess (ctx context.Context , controllerRing , before * shardingv1alpha1.ControllerRing ) error {
97- if err := r .optionallyUpdateStatus (ctx , controllerRing , before , func (ready * metav1.Condition ) {
97+ if err := r .OptionallyUpdateStatus (ctx , controllerRing , before , func (ready * metav1.Condition ) {
9898 ready .Status = metav1 .ConditionTrue
9999 ready .Reason = "ReconciliationSucceeded"
100100 ready .Message = "ControllerRing was successfully reconciled"
@@ -109,7 +109,7 @@ func (r *Reconciler) updateStatusError(ctx context.Context, log logr.Logger, rec
109109
110110 r .Recorder .Event (controllerRing , corev1 .EventTypeWarning , "ReconciliationFailed" , message )
111111
112- if err := r .optionallyUpdateStatus (ctx , controllerRing , before , func (ready * metav1.Condition ) {
112+ if err := r .OptionallyUpdateStatus (ctx , controllerRing , before , func (ready * metav1.Condition ) {
113113 ready .Status = metav1 .ConditionFalse
114114 ready .Reason = "ReconciliationFailed"
115115 ready .Message = message
@@ -122,7 +122,7 @@ func (r *Reconciler) updateStatusError(ctx context.Context, log logr.Logger, rec
122122 return reconcileError
123123}
124124
125- func (r * Reconciler ) optionallyUpdateStatus (ctx context.Context , controllerRing , before * shardingv1alpha1.ControllerRing , mutate func (ready * metav1.Condition )) error {
125+ func (r * Reconciler ) OptionallyUpdateStatus (ctx context.Context , controllerRing , before * shardingv1alpha1.ControllerRing , mutate func (ready * metav1.Condition )) error {
126126 // always update status with the latest observed generation, no matter if reconciliation succeeded or not
127127 controllerRing .Status .ObservedGeneration = controllerRing .Generation
128128 readyCondition := metav1.Condition {
@@ -141,6 +141,15 @@ func (r *Reconciler) optionallyUpdateStatus(ctx context.Context, controllerRing,
141141}
142142
143143func (r * Reconciler ) reconcileWebhooks (ctx context.Context , controllerRing * shardingv1alpha1.ControllerRing ) error {
144+ webhookConfig , err := r .WebhookConfigForControllerRing (controllerRing )
145+ if err != nil {
146+ return err
147+ }
148+
149+ return r .Client .Patch (ctx , webhookConfig , client .Apply )
150+ }
151+
152+ func (r * Reconciler ) WebhookConfigForControllerRing (controllerRing * shardingv1alpha1.ControllerRing ) (* admissionregistrationv1.MutatingWebhookConfiguration , error ) {
144153 webhookConfig := & admissionregistrationv1.MutatingWebhookConfiguration {
145154 TypeMeta : metav1.TypeMeta {
146155 APIVersion : admissionregistrationv1 .SchemeGroupVersion .String (),
@@ -154,11 +163,16 @@ func (r *Reconciler) reconcileWebhooks(ctx context.Context, controllerRing *shar
154163 },
155164 Annotations : maps .Clone (r .Config .Webhook .Config .Annotations ),
156165 },
166+ Webhooks : []admissionregistrationv1.MutatingWebhook {r .WebhookForControllerRing (controllerRing )},
157167 }
158168 if err := controllerutil .SetControllerReference (controllerRing , webhookConfig , r .Client .Scheme ()); err != nil {
159- return fmt .Errorf ("error setting controller reference: %w" , err )
169+ return nil , fmt .Errorf ("error setting controller reference: %w" , err )
160170 }
161171
172+ return webhookConfig , nil
173+ }
174+
175+ func (r * Reconciler ) WebhookForControllerRing (controllerRing * shardingv1alpha1.ControllerRing ) admissionregistrationv1.MutatingWebhook {
162176 webhook := admissionregistrationv1.MutatingWebhook {
163177 Name : "sharder.sharding.timebertt.dev" ,
164178 ClientConfig : * r .Config .Webhook .Config .ClientConfig .DeepCopy (),
@@ -207,9 +221,7 @@ func (r *Reconciler) reconcileWebhooks(ctx context.Context, controllerRing *shar
207221 }
208222 }
209223
210- webhookConfig .Webhooks = []admissionregistrationv1.MutatingWebhook {webhook }
211-
212- return r .Client .Patch (ctx , webhookConfig , client .Apply )
224+ return webhook
213225}
214226
215227// RuleForResource returns the sharder's webhook rule for the given resource.
0 commit comments