@@ -85,7 +85,7 @@ func (c *KubernetesDefaultRouter) GetRoutes(_ *flaggerv1.Canary) (primaryRoute i
8585 return 0 , 0 , nil
8686}
8787
88- func (c * KubernetesDefaultRouter ) reconcileService (canary * flaggerv1.Canary , name string , podSelector string , metadata * flaggerv1. CustomMetadata ) error {
88+ func (c * KubernetesDefaultRouter ) getApexServicePort (canary * flaggerv1.Canary ) corev1. ServicePort {
8989 portName := canary .Spec .Service .PortName
9090 if portName == "" {
9191 portName = "http"
@@ -100,28 +100,23 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
100100 targetPort = canary .Spec .Service .TargetPort
101101 }
102102
103- // set pod selector and apex port
104- svcSpec := corev1.ServiceSpec {
105- Type : corev1 .ServiceTypeClusterIP ,
106- Selector : map [string ]string {c .labelSelector : podSelector },
107- Ports : []corev1.ServicePort {
108- {
109- Name : portName ,
110- Protocol : corev1 .ProtocolTCP ,
111- Port : canary .Spec .Service .Port ,
112- TargetPort : targetPort ,
113- },
114- },
115- }
116- if canary .Spec .Service .Headless {
117- svcSpec .ClusterIP = "None"
103+ cp := corev1.ServicePort {
104+ Name : portName ,
105+ Protocol : corev1 .ProtocolTCP ,
106+ Port : canary .Spec .Service .Port ,
107+ TargetPort : targetPort ,
118108 }
119109
120110 if v := canary .Spec .Service .AppProtocol ; v != "" {
121- svcSpec . Ports [ 0 ] .AppProtocol = & v
111+ cp .AppProtocol = & v
122112 }
123113
124- // set additional ports
114+ return cp
115+ }
116+
117+ func (c * KubernetesDefaultRouter ) getDiscoveryServicePorts (canary * flaggerv1.Canary ) []corev1.ServicePort {
118+ var ports []corev1.ServicePort
119+
125120 for n , p := range c .ports {
126121 cp := corev1.ServicePort {
127122 Name : n ,
@@ -133,9 +128,70 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
133128 },
134129 }
135130
136- svcSpec . Ports = append (svcSpec . Ports , cp )
131+ ports = append (ports , cp )
137132 }
138133
134+ return ports
135+ }
136+
137+ func (c * KubernetesDefaultRouter ) getExplicitServicePorts (canary * flaggerv1.Canary ) []corev1.ServicePort {
138+ var ports []corev1.ServicePort
139+
140+ for _ , p := range canary .Spec .Service .Ports {
141+ portName := p .PortName
142+
143+ targetPort := intstr.IntOrString {
144+ Type : intstr .Int ,
145+ IntVal : p .Port ,
146+ }
147+
148+ if p .TargetPort .String () != "0" {
149+ targetPort = p .TargetPort
150+ }
151+
152+ cp := corev1.ServicePort {
153+ Name : portName ,
154+ Protocol : corev1 .ProtocolTCP ,
155+ Port : p .Port ,
156+ TargetPort : targetPort ,
157+ }
158+
159+ if v := p .AppProtocol ; v != "" {
160+ cp .AppProtocol = & v
161+ }
162+
163+ ports = append (ports , cp )
164+ }
165+
166+ return ports
167+ }
168+
169+ func (c * KubernetesDefaultRouter ) getServicePorts (canary * flaggerv1.Canary ) []corev1.ServicePort {
170+ if len (canary .Spec .Service .Ports ) == 0 {
171+ return append (
172+ []corev1.ServicePort {
173+ c .getApexServicePort (canary ),
174+ },
175+ c .getDiscoveryServicePorts (canary )... ,
176+ )
177+ } else {
178+ return c .getExplicitServicePorts (canary )
179+ }
180+ }
181+
182+ func (c * KubernetesDefaultRouter ) reconcileService (canary * flaggerv1.Canary , name string , podSelector string , metadata * flaggerv1.CustomMetadata ) error {
183+ // set pod selector
184+ svcSpec := corev1.ServiceSpec {
185+ Type : corev1 .ServiceTypeClusterIP ,
186+ Selector : map [string ]string {c .labelSelector : podSelector },
187+ }
188+
189+ if canary .Spec .Service .Headless {
190+ svcSpec .ClusterIP = "None"
191+ }
192+
193+ svcSpec .Ports = c .getServicePorts (canary )
194+
139195 if metadata == nil {
140196 metadata = & flaggerv1.CustomMetadata {}
141197 }
0 commit comments