Skip to content

Commit

Permalink
fix(controller): manually add a decoder since its not injected anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Jun 10, 2024
1 parent cce0226 commit 74c3680
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
10 changes: 2 additions & 8 deletions api/core/v1/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ImageRewriter struct {
IgnoreImages []*regexp.Regexp
IgnorePullPolicyAlways bool
ProxyPort int
decoder *admission.Decoder
Decoder *admission.Decoder
}

type PodInitializer struct {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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{
Expand Down
15 changes: 0 additions & 15 deletions api/core/v1/pod_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions cmd/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 74c3680

Please sign in to comment.