@@ -69,13 +69,21 @@ func (s *Syncer) Start(ctx context.Context) error {
6969
7070 _ , err := cmInformer .AddEventHandler (cache.ResourceEventHandlerFuncs {
7171 AddFunc : func (obj interface {}) {
72- cm := obj .(* corev1.ConfigMap )
72+ cm , ok := obj .(* corev1.ConfigMap )
73+ if ! ok {
74+ klog .Warningf ("unexpected object type in AddFunc: %T" , obj )
75+ return
76+ }
7377 if cm .Name == s .caBundleConfigMapName {
7478 s .onConfigMapUpdate (ctx , cm )
7579 }
7680 },
7781 UpdateFunc : func (oldObj , newObj interface {}) {
78- cm := newObj .(* corev1.ConfigMap )
82+ cm , ok := newObj .(* corev1.ConfigMap )
83+ if ! ok {
84+ klog .Warningf ("unexpected object type in UpdateFunc: %T" , newObj )
85+ return
86+ }
7987 if cm .Name == s .caBundleConfigMapName {
8088 s .onConfigMapUpdate (ctx , cm )
8189 }
@@ -141,9 +149,21 @@ func (s *Syncer) patchWebhook(ctx context.Context, ref WebhookRef, caBundle []by
141149 }
142150}
143151
152+ // buildCABundlePatch builds a JSON patch for updating caBundle on all webhooks.
153+ func buildCABundlePatch (webhookCount int , caBundle []byte ) ([]byte , error ) {
154+ var patches []map [string ]interface {}
155+ for i := 0 ; i < webhookCount ; i ++ {
156+ patches = append (patches , map [string ]interface {}{
157+ "op" : "replace" ,
158+ "path" : fmt .Sprintf ("/webhooks/%d/clientConfig/caBundle" , i ),
159+ "value" : caBundle ,
160+ })
161+ }
162+ return json .Marshal (patches )
163+ }
164+
144165// patchValidatingWebhook patches a ValidatingWebhookConfiguration.
145166func (s * Syncer ) patchValidatingWebhook (ctx context.Context , name string , caBundle []byte ) error {
146- // Get current configuration to determine how many webhooks need patching
147167 current , err := s .client .AdmissionregistrationV1 ().ValidatingWebhookConfigurations ().Get (ctx , name , metav1.GetOptions {})
148168 if err != nil {
149169 if errors .IsNotFound (err ) {
@@ -153,17 +173,7 @@ func (s *Syncer) patchValidatingWebhook(ctx context.Context, name string, caBund
153173 return err
154174 }
155175
156- // Build patch for all webhooks
157- var patches []map [string ]interface {}
158- for i := range current .Webhooks {
159- patches = append (patches , map [string ]interface {}{
160- "op" : "replace" ,
161- "path" : fmt .Sprintf ("/webhooks/%d/clientConfig/caBundle" , i ),
162- "value" : caBundle ,
163- })
164- }
165-
166- patchBytes , err := json .Marshal (patches )
176+ patchBytes , err := buildCABundlePatch (len (current .Webhooks ), caBundle )
167177 if err != nil {
168178 return fmt .Errorf ("failed to marshal patch: %w" , err )
169179 }
@@ -175,7 +185,6 @@ func (s *Syncer) patchValidatingWebhook(ctx context.Context, name string, caBund
175185
176186// patchMutatingWebhook patches a MutatingWebhookConfiguration.
177187func (s * Syncer ) patchMutatingWebhook (ctx context.Context , name string , caBundle []byte ) error {
178- // Get current configuration to determine how many webhooks need patching
179188 current , err := s .client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Get (ctx , name , metav1.GetOptions {})
180189 if err != nil {
181190 if errors .IsNotFound (err ) {
@@ -185,17 +194,7 @@ func (s *Syncer) patchMutatingWebhook(ctx context.Context, name string, caBundle
185194 return err
186195 }
187196
188- // Build patch for all webhooks
189- var patches []map [string ]interface {}
190- for i := range current .Webhooks {
191- patches = append (patches , map [string ]interface {}{
192- "op" : "replace" ,
193- "path" : fmt .Sprintf ("/webhooks/%d/clientConfig/caBundle" , i ),
194- "value" : caBundle ,
195- })
196- }
197-
198- patchBytes , err := json .Marshal (patches )
197+ patchBytes , err := buildCABundlePatch (len (current .Webhooks ), caBundle )
199198 if err != nil {
200199 return fmt .Errorf ("failed to marshal patch: %w" , err )
201200 }
0 commit comments