@@ -34,9 +34,10 @@ const (
34
34
keyName = "tls.key"
35
35
)
36
36
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.
40
41
// Defaults to "" - all addresses.
41
42
Host string
42
43
@@ -50,22 +51,20 @@ type ServerOptions struct {
50
51
// If using SecretCertWriter in Provisioner, the server will provision the certificate in a secret,
51
52
// the user is responsible to mount the secret to the this location for the server to consume.
52
53
CertDir string
53
- }
54
54
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?
60
56
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
64
62
65
63
// setFields allows injecting dependencies from an external source
66
64
setFields inject.Func
67
65
68
- once sync.Once
66
+ // defaultingOnce ensures that the default fields are only ever set once.
67
+ defaultingOnce sync.Once
69
68
}
70
69
71
70
// Webhook defines the basics that a webhook should support.
@@ -80,66 +79,49 @@ type Webhook interface {
80
79
Validate () error
81
80
}
82
81
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 ()
93
86
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
- }
102
87
if s .Port <= 0 {
103
88
s .Port = 443
104
89
}
105
90
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 " )
107
92
}
108
93
}
109
94
110
95
// Register validates and registers webhook(s) in the server
111
96
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.
112
99
for i , webhook := range webhooks {
113
100
// validate the webhook before registering it.
114
101
err := webhook .Validate ()
115
102
if err != nil {
116
103
return err
117
104
}
118
105
// TODO(directxman12): call setfields if we've already started the server
119
- _ , found := s .registry [webhook .GetPath ()]
106
+ _ , found := s .webhooks [webhook .GetPath ()]
120
107
if found {
121
108
return fmt .Errorf ("can't register duplicate path: %v" , webhook .GetPath ())
122
109
}
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 )
125
112
}
126
113
127
114
return nil
128
115
}
129
116
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
-
135
117
// Start runs the server.
136
118
// It will install the webhook related resources depend on the server configuration.
137
119
func (s * Server ) Start (stop <- chan struct {}) error {
138
- s .once .Do (s .setDefault )
120
+ s .defaultingOnce .Do (s .setDefaults )
139
121
140
122
// inject fields here as opposed to in Register so that we're certain to have our setFields
141
123
// function available.
142
- for _ , webhook := range s .registry {
124
+ for _ , webhook := range s .webhooks {
143
125
if err := s .setFields (webhook ); err != nil {
144
126
return err
145
127
}
@@ -161,7 +143,7 @@ func (s *Server) Start(stop <-chan struct{}) error {
161
143
}
162
144
163
145
srv := & http.Server {
164
- Handler : s .sMux ,
146
+ Handler : s .webhookMux ,
165
147
}
166
148
167
149
idleConnsClosed := make (chan struct {})
0 commit comments