Skip to content

Commit 729d38f

Browse files
authored
conformance: initial UDPRoute test (#2661)
* conformance: initial UDPRoute test Signed-off-by: Karol Szwaj <[email protected]> * go.mod Signed-off-by: Karol Szwaj <[email protected]> * reuse function for route checking Signed-off-by: Karol Szwaj <[email protected]> * fix path Signed-off-by: Karol Szwaj <[email protected]> * add features Signed-off-by: Karol Szwaj <[email protected]> * add type for routeHandlerFunc Signed-off-by: Karol Szwaj <[email protected]> * fix goimports Signed-off-by: Karol Szwaj <[email protected]> * rebase: use funcs from grpcroute tests Signed-off-by: Karol Szwaj <[email protected]> --------- Signed-off-by: Karol Szwaj <[email protected]>
1 parent 50091d0 commit 729d38f

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

conformance/base/manifests.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,81 @@ spec:
654654
resources:
655655
requests:
656656
cpu: 10m
657+
---
658+
apiVersion: v1
659+
kind: Service
660+
metadata:
661+
name: coredns
662+
namespace: gateway-conformance-infra
663+
labels:
664+
app: udp
665+
spec:
666+
ports:
667+
- name: udp-dns
668+
port: 53
669+
protocol: UDP
670+
targetPort: 53
671+
selector:
672+
app: udp
673+
---
674+
apiVersion: apps/v1
675+
kind: Deployment
676+
metadata:
677+
name: coredns
678+
namespace: gateway-conformance-infra
679+
labels:
680+
app: udp
681+
spec:
682+
selector:
683+
matchLabels:
684+
app: udp
685+
template:
686+
metadata:
687+
labels:
688+
app: udp
689+
spec:
690+
containers:
691+
- args:
692+
- -conf
693+
- /root/Corefile
694+
image: coredns/coredns
695+
name: coredns
696+
volumeMounts:
697+
- mountPath: /root
698+
name: conf
699+
volumes:
700+
- configMap:
701+
defaultMode: 420
702+
name: coredns
703+
name: conf
704+
---
705+
apiVersion: v1
706+
kind: ConfigMap
707+
metadata:
708+
name: coredns
709+
namespace: gateway-conformance-infra
710+
data:
711+
Corefile: |
712+
.:53 {
713+
forward . 8.8.8.8 9.9.9.9
714+
log
715+
errors
716+
}
717+
foo.bar.com:53 {
718+
whoami
719+
}
720+
---
721+
apiVersion: gateway.networking.k8s.io/v1beta1
722+
kind: Gateway
723+
metadata:
724+
name: udp-gateway
725+
namespace: gateway-conformance-infra
726+
spec:
727+
gatewayClassName: "{GATEWAY_CLASS_NAME}"
728+
listeners:
729+
- name: coredns
730+
protocol: UDP
731+
port: 5300
732+
allowedRoutes:
733+
kinds:
734+
- kind: UDPRoute

conformance/tests/udproute-simple.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"context"
21+
"testing"
22+
"time"
23+
24+
"github.com/miekg/dns"
25+
"k8s.io/apimachinery/pkg/types"
26+
"k8s.io/apimachinery/pkg/util/wait"
27+
28+
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
29+
"sigs.k8s.io/gateway-api/conformance/utils/suite"
30+
)
31+
32+
func init() {
33+
ConformanceTests = append(ConformanceTests, UDPRouteTest)
34+
}
35+
36+
var UDPRouteTest = suite.ConformanceTest{
37+
ShortName: "UDPRoute",
38+
Description: "Make sure UDPRoute is working",
39+
Manifests: []string{"tests/udproute-simple.yaml"},
40+
Features: []suite.SupportedFeature{
41+
suite.SupportGateway,
42+
suite.SupportUDPRoute,
43+
},
44+
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
45+
t.Run("Simple UDP request matching UDPRoute should reach coredns backend", func(t *testing.T) {
46+
namespace := "gateway-conformance-infra"
47+
domain := "foo.bar.com."
48+
routeNN := types.NamespacedName{Name: "udp-coredns", Namespace: namespace}
49+
gwNN := types.NamespacedName{Name: "udp-gateway", Namespace: namespace}
50+
gwAddr := kubernetes.GatewayAndUDPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
51+
52+
msg := new(dns.Msg)
53+
msg.SetQuestion(domain, dns.TypeA)
54+
55+
if err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, true,
56+
func(_ context.Context) (done bool, err error) {
57+
t.Logf("performing DNS query %s on %s", domain, gwAddr)
58+
_, err = dns.Exchange(msg, gwAddr)
59+
if err != nil {
60+
t.Logf("failed to perform a UDP query: %v", err)
61+
return false, nil
62+
}
63+
return true, nil
64+
}); err != nil {
65+
t.Errorf("failed to perform DNS query: %v", err)
66+
}
67+
})
68+
},
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: gateway.networking.k8s.io/v1alpha2
2+
kind: UDPRoute
3+
metadata:
4+
name: udp-coredns
5+
namespace: gateway-conformance-infra
6+
spec:
7+
parentRefs:
8+
- name: udp-gateway
9+
sectionName: coredns
10+
rules:
11+
- backendRefs:
12+
- name: coredns
13+
port: 53

conformance/utils/kubernetes/helpers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,14 @@ func GatewayAndHTTPRoutesMustBeAccepted(t *testing.T, c client.Client, timeoutCo
401401
return GatewayAndRoutesMustBeAccepted(t, c, timeoutConfig, controllerName, gw, &gatewayv1.HTTPRoute{}, routeNNs...)
402402
}
403403

404+
// GatewayAndUDPRoutesMustBeAccepted waits until the specified Gateway has an IP
405+
// address assigned to it and the UDPRoute has a ParentRef referring to the
406+
// Gateway. The test will fail if these conditions are not met before the
407+
// timeouts.
408+
func GatewayAndUDPRoutesMustBeAccepted(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, controllerName string, gw GatewayRef, routeNNs ...types.NamespacedName) string {
409+
return GatewayAndRoutesMustBeAccepted(t, c, timeoutConfig, controllerName, gw, &v1alpha2.UDPRoute{}, routeNNs...)
410+
}
411+
404412
// WaitForGatewayAddress waits until at least one IP Address has been set in the
405413
// status of the specified Gateway.
406414
func WaitForGatewayAddress(t *testing.T, client client.Client, timeoutConfig config.TimeoutConfig, gwName types.NamespacedName) (string, error) {
@@ -599,6 +607,13 @@ func HTTPRouteMustHaveParents(t *testing.T, client client.Client, timeoutConfig
599607
RouteMustHaveParents(t, client, timeoutConfig, routeName, parents, namespaceRequired, &gatewayv1.HTTPRoute{})
600608
}
601609

610+
// UDPRouteMustHaveParents waits for the specified UDPRoute to have parents
611+
// in status that match the expected parents. This will cause the test to halt
612+
// if the specified timeout is exceeded.
613+
func UDPRouteMustHaveParents(t *testing.T, client client.Client, timeoutConfig config.TimeoutConfig, routeName types.NamespacedName, parents []gatewayv1.RouteParentStatus, namespaceRequired bool) {
614+
RouteMustHaveParents(t, client, timeoutConfig, routeName, parents, namespaceRequired, &v1alpha2.UDPRoute{})
615+
}
616+
602617
// TLSRouteMustHaveParents waits for the specified TLSRoute to have parents
603618
// in status that match the expected parents, and also returns the TLSRoute.
604619
// This will cause the test to halt if the specified timeout is exceeded.

conformance/utils/suite/features.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ var TLSRouteCoreFeatures = sets.New(
196196
SupportTLSRoute,
197197
)
198198

199+
// -----------------------------------------------------------------------------
200+
// Features - UDPRoute Conformance (Core)
201+
// -----------------------------------------------------------------------------
202+
203+
const (
204+
// This option indicates support for UDPRoute
205+
SupportUDPRoute SupportedFeature = "UDPRoute"
206+
)
207+
208+
// UDPRouteCoreFeatures includes all SupportedFeatures needed to be conformant with
209+
// the UDPRoute resource.
210+
var UDPRouteFeatures = sets.New(
211+
SupportUDPRoute,
212+
)
213+
199214
// -----------------------------------------------------------------------------
200215
// Features - Mesh Conformance (Core)
201216
// -----------------------------------------------------------------------------

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.21
44

55
require (
66
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0
7+
github.com/miekg/dns v1.1.57
78
github.com/stretchr/testify v1.9.0
89
golang.org/x/net v0.20.0
910
google.golang.org/grpc v1.61.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
8686
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
8787
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
8888
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
89+
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
90+
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
8991
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
9092
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
9193
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

0 commit comments

Comments
 (0)