@@ -269,23 +269,36 @@ func applyCustomResourceDefinitions(c *collector.Manifests, csv *operatorsv1alph
269
269
// applyWebhooks updates csv's webhookDefinitions with any mutating and validating webhooks in the collector.
270
270
func applyWebhooks (c * collector.Manifests , csv * operatorsv1alpha1.ClusterServiceVersion ) {
271
271
webhookDescriptions := []operatorsv1alpha1.WebhookDescription {}
272
+
272
273
for _ , webhook := range c .ValidatingWebhooks {
273
- depName , serviceName := findMatchingDeploymentAndServiceForWebhook (c , webhook .ClientConfig )
274
- if serviceName == "" && depName == "" {
274
+ var validatingServiceName string
275
+ depName , svc := findMatchingDeploymentAndServiceForWebhook (c , webhook .ClientConfig )
276
+
277
+ if svc != nil {
278
+ validatingServiceName = svc .GetName ()
279
+ }
280
+
281
+ if validatingServiceName == "" && depName == "" {
275
282
log .Infof ("No service found for validating webhook %q" , webhook .Name )
276
283
} else if depName == "" {
277
- log .Infof ("No deployment is selected by service %q for validating webhook %q" , serviceName , webhook .Name )
284
+ log .Infof ("No deployment is selected by service %q for validating webhook %q" , validatingServiceName , webhook .Name )
278
285
}
279
- webhookDescriptions = append (webhookDescriptions , validatingToWebhookDescription (webhook , depName ))
286
+ webhookDescriptions = append (webhookDescriptions , validatingToWebhookDescription (webhook , depName , svc ))
280
287
}
281
288
for _ , webhook := range c .MutatingWebhooks {
282
- depName , serviceName := findMatchingDeploymentAndServiceForWebhook (c , webhook .ClientConfig )
283
- if serviceName == "" && depName == "" {
289
+ var mutatingServiceName string
290
+ depName , svc := findMatchingDeploymentAndServiceForWebhook (c , webhook .ClientConfig )
291
+
292
+ if svc != nil {
293
+ mutatingServiceName = svc .GetName ()
294
+ }
295
+
296
+ if mutatingServiceName == "" && depName == "" {
284
297
log .Infof ("No service found for mutating webhook %q" , webhook .Name )
285
298
} else if depName == "" {
286
- log .Infof ("No deployment is selected by service %q for mutating webhook %q" , serviceName , webhook .Name )
299
+ log .Infof ("No deployment is selected by service %q for mutating webhook %q" , mutatingServiceName , webhook .Name )
287
300
}
288
- webhookDescriptions = append (webhookDescriptions , mutatingToWebhookDescription (webhook , depName ))
301
+ webhookDescriptions = append (webhookDescriptions , mutatingToWebhookDescription (webhook , depName , svc ))
289
302
}
290
303
csv .Spec .WebhookDefinitions = webhookDescriptions
291
304
}
@@ -294,7 +307,7 @@ func applyWebhooks(c *collector.Manifests, csv *operatorsv1alpha1.ClusterService
294
307
var defaultAdmissionReviewVersions = []string {"v1beta1" }
295
308
296
309
// validatingToWebhookDescription transforms webhook into a WebhookDescription.
297
- func validatingToWebhookDescription (webhook admissionregv1.ValidatingWebhook , depName string ) operatorsv1alpha1.WebhookDescription {
310
+ func validatingToWebhookDescription (webhook admissionregv1.ValidatingWebhook , depName string , ws * corev1. Service ) operatorsv1alpha1.WebhookDescription {
298
311
description := operatorsv1alpha1.WebhookDescription {
299
312
Type : operatorsv1alpha1 .ValidatingAdmissionWebhook ,
300
313
GenerateName : webhook .Name ,
@@ -315,8 +328,18 @@ func validatingToWebhookDescription(webhook admissionregv1.ValidatingWebhook, de
315
328
}
316
329
317
330
if serviceRef := webhook .ClientConfig .Service ; serviceRef != nil {
331
+ var webhookServiceRefPort int32 = 443
318
332
if serviceRef .Port != nil {
319
- description .ContainerPort = * serviceRef .Port
333
+ webhookServiceRefPort = * serviceRef .Port
334
+ }
335
+ if ws != nil {
336
+ for _ , port := range ws .Spec .Ports {
337
+ if webhookServiceRefPort == port .Port {
338
+ description .ContainerPort = port .Port
339
+ description .TargetPort = & port .TargetPort
340
+ break
341
+ }
342
+ }
320
343
}
321
344
description .DeploymentName = depName
322
345
if description .DeploymentName == "" {
@@ -328,7 +351,7 @@ func validatingToWebhookDescription(webhook admissionregv1.ValidatingWebhook, de
328
351
}
329
352
330
353
// mutatingToWebhookDescription transforms webhook into a WebhookDescription.
331
- func mutatingToWebhookDescription (webhook admissionregv1.MutatingWebhook , depName string ) operatorsv1alpha1.WebhookDescription {
354
+ func mutatingToWebhookDescription (webhook admissionregv1.MutatingWebhook , depName string , ws * corev1. Service ) operatorsv1alpha1.WebhookDescription {
332
355
description := operatorsv1alpha1.WebhookDescription {
333
356
Type : operatorsv1alpha1 .MutatingAdmissionWebhook ,
334
357
GenerateName : webhook .Name ,
@@ -350,8 +373,18 @@ func mutatingToWebhookDescription(webhook admissionregv1.MutatingWebhook, depNam
350
373
}
351
374
352
375
if serviceRef := webhook .ClientConfig .Service ; serviceRef != nil {
376
+ var webhookServiceRefPort int32 = 443
353
377
if serviceRef .Port != nil {
354
- description .ContainerPort = * serviceRef .Port
378
+ webhookServiceRefPort = * serviceRef .Port
379
+ }
380
+ if ws != nil {
381
+ for _ , port := range ws .Spec .Ports {
382
+ if webhookServiceRefPort == port .Port {
383
+ description .ContainerPort = port .Port
384
+ description .TargetPort = & port .TargetPort
385
+ break
386
+ }
387
+ }
355
388
}
356
389
description .DeploymentName = depName
357
390
if description .DeploymentName == "" {
@@ -365,15 +398,14 @@ func mutatingToWebhookDescription(webhook admissionregv1.MutatingWebhook, depNam
365
398
// findMatchingDeploymentAndServiceForWebhook matches a Service to a webhook's client config (if it uses a service)
366
399
// then matches that Service to a Deployment by comparing label selectors (if the Service uses label selectors).
367
400
// The names of both Service and Deployment are returned if found.
368
- func findMatchingDeploymentAndServiceForWebhook (c * collector.Manifests , wcc admissionregv1.WebhookClientConfig ) (depName , serviceName string ) {
401
+ func findMatchingDeploymentAndServiceForWebhook (c * collector.Manifests , wcc admissionregv1.WebhookClientConfig ) (depName string , ws * corev1. Service ) {
369
402
// Return if a service reference is not specified, since a URL will be in that case.
370
403
if wcc .Service == nil {
371
404
return
372
405
}
373
406
374
407
// Find the matching service, if any. The webhook server may be externally managed
375
408
// if no service is created by the operator.
376
- var ws * corev1.Service
377
409
for i , service := range c .Services {
378
410
if service .GetName () == wcc .Service .Name {
379
411
ws = & c .Services [i ]
@@ -383,7 +415,6 @@ func findMatchingDeploymentAndServiceForWebhook(c *collector.Manifests, wcc admi
383
415
if ws == nil {
384
416
return
385
417
}
386
- serviceName = ws .GetName ()
387
418
388
419
// Only ExternalName-type services cannot have selectors.
389
420
if ws .Spec .Type == corev1 .ServiceTypeExternalName {
@@ -416,7 +447,7 @@ func findMatchingDeploymentAndServiceForWebhook(c *collector.Manifests, wcc admi
416
447
}
417
448
}
418
449
419
- return depName , serviceName
450
+ return depName , ws
420
451
}
421
452
422
453
// applyCustomResources updates csv's "alm-examples" annotation with the
0 commit comments