@@ -34,8 +34,8 @@ import (
34
34
)
35
35
36
36
type generatorOptions struct {
37
- // registry maps a path to a http.Handler .
38
- registry map [string ]Webhook
37
+ // webhooks maps a path to a webhoook .
38
+ webhooks map [string ]webhook
39
39
40
40
// port is the port number that the server will serve.
41
41
// It will be defaulted to 443 if unspecified.
@@ -51,17 +51,16 @@ type generatorOptions struct {
51
51
52
52
// secret is the location for storing the certificate for the admission server.
53
53
// The server should have permission to create a secret in the namespace.
54
- // This is optional.
55
- secret * apitypes.NamespacedName // nolint: structcheck
54
+ secret * apitypes.NamespacedName
56
55
57
56
// service is a k8s service fronting the webhook server pod(s).
58
- // This field is optional. But one and only one of service and host need to be set.
57
+ // One and only one of service and host can be set.
59
58
// This maps to field .Webhooks.ClientConfig.Service
60
59
// https://github.com/kubernetes/api/blob/183f3326a9353bd6d41430fc80f96259331d029c/admissionregistration/v1beta1/types.go#L260
61
60
service * service
62
61
// host is the host name of .Webhooks.ClientConfig.URL
63
62
// https://github.com/kubernetes/api/blob/183f3326a9353bd6d41430fc80f96259331d029c/admissionregistration/v1beta1/types.go#L250
64
- // This field is optional. But one and only one of service and host need to be set.
63
+ // One and only one of service and host can be set.
65
64
// If neither service nor host is unspecified, host will be defaulted to "localhost".
66
65
host * string
67
66
}
@@ -79,8 +78,8 @@ type service struct {
79
78
80
79
// setDefault does defaulting for the generatorOptions.
81
80
func (o * generatorOptions ) setDefault () {
82
- if o .registry == nil {
83
- o .registry = map [string ]Webhook {}
81
+ if o .webhooks == nil {
82
+ o .webhooks = map [string ]webhook {}
84
83
}
85
84
if o .port <= 0 {
86
85
o .port = 443
@@ -117,71 +116,16 @@ func (o *generatorOptions) Generate() ([]runtime.Object, error) {
117
116
return objects , nil
118
117
}
119
118
120
- func (o * generatorOptions ) getClientConfig () (* admissionregistration.WebhookClientConfig , error ) {
121
- if o .host != nil && o .service != nil {
122
- return nil , errors .New ("URL and service can't be set at the same time" )
123
- }
124
- cc := & admissionregistration.WebhookClientConfig {
125
- // Put an non-empty and not harmful CABundle here.
126
- // Not doing this will cause the field
127
- CABundle : []byte (`\n` ),
128
- }
129
- if o .host != nil {
130
- u := url.URL {
131
- Scheme : "https" ,
132
- Host : net .JoinHostPort (* o .host , strconv .Itoa (int (o .port ))),
133
- }
134
- urlString := u .String ()
135
- cc .URL = & urlString
136
- }
137
- if o .service != nil {
138
- cc .Service = & admissionregistration.ServiceReference {
139
- Name : o .service .name ,
140
- Namespace : o .service .namespace ,
141
- // Path will be set later
142
- }
143
- }
144
- return cc , nil
145
- }
146
-
147
- // getClientConfigWithPath constructs a WebhookClientConfig based on the server generatorOptions.
148
- // It will use path to the set the path in WebhookClientConfig.
149
- func (o * generatorOptions ) getClientConfigWithPath (path string ) (* admissionregistration.WebhookClientConfig , error ) {
150
- cc , err := o .getClientConfig ()
151
- if err != nil {
152
- return nil , err
153
- }
154
- return cc , setPath (cc , path )
155
- }
156
-
157
- // setPath sets the path in the WebhookClientConfig.
158
- func setPath (cc * admissionregistration.WebhookClientConfig , path string ) error {
159
- if cc .URL != nil {
160
- u , err := url .Parse (* cc .URL )
161
- if err != nil {
162
- return err
163
- }
164
- u .Path = path
165
- urlString := u .String ()
166
- cc .URL = & urlString
167
- }
168
- if cc .Service != nil {
169
- cc .Service .Path = & path
170
- }
171
- return nil
172
- }
173
-
174
- // whConfigs creates a mutatingWebhookConfiguration and(or) a validatingWebhookConfiguration based on registry.
175
- // For the same type of webhook configuration, it generates a webhook entry per endpoint.
119
+ // whConfigs creates a mutatingWebhookConfiguration and(or) a validatingWebhookConfiguration.
176
120
func (o * generatorOptions ) whConfigs () ([]runtime.Object , error ) {
177
- for _ , webhook := range o .registry {
121
+ for _ , webhook := range o .webhooks {
178
122
if err := webhook .Validate (); err != nil {
179
123
return nil , err
180
124
}
181
125
}
182
126
183
127
objs := []runtime.Object {}
184
- mutatingWH , err := o .mutatingWHConfigs ()
128
+ mutatingWH , err := o .mutatingWHConfig ()
185
129
if err != nil {
186
130
return nil , err
187
131
}
@@ -198,15 +142,16 @@ func (o *generatorOptions) whConfigs() ([]runtime.Object, error) {
198
142
return objs , nil
199
143
}
200
144
201
- func (o * generatorOptions ) mutatingWHConfigs () (runtime.Object , error ) {
145
+ // mutatingWHConfig creates mutatingWebhookConfiguration.
146
+ func (o * generatorOptions ) mutatingWHConfig () (runtime.Object , error ) {
202
147
mutatingWebhooks := []v1beta1.Webhook {}
203
- for path , webhook := range o .registry {
204
- if webhook .GetType () != webhookTypeMutating {
148
+ for path , webhook := range o .webhooks {
149
+ if webhook .GetType () != mutatingWebhook {
205
150
continue
206
151
}
207
152
208
- admissionWebhook := webhook .(* admissionWebhook )
209
- wh , err := o .admissionWebhook (path , admissionWebhook )
153
+ aw := webhook .(* admissionWebhook )
154
+ wh , err := o .admissionWebhook (path , aw )
210
155
if err != nil {
211
156
return nil , err
212
157
}
@@ -226,10 +171,9 @@ func (o *generatorOptions) mutatingWHConfigs() (runtime.Object, error) {
226
171
ObjectMeta : metav1.ObjectMeta {
227
172
Name : o .mutatingWebhookConfigName ,
228
173
Annotations : map [string ]string {
229
- // The format is "namespace/secret-name"
230
174
// This annotation will be understood by cert-manager.
231
175
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
232
- "alpha.admissionwebhook.kubebuilder .io/ca-secret-name " : o . secret . String () ,
176
+ "alpha.admissionwebhook.cert-manager .io" : "true" ,
233
177
},
234
178
},
235
179
Webhooks : mutatingWebhooks ,
@@ -240,9 +184,9 @@ func (o *generatorOptions) mutatingWHConfigs() (runtime.Object, error) {
240
184
241
185
func (o * generatorOptions ) validatingWHConfigs () (runtime.Object , error ) {
242
186
validatingWebhooks := []v1beta1.Webhook {}
243
- for path , webhook := range o .registry {
187
+ for path , webhook := range o .webhooks {
244
188
var aw * admissionWebhook
245
- if webhook .GetType () != webhookTypeValidating {
189
+ if webhook .GetType () != validatingWebhook {
246
190
continue
247
191
}
248
192
@@ -267,10 +211,9 @@ func (o *generatorOptions) validatingWHConfigs() (runtime.Object, error) {
267
211
ObjectMeta : metav1.ObjectMeta {
268
212
Name : o .validatingWebhookConfigName ,
269
213
Annotations : map [string ]string {
270
- // The format is "namespace/secret-name"
271
214
// This annotation will be understood by cert-manager.
272
215
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
273
- "alpha.admissionwebhook.kubebuilder .io/ca-secret-name " : o . secret . String () ,
216
+ "alpha.admissionwebhook.cert-manager .io" : "true" ,
274
217
},
275
218
},
276
219
Webhooks : validatingWebhooks ,
@@ -305,6 +248,60 @@ func (o *generatorOptions) admissionWebhook(path string, wh *admissionWebhook) (
305
248
return webhook , nil
306
249
}
307
250
251
+ // getClientConfigWithPath constructs a WebhookClientConfig based on the server generatorOptions.
252
+ // It will use path to the set the path in WebhookClientConfig.
253
+ func (o * generatorOptions ) getClientConfigWithPath (path string ) (* admissionregistration.WebhookClientConfig , error ) {
254
+ cc , err := o .getClientConfig ()
255
+ if err != nil {
256
+ return nil , err
257
+ }
258
+ return cc , setPath (cc , path )
259
+ }
260
+
261
+ func (o * generatorOptions ) getClientConfig () (* admissionregistration.WebhookClientConfig , error ) {
262
+ if o .host != nil && o .service != nil {
263
+ return nil , errors .New ("URL and service can't be set at the same time" )
264
+ }
265
+ cc := & admissionregistration.WebhookClientConfig {
266
+ // Put an non-empty and not harmful CABundle here.
267
+ // Not doing this will cause the field
268
+ CABundle : []byte (`\n` ),
269
+ }
270
+ if o .host != nil {
271
+ u := url.URL {
272
+ Scheme : "https" ,
273
+ Host : net .JoinHostPort (* o .host , strconv .Itoa (int (o .port ))),
274
+ }
275
+ urlString := u .String ()
276
+ cc .URL = & urlString
277
+ }
278
+ if o .service != nil {
279
+ cc .Service = & admissionregistration.ServiceReference {
280
+ Name : o .service .name ,
281
+ Namespace : o .service .namespace ,
282
+ // Path will be set later
283
+ }
284
+ }
285
+ return cc , nil
286
+ }
287
+
288
+ // setPath sets the path in the WebhookClientConfig.
289
+ func setPath (cc * admissionregistration.WebhookClientConfig , path string ) error {
290
+ if cc .URL != nil {
291
+ u , err := url .Parse (* cc .URL )
292
+ if err != nil {
293
+ return err
294
+ }
295
+ u .Path = path
296
+ urlString := u .String ()
297
+ cc .URL = & urlString
298
+ }
299
+ if cc .Service != nil {
300
+ cc .Service .Path = & path
301
+ }
302
+ return nil
303
+ }
304
+
308
305
// getService creates a corev1.Service object fronting the admission server.
309
306
func (o * generatorOptions ) getService () runtime.Object {
310
307
if o .service == nil {
@@ -322,7 +319,7 @@ func (o *generatorOptions) getService() runtime.Object {
322
319
// Secret here only need name, since it will be in the same namespace as the service.
323
320
// This annotation will be understood by cert-manager.
324
321
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
325
- "alpha.service.kubebuilder .io/serving-cert-secret-name" : o .secret .Name ,
322
+ "alpha.service.cert-manager .io/serving-cert-secret-name" : o .secret .Name ,
326
323
},
327
324
},
328
325
Spec : corev1.ServiceSpec {
0 commit comments