Skip to content

Commit

Permalink
🐛 use mgr.SetFields() for webhook server and implement injector inter…
Browse files Browse the repository at this point in the history
…face for webhooks
  • Loading branch information
Mengqi Yu committed Feb 12, 2019
1 parent d169520 commit a4ef929
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
21 changes: 4 additions & 17 deletions pkg/webhook/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

admissionv1beta1 "k8s.io/api/admission/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 @@ -210,24 +209,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
47 changes: 14 additions & 33 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"strconv"
"sync"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
)
Expand All @@ -48,11 +46,6 @@ type ServerOptions struct {
// If using SecretCertWriter in Provisioner, the server will provision the certificate in a secret,
// the user is responsible to mount the secret to the this location for the server to consume.
CertDir string

// Client is a client defined in controller-runtime instead of a client-go client.
// It knows how to talk to a kubernetes cluster.
// Client will be injected by the manager if not set.
Client client.Client
}

// Server is an admission webhook server that can serve traffic and
Expand All @@ -71,6 +64,9 @@ type Server struct {
// manager is the manager that this webhook server will be registered.
manager manager.Manager

// setFields is used to inject dependencies into webhooks
setFields func(i interface{}) error

// err will be non-nil if there is an error occur during initialization.
err error

Expand Down Expand Up @@ -120,19 +116,6 @@ func (s *Server) setDefault() {
if len(s.CertDir) == 0 {
s.CertDir = path.Join("k8s-webhook-server", "cert")
}

if s.Client == nil {
cfg, err := config.GetConfig()
if err != nil {
s.err = err
return
}
s.Client, err = client.New(cfg, client.Options{})
if err != nil {
s.err = err
return
}
}
}

// Register validates and registers webhook(s) in the server
Expand Down Expand Up @@ -160,7 +143,13 @@ func (s *Server) Complete() error {
if s.err != nil {
return s.err
}
// TODO(mengqiy): inject dependencies into each http.Handler

for path := range s.registry {
// Inject dependencies into each http.Handler
if err := s.setFields(s.registry[path]); err != nil {
return err
}
}
return s.manager.Add(s)
}

Expand Down Expand Up @@ -214,18 +203,10 @@ func (s *Server) Start(stop <-chan struct{}) error {
return nil
}

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

// InjectClient injects the client into the server
func (s *Server) InjectClient(c client.Client) error {
s.Client = c
for path := range s.registry {
// TODO(mengqiy): remove this in PR #316
if wh, ok := s.registry[path].(Webhook); ok {
if _, err := inject.ClientInto(c, wh.Handler()); err != nil {
return err
}
}
}
// InjectFunc injects dependencies into the handlers.
func (s *Server) InjectFunc(f inject.Func) error {
s.setFields = f
return nil
}

0 comments on commit a4ef929

Please sign in to comment.