@@ -25,6 +25,7 @@ import (
2525 discoveryv1 "k8s.io/api/discovery/v1"
2626 networkingv1 "k8s.io/api/networking/v1"
2727 "k8s.io/apimachinery/pkg/types"
28+ "k8s.io/apimachinery/pkg/util/intstr"
2829 "k8s.io/utils/ptr"
2930
3031 adctypes "github.com/apache/apisix-ingress-controller/api/adc"
@@ -172,12 +173,11 @@ func (t *Translator) resolveIngressUpstream(
172173 t .AttachBackendTrafficPolicyToUpstream (backendRef , tctx .BackendTrafficPolicies , upstream )
173174 // determine service port/port name
174175 var protocol string
175- var servicePort int32 = 0
176- var servicePortName string
176+ var port intstr.IntOrString
177177 if backendService .Port .Number != 0 {
178- servicePort = backendService .Port .Number
178+ port = intstr . FromInt32 ( backendService .Port .Number )
179179 } else if backendService .Port .Name != "" {
180- servicePortName = backendService .Port .Name
180+ port = intstr . FromString ( backendService .Port .Name )
181181 }
182182
183183 getService := tctx .Services [types.NamespacedName {
@@ -187,43 +187,28 @@ func (t *Translator) resolveIngressUpstream(
187187 if getService == nil {
188188 return protocol
189189 }
190-
190+ getServicePort , _ := findMatchingServicePort (getService , port )
191+ if getServicePort != nil && getServicePort .AppProtocol != nil {
192+ protocol = * getServicePort .AppProtocol
193+ if upstream .Scheme == "" {
194+ upstream .Scheme = appProtocolToUpstreamScheme (* getServicePort .AppProtocol )
195+ }
196+ }
191197 if getService .Spec .Type == corev1 .ServiceTypeExternalName {
192- defaultServicePort := 80
193- if servicePort > 0 {
194- defaultServicePort = int (servicePort )
198+ servicePort := 80
199+ if getServicePort != nil {
200+ servicePort = int (getServicePort . Port )
195201 }
196202 upstream .Nodes = adctypes.UpstreamNodes {
197203 {
198204 Host : getService .Spec .ExternalName ,
199- Port : defaultServicePort ,
205+ Port : servicePort ,
200206 Weight : 1 ,
201207 },
202208 }
203209 return protocol
204210 }
205211
206- // find matching service port object
207- var getServicePort * corev1.ServicePort
208- for _ , port := range getService .Spec .Ports {
209- p := port
210- if servicePort > 0 && p .Port == servicePort {
211- getServicePort = & p
212- break
213- }
214- if servicePortName != "" && p .Name == servicePortName {
215- getServicePort = & p
216- break
217- }
218- }
219-
220- if getServicePort != nil && getServicePort .AppProtocol != nil {
221- protocol = * getServicePort .AppProtocol
222- if upstream .Scheme == "" {
223- upstream .Scheme = appProtocolToUpstreamScheme (* getServicePort .AppProtocol )
224- }
225- }
226-
227212 endpointSlices := tctx .EndpointSlices [types.NamespacedName {
228213 Namespace : obj .Namespace ,
229214 Name : backendService .Name ,
0 commit comments