Skip to content

Commit 4daa0a0

Browse files
committed
add test
1 parent 00df0f7 commit 4daa0a0

File tree

2 files changed

+172
-3
lines changed

2 files changed

+172
-3
lines changed

internal/adc/translator/apisixroute.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525

2626
"github.com/pkg/errors"
27+
corev1 "k8s.io/api/core/v1"
2728
v1 "k8s.io/api/core/v1"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/types"
@@ -333,6 +334,30 @@ func getPortFromService(svc *v1.Service, backendSvcPort intstr.IntOrString) (int
333334
return port, nil
334335
}
335336

337+
func findMatchingServicePort(svc *v1.Service, backendSvcPort intstr.IntOrString) (*corev1.ServicePort, error) {
338+
var servicePort *corev1.ServicePort
339+
var portNumber int32 = -1
340+
var servicePortName string
341+
switch backendSvcPort.Type {
342+
case intstr.Int:
343+
portNumber = backendSvcPort.IntVal
344+
case intstr.String:
345+
servicePortName = backendSvcPort.StrVal
346+
}
347+
for _, svcPort := range svc.Spec.Ports {
348+
p := svcPort
349+
if p.Port == portNumber || (p.Name != "" && p.Name == servicePortName) {
350+
servicePort = &p
351+
break
352+
}
353+
}
354+
if servicePort == nil {
355+
return nil, errors.Errorf("service port %s not found in service %s", backendSvcPort.String(), svc.Name)
356+
}
357+
358+
return servicePort, nil
359+
}
360+
336361
func (t *Translator) translateApisixRouteHTTPBackend(tctx *provider.TranslateContext, ar *apiv2.ApisixRoute, backend apiv2.ApisixRouteHTTPBackend, enableWebsocket **bool) (*adc.Upstream, error) {
337362
auNN := types.NamespacedName{
338363
Namespace: ar.Namespace,
@@ -392,17 +417,17 @@ func (t *Translator) translateApisixRouteBackendResolveGranularityService(tctx *
392417
if svc.Spec.ClusterIP == "" {
393418
return nil, "", errors.Errorf("conflict headless service and backend resolve granularity, ApisixRoute: %s, Service: %s", arNN, serviceNN)
394419
}
395-
port, err := getPortFromService(svc, backend.ServicePort)
420+
port, err := findMatchingServicePort(svc, backend.ServicePort)
396421
if err != nil {
397422
return nil, "", err
398423
}
399424
return adc.UpstreamNodes{
400425
{
401426
Host: svc.Spec.ClusterIP,
402-
Port: int(port),
427+
Port: int(port.Port),
403428
Weight: *cmp.Or(backend.Weight, ptr.To(apiv2.DefaultWeight)),
404429
},
405-
}, "", nil
430+
}, ptr.Deref(port.AppProtocol, ""), nil
406431
}
407432

408433
func (t *Translator) translateApisixRouteStreamBackendResolveGranularity(tctx *provider.TranslateContext, arNN types.NamespacedName, backend apiv2.ApisixRouteStreamBackend) (adc.UpstreamNodes, string, error) {

test/e2e/crds/v2/route.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v2
1919

2020
import (
2121
"context"
22+
"crypto/tls"
2223
"fmt"
2324
"io"
2425
"math"
@@ -34,6 +35,7 @@ import (
3435
"github.com/stretchr/testify/assert"
3536
"k8s.io/apimachinery/pkg/types"
3637
"k8s.io/apimachinery/pkg/util/wait"
38+
"k8s.io/utils/ptr"
3739

3840
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3941
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
@@ -2033,4 +2035,146 @@ spec:
20332035
})
20342036
})
20352037
})
2038+
2039+
Context("Test Services With AppProtocol", func() {
2040+
const apisixRoute = `
2041+
apiVersion: apisix.apache.org/v2
2042+
kind: ApisixRoute
2043+
metadata:
2044+
name: nginx
2045+
spec:
2046+
ingressClassName: %s
2047+
http:
2048+
- name: rule0
2049+
match:
2050+
hosts:
2051+
- nginx.example
2052+
paths:
2053+
- /v1
2054+
backends:
2055+
- serviceName: nginx
2056+
servicePort: 443
2057+
`
2058+
const apisixRouteWithGranularityService = `
2059+
apiVersion: apisix.apache.org/v2
2060+
kind: ApisixRoute
2061+
metadata:
2062+
name: nginx-v2
2063+
spec:
2064+
ingressClassName: %s
2065+
http:
2066+
- name: rule0
2067+
match:
2068+
hosts:
2069+
- nginx.example
2070+
paths:
2071+
- /v2
2072+
backends:
2073+
- serviceName: nginx
2074+
servicePort: 443
2075+
resolveGranularity: service
2076+
`
2077+
const apisixRouteWithBackendWSS = `
2078+
apiVersion: apisix.apache.org/v2
2079+
kind: ApisixRoute
2080+
metadata:
2081+
name: default
2082+
spec:
2083+
ingressClassName: %s
2084+
http:
2085+
- name: rule0
2086+
match:
2087+
hosts:
2088+
- api6.com
2089+
paths:
2090+
- /ws
2091+
backends:
2092+
- serviceName: nginx
2093+
servicePort: 8443
2094+
resolveGranularity: service
2095+
`
2096+
2097+
const apisixTlsSpec = `
2098+
apiVersion: apisix.apache.org/v2
2099+
kind: ApisixTls
2100+
metadata:
2101+
name: test-tls
2102+
spec:
2103+
ingressClassName: %s
2104+
hosts:
2105+
- api6.com
2106+
secret:
2107+
name: test-tls-secret
2108+
namespace: %s
2109+
`
2110+
BeforeEach(func() {
2111+
s.DeployNginx(framework.NginxOptions{
2112+
Namespace: s.Namespace(),
2113+
Replicas: ptr.To(int32(1)),
2114+
})
2115+
})
2116+
2117+
It("HTTPS Backend", func() {
2118+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "nginx"},
2119+
new(apiv2.ApisixRoute), fmt.Sprintf(apisixRoute, s.Namespace()))
2120+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "nginx-v2"},
2121+
new(apiv2.ApisixRoute), fmt.Sprintf(apisixRouteWithGranularityService, s.Namespace()))
2122+
2123+
s.RequestAssert(&scaffold.RequestAssert{
2124+
Method: "GET",
2125+
Path: "/v1",
2126+
Host: "nginx.example",
2127+
Check: scaffold.WithExpectedStatus(http.StatusOK),
2128+
})
2129+
s.RequestAssert(&scaffold.RequestAssert{
2130+
Method: "GET",
2131+
Path: "/v2",
2132+
Host: "nginx.example",
2133+
Check: scaffold.WithExpectedStatus(http.StatusOK),
2134+
})
2135+
})
2136+
2137+
It("WSS Backend", func() {
2138+
err := s.NewKubeTlsSecret("test-tls-secret", Cert, Key)
2139+
Expect(err).NotTo(HaveOccurred(), "creating TLS secret")
2140+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "test-tls"},
2141+
&apiv2.ApisixTls{}, fmt.Sprintf(apisixTlsSpec, s.Namespace(), s.Namespace()))
2142+
2143+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"},
2144+
new(apiv2.ApisixRoute), fmt.Sprintf(apisixRouteWithBackendWSS, s.Namespace()))
2145+
time.Sleep(6 * time.Second)
2146+
2147+
By("verify wss connection")
2148+
u := url.URL{
2149+
Scheme: "wss",
2150+
Host: s.GetAPISIXHTTPSEndpoint(),
2151+
Path: "/ws",
2152+
}
2153+
headers := http.Header{"Host": []string{"api6.com"}}
2154+
dialer := websocket.Dialer{
2155+
TLSClientConfig: &tls.Config{
2156+
InsecureSkipVerify: true,
2157+
ServerName: "api6.com",
2158+
},
2159+
}
2160+
2161+
conn, resp, err := dialer.Dial(u.String(), headers)
2162+
Expect(err).ShouldNot(HaveOccurred(), "WebSocket handshake")
2163+
Expect(resp.StatusCode).Should(Equal(http.StatusSwitchingProtocols))
2164+
2165+
defer func() {
2166+
_ = conn.Close()
2167+
}()
2168+
2169+
By("send and receive message through WebSocket")
2170+
testMessage := "hello, this is APISIX"
2171+
err = conn.WriteMessage(websocket.TextMessage, []byte(testMessage))
2172+
Expect(err).ShouldNot(HaveOccurred(), "writing WebSocket message")
2173+
2174+
// Then our echo
2175+
_, msg, err := conn.ReadMessage()
2176+
Expect(err).ShouldNot(HaveOccurred(), "reading echo message")
2177+
Expect(string(msg)).To(Equal(testMessage), "message content verification")
2178+
})
2179+
})
20362180
})

0 commit comments

Comments
 (0)