Skip to content

Commit d169520

Browse files
author
Mengqi Yu
committed
tweak
1 parent 0f3831c commit d169520

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

example/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ func main() {
102102
}
103103

104104
entryLog.Info("registering webhooks to the webhook server")
105-
err = as.Register(mutatingWebhook, validatingWebhook)
105+
as.Register(mutatingWebhook, validatingWebhook)
106+
err = as.Complete()
106107
if err != nil {
107-
entryLog.Error(err, "unable to register webhooks in the admission server")
108+
entryLog.Error(err, "unable to setup the admission server")
108109
os.Exit(1)
109110
}
110111

pkg/webhook/server.go

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package webhook
1919
import (
2020
"context"
2121
"crypto/tls"
22-
"fmt"
2322
"net"
2423
"net/http"
2524
"path"
@@ -30,7 +29,6 @@ import (
3029
"sigs.k8s.io/controller-runtime/pkg/client/config"
3130
"sigs.k8s.io/controller-runtime/pkg/manager"
3231
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
33-
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3432
)
3533

3634
const (
@@ -55,9 +53,6 @@ type ServerOptions struct {
5553
// It knows how to talk to a kubernetes cluster.
5654
// Client will be injected by the manager if not set.
5755
Client client.Client
58-
59-
// err will be non-nil if there is an error occur during initialization.
60-
err error // nolint: structcheck
6156
}
6257

6358
// Server is an admission webhook server that can serve traffic and
@@ -71,16 +66,21 @@ type Server struct {
7166

7267
sMux *http.ServeMux
7368
// registry maps a path to a http.Handler.
74-
registry map[string]Webhook
69+
registry map[string]http.Handler
7570

7671
// manager is the manager that this webhook server will be registered.
7772
manager manager.Manager
7873

74+
// err will be non-nil if there is an error occur during initialization.
75+
err error
76+
7977
once sync.Once
8078
}
8179

8280
// Webhook defines the basics that a webhook should support.
8381
type Webhook interface {
82+
http.Handler
83+
8484
// GetPath returns the path that the webhook registered.
8585
GetPath() string
8686
// Handler returns a http.Handler for the webhook.
@@ -95,7 +95,7 @@ func NewServer(name string, mgr manager.Manager, options ServerOptions) (*Server
9595
as := &Server{
9696
Name: name,
9797
sMux: http.NewServeMux(),
98-
registry: map[string]Webhook{},
98+
registry: map[string]http.Handler{},
9999
ServerOptions: options,
100100
manager: mgr,
101101
}
@@ -109,7 +109,7 @@ func (s *Server) setDefault() {
109109
s.Name = "default-k8s-webhook-server"
110110
}
111111
if s.registry == nil {
112-
s.registry = map[string]Webhook{}
112+
s.registry = map[string]http.Handler{}
113113
}
114114
if s.sMux == nil {
115115
s.sMux = http.DefaultServeMux
@@ -136,31 +136,34 @@ func (s *Server) setDefault() {
136136
}
137137

138138
// Register validates and registers webhook(s) in the server
139-
func (s *Server) Register(webhooks ...Webhook) error {
139+
func (s *Server) Register(webhooks ...Webhook) {
140140
for i, webhook := range webhooks {
141141
// validate the webhook before registering it.
142142
err := webhook.Validate()
143143
if err != nil {
144-
return err
145-
}
146-
_, found := s.registry[webhook.GetPath()]
147-
if found {
148-
return fmt.Errorf("can't register duplicate path: %v", webhook.GetPath())
144+
s.err = err
145+
return
149146
}
150-
s.registry[webhook.GetPath()] = webhooks[i]
147+
// Handle actually ensures that no duplicate paths are registered.
151148
s.sMux.Handle(webhook.GetPath(), webhook.Handler())
149+
s.registry[webhook.GetPath()] = webhooks[i]
152150
}
153-
154-
// Lazily add Server to manager.
155-
// Because the all webhook handlers to be in place, so we can inject the things they need.
156-
return s.manager.Add(s)
157151
}
158152

159153
// Handle registers a http.Handler for the given pattern.
160154
func (s *Server) Handle(pattern string, handler http.Handler) {
161155
s.sMux.Handle(pattern, handler)
162156
}
163157

158+
// Complete must be called to complete the server setup
159+
func (s *Server) Complete() error {
160+
if s.err != nil {
161+
return s.err
162+
}
163+
// TODO(mengqiy): inject dependencies into each http.Handler
164+
return s.manager.Add(s)
165+
}
166+
164167
var _ manager.Runnable = &Server{}
165168

166169
// Start runs the server.
@@ -216,21 +219,12 @@ var _ inject.Client = &Server{}
216219
// InjectClient injects the client into the server
217220
func (s *Server) InjectClient(c client.Client) error {
218221
s.Client = c
219-
for _, wh := range s.registry {
220-
if _, err := inject.ClientInto(c, wh.Handler()); err != nil {
221-
return err
222-
}
223-
}
224-
return nil
225-
}
226-
227-
var _ inject.Decoder = &Server{}
228-
229-
// InjectDecoder injects the decoder into the server
230-
func (s *Server) InjectDecoder(d atypes.Decoder) error {
231-
for _, wh := range s.registry {
232-
if _, err := inject.DecoderInto(d, wh.Handler()); err != nil {
233-
return err
222+
for path := range s.registry {
223+
// TODO(mengqiy): remove this in PR #316
224+
if wh, ok := s.registry[path].(Webhook); ok {
225+
if _, err := inject.ClientInto(c, wh.Handler()); err != nil {
226+
return err
227+
}
234228
}
235229
}
236230
return nil

0 commit comments

Comments
 (0)