From 74c3680628cabbb98b887d0bc0abe023fd16ffad Mon Sep 17 00:00:00 2001 From: Paul Laffitte Date: Sat, 8 Jun 2024 00:59:24 +0200 Subject: [PATCH] fix(controller): manually add a decoder since its not injected anymore removed in controller-runtime v0.15.0 https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0 --- api/core/v1/pod_webhook.go | 10 ++-------- api/core/v1/pod_webhook_test.go | 15 --------------- cmd/cache/main.go | 2 ++ 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/api/core/v1/pod_webhook.go b/api/core/v1/pod_webhook.go index ce3262db..9effa060 100644 --- a/api/core/v1/pod_webhook.go +++ b/api/core/v1/pod_webhook.go @@ -37,7 +37,7 @@ type ImageRewriter struct { IgnoreImages []*regexp.Regexp IgnorePullPolicyAlways bool ProxyPort int - decoder *admission.Decoder + Decoder *admission.Decoder } type PodInitializer struct { @@ -56,7 +56,7 @@ func (a *ImageRewriter) Handle(ctx context.Context, req admission.Request) admis WithName("webhook.pod") pod := &corev1.Pod{} - err := a.decoder.Decode(req, pod) + err := a.Decoder.Decode(req, pod) if err != nil { return admission.Errored(http.StatusBadRequest, err) } @@ -106,12 +106,6 @@ func (a *ImageRewriter) RewriteImages(pod *corev1.Pod, isNewPod bool) []Rewritte return rewrittenImages } -// InjectDecoder injects the decoder -func (a *ImageRewriter) InjectDecoder(d *admission.Decoder) error { - a.decoder = d - return nil -} - func (a *ImageRewriter) handleContainer(pod *corev1.Pod, container *corev1.Container, annotationKey string, rewriteImage bool) RewrittenImage { if err := a.isImageRewritable(container); err != nil { return RewrittenImage{ diff --git a/api/core/v1/pod_webhook_test.go b/api/core/v1/pod_webhook_test.go index b2aaf780..a058d18b 100644 --- a/api/core/v1/pod_webhook_test.go +++ b/api/core/v1/pod_webhook_test.go @@ -11,7 +11,6 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) var podStub = corev1.Pod{ @@ -116,20 +115,6 @@ func TestRewriteImagesWithIgnore(t *testing.T) { }) } -func TestInjectDecoder(t *testing.T) { - g := NewWithT(t) - t.Run("Inject decoder", func(t *testing.T) { - ir := ImageRewriter{} - decoder := &admission.Decoder{} - - g.Expect(ir.decoder).To(BeNil()) - err := ir.InjectDecoder(decoder) - g.Expect(err).To(Not(HaveOccurred())) - g.Expect(ir.decoder).To(Not(BeNil())) - g.Expect(ir.decoder).To(Equal(decoder)) - }) -} - func Test_isImageRewritable(t *testing.T) { emptyRegexps := []*regexp.Regexp{} someRegexps := []*regexp.Regexp{ diff --git a/cmd/cache/main.go b/cmd/cache/main.go index b40bcaef..2140bef6 100644 --- a/cmd/cache/main.go +++ b/cmd/cache/main.go @@ -15,6 +15,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" kuikenixiov1 "github.com/enix/kube-image-keeper/api/core/v1" kuikv1alpha1 "github.com/enix/kube-image-keeper/api/kuik/v1alpha1" @@ -112,6 +113,7 @@ func main() { IgnoreImages: ignoreImages, IgnorePullPolicyAlways: ignorePullPolicyAlways, ProxyPort: proxyPort, + Decoder: admission.NewDecoder(mgr.GetScheme()), } mgr.GetWebhookServer().Register("/mutate-core-v1-pod", &webhook.Admission{Handler: &imageRewriter}) if err = (&kuikv1alpha1.CachedImage{}).SetupWebhookWithManager(mgr); err != nil {