@@ -269,23 +269,36 @@ func applyCustomResourceDefinitions(c *collector.Manifests, csv *operatorsv1alph
269269// applyWebhooks updates csv's webhookDefinitions with any mutating and validating webhooks in the collector.
270270func applyWebhooks (c * collector.Manifests , csv * operatorsv1alpha1.ClusterServiceVersion ) {
271271 webhookDescriptions := []operatorsv1alpha1.WebhookDescription {}
272+
272273 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 == "" {
275282 log .Infof ("No service found for validating webhook %q" , webhook .Name )
276283 } 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 )
278285 }
279- webhookDescriptions = append (webhookDescriptions , validatingToWebhookDescription (webhook , depName ))
286+ webhookDescriptions = append (webhookDescriptions , validatingToWebhookDescription (webhook , depName , svc ))
280287 }
281288 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 == "" {
284297 log .Infof ("No service found for mutating webhook %q" , webhook .Name )
285298 } 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 )
287300 }
288- webhookDescriptions = append (webhookDescriptions , mutatingToWebhookDescription (webhook , depName ))
301+ webhookDescriptions = append (webhookDescriptions , mutatingToWebhookDescription (webhook , depName , svc ))
289302 }
290303 csv .Spec .WebhookDefinitions = webhookDescriptions
291304}
@@ -294,7 +307,7 @@ func applyWebhooks(c *collector.Manifests, csv *operatorsv1alpha1.ClusterService
294307var defaultAdmissionReviewVersions = []string {"v1beta1" }
295308
296309// 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 {
298311 description := operatorsv1alpha1.WebhookDescription {
299312 Type : operatorsv1alpha1 .ValidatingAdmissionWebhook ,
300313 GenerateName : webhook .Name ,
@@ -315,8 +328,18 @@ func validatingToWebhookDescription(webhook admissionregv1.ValidatingWebhook, de
315328 }
316329
317330 if serviceRef := webhook .ClientConfig .Service ; serviceRef != nil {
331+ var webhookServiceRefPort int32 = 443
318332 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+ }
320343 }
321344 description .DeploymentName = depName
322345 if description .DeploymentName == "" {
@@ -328,7 +351,7 @@ func validatingToWebhookDescription(webhook admissionregv1.ValidatingWebhook, de
328351}
329352
330353// 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 {
332355 description := operatorsv1alpha1.WebhookDescription {
333356 Type : operatorsv1alpha1 .MutatingAdmissionWebhook ,
334357 GenerateName : webhook .Name ,
@@ -350,8 +373,18 @@ func mutatingToWebhookDescription(webhook admissionregv1.MutatingWebhook, depNam
350373 }
351374
352375 if serviceRef := webhook .ClientConfig .Service ; serviceRef != nil {
376+ var webhookServiceRefPort int32 = 443
353377 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+ }
355388 }
356389 description .DeploymentName = depName
357390 if description .DeploymentName == "" {
@@ -365,15 +398,14 @@ func mutatingToWebhookDescription(webhook admissionregv1.MutatingWebhook, depNam
365398// findMatchingDeploymentAndServiceForWebhook matches a Service to a webhook's client config (if it uses a service)
366399// then matches that Service to a Deployment by comparing label selectors (if the Service uses label selectors).
367400// 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 ) {
369402 // Return if a service reference is not specified, since a URL will be in that case.
370403 if wcc .Service == nil {
371404 return
372405 }
373406
374407 // Find the matching service, if any. The webhook server may be externally managed
375408 // if no service is created by the operator.
376- var ws * corev1.Service
377409 for i , service := range c .Services {
378410 if service .GetName () == wcc .Service .Name {
379411 ws = & c .Services [i ]
@@ -383,7 +415,6 @@ func findMatchingDeploymentAndServiceForWebhook(c *collector.Manifests, wcc admi
383415 if ws == nil {
384416 return
385417 }
386- serviceName = ws .GetName ()
387418
388419 // Only ExternalName-type services cannot have selectors.
389420 if ws .Spec .Type == corev1 .ServiceTypeExternalName {
@@ -416,7 +447,7 @@ func findMatchingDeploymentAndServiceForWebhook(c *collector.Manifests, wcc admi
416447 }
417448 }
418449
419- return depName , serviceName
450+ return depName , ws
420451}
421452
422453// applyCustomResources updates csv's "alm-examples" annotation with the
0 commit comments