Skip to content

Commit bc45e1d

Browse files
committed
feat: inject rerouted image pull secret if not already present in the pod
1 parent 70e06f7 commit bc45e1d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

internal/webhook/core/v1/pod_webhook.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,22 @@ func (d *PodCustomDefaulter) defaultt(ctx context.Context, pod *corev1.Pod) erro
214214
return err
215215
}
216216

217-
d.rerouteContainerImage(ctx, &container, podImagePullSecrets)
217+
alternativeSecret := d.rerouteContainerImage(ctx, &container, podImagePullSecrets)
218+
alternativeSecretIndex := slices.IndexFunc(pod.Spec.ImagePullSecrets, func(localObjectReference corev1.LocalObjectReference) bool {
219+
return localObjectReference.Name == alternativeSecret.Name
220+
})
221+
// Inject rerouted image pull secret if not already present in the pod
222+
if alternativeSecretIndex == -1 {
223+
pod.Spec.ImagePullSecrets = append(pod.Spec.ImagePullSecrets, corev1.LocalObjectReference{
224+
Name: alternativeSecret.Name,
225+
})
226+
}
218227
}
219228

220229
return nil
221230
}
222231

223-
func (d *PodCustomDefaulter) rerouteContainerImage(ctx context.Context, container *Container, pullSecrets []corev1.Secret) {
232+
func (d *PodCustomDefaulter) rerouteContainerImage(ctx context.Context, container *Container, pullSecrets []corev1.Secret) *corev1.Secret {
224233
log := logf.FromContext(ctx)
225234

226235
for _, image := range container.Images {
@@ -237,9 +246,11 @@ func (d *PodCustomDefaulter) rerouteContainerImage(ctx context.Context, containe
237246
log.Info("rerouting image", "container", container.Name, "isInit", container.IsInit, "originalImage", container.Image, "reroutedImage", image.Reference)
238247
container.Image = image.Reference
239248
}
240-
return
249+
return image.ImagePullSecret
241250
}
242251
}
252+
253+
return nil
243254
}
244255

245256
func (d *PodCustomDefaulter) checkImageAvailability(ctx context.Context, reference string, pullSecrets []corev1.Secret) (bool, error) {

0 commit comments

Comments
 (0)