Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loop over init containers as well #96

Closed
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions pkg/webhook/image_swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ImageSwapper struct {
filters []config.JMESPathFilter

// copier manages the jobs copying the images to the target registry
copier *pond.WorkerPool
copier *pond.WorkerPool

imageSwapPolicy types.ImageSwapPolicy
imageCopyPolicy types.ImageCopyPolicy
Expand All @@ -42,9 +42,9 @@ type ImageSwapper struct {
// NewImageSwapper returns a new ImageSwapper initialized.
func NewImageSwapper(registryClient registry.Client, filters []config.JMESPathFilter, imageSwapPolicy types.ImageSwapPolicy, imageCopyPolicy types.ImageCopyPolicy) mutating.Mutator {
return &ImageSwapper{
registryClient: registryClient,
filters: filters,
copier: pond.New(100, 1000),
registryClient: registryClient,
filters: filters,
copier: pond.New(100, 1000),
imageSwapPolicy: imageSwapPolicy,
imageCopyPolicy: imageCopyPolicy,
}
Expand Down Expand Up @@ -90,10 +90,15 @@ func (p *ImageSwapper) Mutate(ctx context.Context, obj metav1.Object) (bool, err
Logger()
//spew.Dump()

lctx := logger.
WithContext(ctx)
lctx := logger.WithContext(ctx)

for i, container := range pod.Spec.Containers {
p.replaceContainerImages(lctx, ar, pod, pod.Spec.Containers)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.replaceContainerImages(lctx, ar, pod, pod.Spec.Containers)
p.processContainers(lctx, ar, pod, pod.Spec.Containers)

p.replaceContainerImages(lctx, ar, pod, pod.Spec.InitContainers)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.replaceContainerImages(lctx, ar, pod, pod.Spec.InitContainers)
p.processContainers(lctx, ar, pod, pod.Spec.InitContainers)

return false, nil
}

func (p *ImageSwapper) replaceContainerImages(lctx context.Context, ar *v1beta1.AdmissionRequest, pod *corev1.Pod, containers []corev1.Container) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (p *ImageSwapper) replaceContainerImages(lctx context.Context, ar *v1beta1.AdmissionRequest, pod *corev1.Pod, containers []corev1.Container) {
func (p *ImageSwapper) processContainers(lctx context.Context, ar *v1beta1.AdmissionRequest, pod *corev1.Pod, containers []corev1.Container) {

for i, container := range containers {
srcRef, err := alltransports.ParseImageName("docker://" + container.Image)
if err != nil {
log.Ctx(lctx).Warn().Msgf("invalid source name %s: %v", container.Image, err)
Expand Down Expand Up @@ -152,18 +157,16 @@ func (p *ImageSwapper) Mutate(ctx context.Context, obj metav1.Object) (bool, err
switch p.imageSwapPolicy {
case types.ImageSwapPolicyAlways:
log.Ctx(lctx).Debug().Str("image", targetImage).Msg("set new container image")
pod.Spec.Containers[i].Image = targetImage
containers[i].Image = targetImage
case types.ImageSwapPolicyExists:
if p.registryClient.ImageExists(targetImage) {
log.Ctx(lctx).Debug().Str("image", targetImage).Msg("set new container image")
pod.Spec.Containers[i].Image = targetImage
containers[i].Image = targetImage
}
default:
panic("unknown imageSwapPolicy")
}
}

return false, nil
}

// filterMatch returns true if one of the filters matches the context
Expand Down