@@ -2,13 +2,12 @@ package translator
22
33import (
44 "fmt"
5+ "strings"
56
67 adctypes "github.com/api7/api7-ingress-controller/api/adc"
78 "github.com/api7/api7-ingress-controller/internal/controller/label"
89 "github.com/api7/api7-ingress-controller/internal/id"
910 "github.com/api7/api7-ingress-controller/internal/provider"
10- "github.com/api7/gopkg/pkg/log"
11- "go.uber.org/zap"
1211 corev1 "k8s.io/api/core/v1"
1312 discoveryv1 "k8s.io/api/discovery/v1"
1413 networkingv1 "k8s.io/api/networking/v1"
@@ -23,11 +22,13 @@ func (t *Translator) translateIngressTLS(ingressTLS *networkingv1.IngressTLS, se
2322 }
2423
2524 hosts := ingressTLS .Hosts
26- certHosts , err := extractHost (cert )
27- if err != nil {
28- return nil , err
25+ if len (hosts ) == 0 {
26+ certHosts , err := extractHost (cert )
27+ if err != nil {
28+ return nil , err
29+ }
30+ hosts = append (hosts , certHosts ... )
2931 }
30- hosts = append (hosts , certHosts ... )
3132 if len (hosts ) == 0 {
3233 return nil , fmt .Errorf ("no hosts found in ingress TLS" )
3334 }
@@ -66,10 +67,6 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
6667 if secret == nil {
6768 continue
6869 }
69- if secret .Data == nil {
70- log .Warnw ("secret data is nil" , zap .String ("secret" , secret .Namespace + "/" + secret .Name ))
71- continue
72- }
7370 ssl , err := t .translateIngressTLS (& tls , secret , labels )
7471 if err != nil {
7572 return nil , err
@@ -120,21 +117,16 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
120117 } else if backendService .Port .Name != "" {
121118 servicePortName = backendService .Port .Name
122119 }
120+ _ = servicePort
123121
124122 // convert the EndpointSlice to upstream nodes
125123 if len (endpointSlices ) > 0 {
126- upstream .Nodes = t .translateEndpointSliceForIngress (1 , endpointSlices , servicePort , servicePortName )
124+ upstream .Nodes = t .translateEndpointSliceForIngress (1 , endpointSlices , servicePortName )
127125 }
128126
129127 // if there is no upstream node, create a placeholder node
130128 if len (upstream .Nodes ) == 0 {
131- upstream .Nodes = adctypes.UpstreamNodes {
132- {
133- Host : "0.0.0.0" ,
134- Port : int (servicePort ),
135- Weight : 1 ,
136- },
137- }
129+ upstream .Nodes = adctypes.UpstreamNodes {}
138130 }
139131
140132 service .Upstream = upstream
@@ -145,15 +137,30 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
145137 route .ID = id .GenID (route .Name )
146138 route .Labels = labels
147139
148- // set the path matching rule
149- switch * path .PathType {
150- case networkingv1 .PathTypeExact :
151- route .Uris = []string {path .Path }
152- case networkingv1 .PathTypePrefix :
153- route .Uris = []string {path .Path + "*" }
154- case networkingv1 .PathTypeImplementationSpecific :
155- route .Uris = []string {path .Path + "*" }
140+ uris := []string {path .Path }
141+ if path .PathType != nil {
142+ if * path .PathType == networkingv1 .PathTypePrefix {
143+ // As per the specification of Ingress path matching rule:
144+ // if the last element of the path is a substring of the
145+ // last element in request path, it is not a match, e.g. /foo/bar
146+ // matches /foo/bar/baz, but does not match /foo/barbaz.
147+ // While in APISIX, /foo/bar matches both /foo/bar/baz and
148+ // /foo/barbaz.
149+ // In order to be conformant with Ingress specification, here
150+ // we create two paths here, the first is the path itself
151+ // (exact match), the other is path + "/*" (prefix match).
152+ prefix := path .Path
153+ if strings .HasSuffix (prefix , "/" ) {
154+ prefix += "*"
155+ } else {
156+ prefix += "/*"
157+ }
158+ uris = append (uris , prefix )
159+ } else if * path .PathType == networkingv1 .PathTypeImplementationSpecific {
160+ uris = []string {"/*" }
161+ }
156162 }
163+ route .Uris = uris
157164
158165 service .Routes = []* adctypes.Route {route }
159166 result .Services = append (result .Services , service )
@@ -164,16 +171,16 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
164171}
165172
166173// translateEndpointSliceForIngress create upstream nodes from EndpointSlice
167- func (t * Translator ) translateEndpointSliceForIngress (weight int , endpointSlices []discoveryv1.EndpointSlice , portNumber int32 , portName string ) adctypes.UpstreamNodes {
174+ func (t * Translator ) translateEndpointSliceForIngress (weight int , endpointSlices []discoveryv1.EndpointSlice , portName string ) adctypes.UpstreamNodes {
168175 var nodes adctypes.UpstreamNodes
169176 if len (endpointSlices ) == 0 {
170177 return nodes
171178 }
172179
173180 for _ , endpointSlice := range endpointSlices {
174181 for _ , port := range endpointSlice .Ports {
175- // if the port number or port name is specified, only use the matching port
176- if ( portNumber != 0 && * port . Port != portNumber ) || ( portName != "" && * port .Name != portName ) {
182+ // if the port name is specified, only use the matching port
183+ if portName != "" && * port .Name != portName {
177184 continue
178185 }
179186 for _ , endpoint := range endpointSlice .Endpoints {
0 commit comments