Skip to content

Commit

Permalink
🐛 use mgr.SetFields() for webhook server and implement injector
Browse files Browse the repository at this point in the history
interface for webhooks
  • Loading branch information
Mengqi Yu committed Feb 4, 2019
1 parent abf0c07 commit 859819e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
21 changes: 4 additions & 17 deletions pkg/webhook/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
admissionv1beta1 "k8s.io/api/admission/v1beta1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
Expand Down Expand Up @@ -234,24 +233,12 @@ func (w *Webhook) Validate() error {
return nil
}

var _ inject.Client = &Webhook{}
var _ inject.Injector = &Webhook{}

// InjectClient injects the client into the handlers
func (w *Webhook) InjectClient(c client.Client) error {
// InjectFunc injects dependencies into the handlers.
func (w *Webhook) InjectFunc(f inject.Func) error {
for _, handler := range w.Handlers {
if _, err := inject.ClientInto(c, handler); err != nil {
return err
}
}
return nil
}

var _ inject.Decoder = &Webhook{}

// InjectDecoder injects the decoder into the handlers
func (w *Webhook) InjectDecoder(d atypes.Decoder) error {
for _, handler := range w.Handlers {
if _, err := inject.DecoderInto(d, handler); err != nil {
if err := f(handler); err != nil {
return err
}
}
Expand Down
18 changes: 5 additions & 13 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert"
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert/writer"
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
Expand Down Expand Up @@ -181,6 +180,11 @@ func (s *Server) Register(webhooks ...Webhook) error {
}
s.registry[webhook.GetPath()] = webhooks[i]
s.sMux.Handle(webhook.GetPath(), webhook.Handler())

// Inject dependencies into each webhook
if err := s.manager.SetFields(webhooks[i]); err != nil {
return err
}
}

// Lazily add Server to manager.
Expand Down Expand Up @@ -294,15 +298,3 @@ func (s *Server) InjectClient(c client.Client) error {
}
return nil
}

var _ inject.Decoder = &Server{}

// InjectDecoder injects the client into the server
func (s *Server) InjectDecoder(d atypes.Decoder) error {
for _, wh := range s.registry {
if _, err := inject.DecoderInto(d, wh.Handler()); err != nil {
return err
}
}
return nil
}

0 comments on commit 859819e

Please sign in to comment.