Skip to content

Commit 7a648a9

Browse files
committed
Clean up webhook server
This renames a few ambigously named fields in the webhook server, removes some extraneous fields and methods, and removes the unnecessary constructor for Server.
1 parent 805ef70 commit 7a648a9

File tree

3 files changed

+32
-56
lines changed

3 files changed

+32
-56
lines changed

example/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,14 @@ func main() {
9090
}
9191

9292
entryLog.Info("setting up webhook server")
93-
as, err := webhook.NewServer(webhook.ServerOptions{
93+
hookServer := &webhook.Server{
9494
Port: 9876,
9595
CertDir: "/tmp/cert",
96-
})
97-
if err != nil {
98-
entryLog.Error(err, "unable to create a new webhook server")
99-
os.Exit(1)
10096
}
101-
mgr.Add(as)
97+
mgr.Add(hookServer)
10298

10399
entryLog.Info("registering webhooks to the webhook server")
104-
err = as.Register(mutatingWebhook, validatingWebhook)
100+
err = hookServer.Register(mutatingWebhook, validatingWebhook)
105101
if err != nil {
106102
entryLog.Error(err, "unable to setup the admission server")
107103
os.Exit(1)

pkg/webhook/doc.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,21 @@ Build webhooks
3333
3434
Create a webhook server.
3535
36-
as, err := NewServer("baz-admission-server", mgr, ServerOptions{
36+
hookServer := &Server{
3737
CertDir: "/tmp/cert",
38-
})
39-
if err != nil {
40-
// handle error
4138
}
39+
mgr.Add(hookServer)
4240
4341
Register the webhooks in the server.
4442
45-
err = as.Register(webhook1, webhook2)
43+
err = hookServer.Register(webhook1, webhook2)
4644
if err != nil {
4745
// handle error
4846
}
4947
5048
Start the server by starting the manager
5149
52-
err := mrg.Start(signals.SetupSignalHandler())
50+
err := mgr.Start(signals.SetupSignalHandler())
5351
if err != nil {
5452
// handle error
5553
}

pkg/webhook/server.go

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const (
3434
keyName = "tls.key"
3535
)
3636

37-
// ServerOptions are options for configuring an admission webhook server.
38-
type ServerOptions struct {
39-
// Address that the server will listen on.
37+
// Server is an admission webhook server that can serve traffic and
38+
// generates related k8s resources for deploying.
39+
type Server struct {
40+
// Host is the address that the server will listen on.
4041
// Defaults to "" - all addresses.
4142
Host string
4243

@@ -50,22 +51,20 @@ type ServerOptions struct {
5051
// If using SecretCertWriter in Provisioner, the server will provision the certificate in a secret,
5152
// the user is responsible to mount the secret to the this location for the server to consume.
5253
CertDir string
53-
}
5454

55-
// Server is an admission webhook server that can serve traffic and
56-
// generates related k8s resources for deploying.
57-
type Server struct {
58-
// ServerOptions contains options for configuring the admission server.
59-
ServerOptions
55+
// TODO(directxman12): should we make the mux configurable?
6056

61-
sMux *http.ServeMux
62-
// registry maps a path to a http.Handler.
63-
registry map[string]http.Handler
57+
// webhookMux is the multiplexer that handles different webhooks.
58+
webhookMux *http.ServeMux
59+
// hooks keep track of all registered webhooks for dependency injection,
60+
// and to provide better panic messages on duplicate webhook registration.
61+
webhooks map[string]Webhook
6462

6563
// setFields allows injecting dependencies from an external source
6664
setFields inject.Func
6765

68-
once sync.Once
66+
// defaultingOnce ensures that the default fields are only ever set once.
67+
defaultingOnce sync.Once
6968
}
7069

7170
// Webhook defines the basics that a webhook should support.
@@ -80,66 +79,49 @@ type Webhook interface {
8079
Validate() error
8180
}
8281

83-
// NewServer creates a new admission webhook server.
84-
func NewServer(options ServerOptions) (*Server, error) {
85-
as := &Server{
86-
sMux: http.NewServeMux(),
87-
registry: map[string]http.Handler{},
88-
ServerOptions: options,
89-
}
90-
91-
return as, nil
92-
}
82+
// setDefaults does defaulting for the Server.
83+
func (s *Server) setDefaults() {
84+
s.webhooks = map[string]Webhook{}
85+
s.webhookMux = http.NewServeMux()
9386

94-
// setDefault does defaulting for the Server.
95-
func (s *Server) setDefault() {
96-
if s.registry == nil {
97-
s.registry = map[string]http.Handler{}
98-
}
99-
if s.sMux == nil {
100-
s.sMux = http.NewServeMux()
101-
}
10287
if s.Port <= 0 {
10388
s.Port = 443
10489
}
10590
if len(s.CertDir) == 0 {
106-
s.CertDir = path.Join("k8s-webhook-server", "cert")
91+
s.CertDir = path.Join("/tmp", "k8s-webhook-server", "serving-certs")
10792
}
10893
}
10994

11095
// Register validates and registers webhook(s) in the server
11196
func (s *Server) Register(webhooks ...Webhook) error {
97+
// TODO(directxman12): is it really worth the ergonomics hit to make this a catchable error?
98+
// In most cases you'll probably just bail immediately anyway.
11299
for i, webhook := range webhooks {
113100
// validate the webhook before registering it.
114101
err := webhook.Validate()
115102
if err != nil {
116103
return err
117104
}
118105
// TODO(directxman12): call setfields if we've already started the server
119-
_, found := s.registry[webhook.GetPath()]
106+
_, found := s.webhooks[webhook.GetPath()]
120107
if found {
121108
return fmt.Errorf("can't register duplicate path: %v", webhook.GetPath())
122109
}
123-
s.registry[webhook.GetPath()] = webhooks[i]
124-
s.sMux.Handle(webhook.GetPath(), webhook)
110+
s.webhooks[webhook.GetPath()] = webhooks[i]
111+
s.webhookMux.Handle(webhook.GetPath(), webhook)
125112
}
126113

127114
return nil
128115
}
129116

130-
// Handle registers a http.Handler for the given pattern.
131-
func (s *Server) Handle(pattern string, handler http.Handler) {
132-
s.sMux.Handle(pattern, handler)
133-
}
134-
135117
// Start runs the server.
136118
// It will install the webhook related resources depend on the server configuration.
137119
func (s *Server) Start(stop <-chan struct{}) error {
138-
s.once.Do(s.setDefault)
120+
s.defaultingOnce.Do(s.setDefaults)
139121

140122
// inject fields here as opposed to in Register so that we're certain to have our setFields
141123
// function available.
142-
for _, webhook := range s.registry {
124+
for _, webhook := range s.webhooks {
143125
if err := s.setFields(webhook); err != nil {
144126
return err
145127
}
@@ -161,7 +143,7 @@ func (s *Server) Start(stop <-chan struct{}) error {
161143
}
162144

163145
srv := &http.Server{
164-
Handler: s.sMux,
146+
Handler: s.webhookMux,
165147
}
166148

167149
idleConnsClosed := make(chan struct{})

0 commit comments

Comments
 (0)