Skip to content

Commit 822d167

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent a2f6e64 commit 822d167

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

api/adc/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ type Upstream struct {
148148

149149
HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
150150
Key string `json:"key,omitempty" yaml:"key,omitempty"`
151-
Nodes UpstreamNodes `json:"nodes,omitempty" yaml:"nodes,omitempty"`
151+
Nodes UpstreamNodes `json:"nodes" yaml:"nodes"`
152152
PassHost *PassHost `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
153153
Retries *int64 `json:"retries,omitempty" yaml:"retries,omitempty"`
154154
RetryTimeout *float64 `json:"retry_timeout,omitempty" yaml:"retry_timeout,omitempty"`

internal/controller/ingress_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/api7/api7-ingress-controller/internal/provider"
1111
"github.com/api7/gopkg/pkg/log"
1212
"github.com/go-logr/logr"
13+
"go.uber.org/zap"
1314
corev1 "k8s.io/api/core/v1"
1415
discoveryv1 "k8s.io/api/discovery/v1"
1516
networkingv1 "k8s.io/api/networking/v1"
@@ -267,6 +268,11 @@ func (r *IngressReconciler) processTLS(ctx context.Context, tctx *provider.Trans
267268
return err
268269
}
269270

271+
if secret.Data == nil {
272+
log.Warnw("secret data is nil", zap.String("secret", secret.Namespace+"/"+secret.Name))
273+
continue
274+
}
275+
270276
// add the secret to the translate context
271277
tctx.Secrets[types.NamespacedName{Namespace: ingress.Namespace, Name: tls.SecretName}] = &secret
272278
}

internal/provider/adc/translator/gateway.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ func (t *Translator) translateSecret(tctx *provider.TranslateContext, listener g
8585
// Dashboard doesn't allow wildcard hostname
8686
if listener.Hostname != nil && *listener.Hostname != "" {
8787
sslObj.Snis = append(sslObj.Snis, string(*listener.Hostname))
88+
} else {
89+
hosts, err := extractHost(cert)
90+
if err != nil {
91+
return nil, err
92+
}
93+
sslObj.Snis = append(sslObj.Snis, hosts...)
8894
}
89-
hosts, err := extractHost(cert)
90-
if err != nil {
91-
return nil, err
92-
}
93-
if len(hosts) == 0 {
95+
if len(sslObj.Snis) == 0 {
9496
log.Warnw("no valid hostname found in certificate", zap.String("secret", secret.Namespace+"/"+secret.Name))
9597
continue
9698
}
97-
sslObj.Snis = append(sslObj.Snis, hosts...)
9899
// Note: Dashboard doesn't allow duplicate certificate across ssl objects
99100
sslObj.ID = id.GenID(string(cert))
100101
sslObj.Labels = label.GenLabel(obj)

internal/provider/adc/translator/ingress.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package translator
22

33
import (
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

Comments
 (0)