@@ -80,7 +80,10 @@ type WebhookInstallOptions struct {
80
80
// ModifyWebhookDefinitions modifies webhook definitions by:
81
81
// - applying CABundle based on the provided tinyca
82
82
// - if webhook client config uses service spec, it's removed and replaced with direct url
83
- func (o * WebhookInstallOptions ) ModifyWebhookDefinitions (caData []byte ) error {
83
+ func (o * WebhookInstallOptions ) ModifyWebhookDefinitions () error {
84
+ caData := o .LocalServingCAData
85
+
86
+ // generate host port.
84
87
hostPort , err := o .generateHostPort ()
85
88
if err != nil {
86
89
return err
@@ -161,16 +164,14 @@ func (o *WebhookInstallOptions) generateHostPort() (string, error) {
161
164
// controller-runtime, where we need a random host-port & caData for webhook
162
165
// tests, but may be useful in similar scenarios.
163
166
func (o * WebhookInstallOptions ) PrepWithoutInstalling () error {
164
- hookCA , err := o .setupCA ()
165
- if err != nil {
167
+ if err := o .setupCA (); err != nil {
166
168
return err
167
169
}
168
170
if err := parseWebhook (o ); err != nil {
169
171
return err
170
172
}
171
173
172
- err = o .ModifyWebhookDefinitions (hookCA )
173
- if err != nil {
174
+ if err := o .ModifyWebhookDefinitions (); err != nil {
174
175
return err
175
176
}
176
177
@@ -179,8 +180,10 @@ func (o *WebhookInstallOptions) PrepWithoutInstalling() error {
179
180
180
181
// Install installs specified webhooks to the API server
181
182
func (o * WebhookInstallOptions ) Install (config * rest.Config ) error {
182
- if err := o .PrepWithoutInstalling (); err != nil {
183
- return err
183
+ if len (o .LocalServingCAData ) == 0 {
184
+ if err := o .PrepWithoutInstalling (); err != nil {
185
+ return err
186
+ }
184
187
}
185
188
186
189
if err := createWebhooks (config , o .MutatingWebhooks , o .ValidatingWebhooks ); err != nil {
@@ -269,38 +272,38 @@ func (p *webhookPoller) poll() (done bool, err error) {
269
272
}
270
273
271
274
// setupCA creates CA for testing and writes them to disk
272
- func (o * WebhookInstallOptions ) setupCA () ([] byte , error ) {
275
+ func (o * WebhookInstallOptions ) setupCA () error {
273
276
hookCA , err := integration .NewTinyCA ()
274
277
if err != nil {
275
- return nil , fmt .Errorf ("unable to set up webhook CA: %v" , err )
278
+ return fmt .Errorf ("unable to set up webhook CA: %v" , err )
276
279
}
277
280
278
281
names := []string {"localhost" , o .LocalServingHost , o .LocalServingHostExternalName }
279
282
hookCert , err := hookCA .NewServingCert (names ... )
280
283
if err != nil {
281
- return nil , fmt .Errorf ("unable to set up webhook serving certs: %v" , err )
284
+ return fmt .Errorf ("unable to set up webhook serving certs: %v" , err )
282
285
}
283
286
284
287
localServingCertsDir , err := ioutil .TempDir ("" , "envtest-serving-certs-" )
285
288
o .LocalServingCertDir = localServingCertsDir
286
289
if err != nil {
287
- return nil , fmt .Errorf ("unable to create directory for webhook serving certs: %v" , err )
290
+ return fmt .Errorf ("unable to create directory for webhook serving certs: %v" , err )
288
291
}
289
292
290
293
certData , keyData , err := hookCert .AsBytes ()
291
294
if err != nil {
292
- return nil , fmt .Errorf ("unable to marshal webhook serving certs: %v" , err )
295
+ return fmt .Errorf ("unable to marshal webhook serving certs: %v" , err )
293
296
}
294
297
295
298
if err := ioutil .WriteFile (filepath .Join (localServingCertsDir , "tls.crt" ), certData , 0640 ); err != nil {
296
- return nil , fmt .Errorf ("unable to write webhook serving cert to disk: %v" , err )
299
+ return fmt .Errorf ("unable to write webhook serving cert to disk: %v" , err )
297
300
}
298
301
if err := ioutil .WriteFile (filepath .Join (localServingCertsDir , "tls.key" ), keyData , 0640 ); err != nil {
299
- return nil , fmt .Errorf ("unable to write webhook serving key to disk: %v" , err )
302
+ return fmt .Errorf ("unable to write webhook serving key to disk: %v" , err )
300
303
}
301
304
302
305
o .LocalServingCAData = certData
303
- return certData , nil
306
+ return err
304
307
}
305
308
306
309
func createWebhooks (config * rest.Config , mutHooks []client.Object , valHooks []client.Object ) error {
0 commit comments