Skip to content

Commit 30fd9af

Browse files
committed
kubernetesservice: support forwarding UDP
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent a313503 commit 30fd9af

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

hack/bats/extras/k8s.bats

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,35 @@ k() {
5050

5151
# bats test_tags=nodes:1
5252
@test 'Single-node' {
53-
k create deployment nginx --image="${TEST_CONTAINER_IMAGES["nginx"]}"
54-
k rollout status deployment nginx --timeout 60s
53+
# Deploy test services
54+
services=(nginx coredns)
55+
for svc in "${services[@]}"; do
56+
k create deployment "$svc" --image="${TEST_CONTAINER_IMAGES["$svc"]}"
57+
done
58+
for svc in "${services[@]}"; do
59+
k rollout status deployment "$svc" --timeout 60s
60+
done
61+
62+
# Test TCP port forwarding
5563
k create service nodeport nginx --node-port=31080 --tcp=80:80
5664
run curl --fail --silent --show-error --retry 30 --retry-all-errors http://localhost:31080
5765
assert_success
5866
assert_output --partial "Welcome to nginx"
59-
# TODO: support UDP
60-
k delete service nginx
61-
k delete deployment nginx
67+
68+
# Test UDP port forwarding
69+
#
70+
# `kubectl create service nodeport` does not support UDP, so use `kubectl expose` instead.
71+
# https://github.com/kubernetes/kubernetes/issues/134732
72+
k expose deployment coredns --port=53 --type=NodePort \
73+
--overrides='{"spec":{"ports":[{"port":53,"protocol":"UDP","targetPort":53,"nodePort":32053}]}}'
74+
run dig @127.0.0.1 -p 32053 lima-vm.io
75+
assert_success
76+
77+
# Cleanup
78+
for svc in "${services[@]}"; do
79+
k delete service "$svc"
80+
k delete deployment "$svc"
81+
done
6282
}
6383

6484
# TODO: add a test for multi-node

hack/bats/helpers/load.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ assert_output_lines_count() {
7070
# NOTE: keep this list in sync with hack/test-templates.sh .
7171
declare -A -g TEST_CONTAINER_IMAGES=(
7272
["nginx"]="ghcr.io/stargz-containers/nginx:1.19-alpine-org"
73+
["coredns"]="public.ecr.aws/eks-distro/coredns/coredns:v1.12.2-eks-1-31-latest"
7374
)

pkg/guestagent/kubernetesservice/kubernetesservice.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import (
2626
type Protocol string
2727

2828
const (
29-
// UDP/SCTP when lima port forwarding works on those protocols.
30-
3129
TCP Protocol = "tcp"
30+
UDP Protocol = "udp"
3231
)
3332

3433
type Entry struct {
@@ -134,8 +133,12 @@ func (s *ServiceWatcher) GetPorts() []Entry {
134133
}
135134

136135
for _, portEntry := range service.Spec.Ports {
137-
if portEntry.Protocol != corev1.ProtocolTCP {
138-
// currently only TCP port can be forwarded
136+
switch portEntry.Protocol {
137+
case corev1.ProtocolTCP, corev1.ProtocolUDP:
138+
// NOP
139+
default:
140+
logrus.Debugf("unsupported protocol %s for service %s/%s, skipping",
141+
portEntry.Protocol, service.Namespace, service.Name)
139142
continue
140143
}
141144

pkg/guestagent/kubernetesservice/kubernetesservice_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,24 @@ func TestGetPorts(t *testing.T) {
5858
TargetPort: intstr.FromInt(80),
5959
NodePort: 8080,
6060
},
61+
{
62+
Name: "dns",
63+
Protocol: corev1.ProtocolUDP,
64+
Port: 53,
65+
TargetPort: intstr.FromInt(53),
66+
NodePort: 5353,
67+
},
6168
},
6269
},
6370
},
6471
want: []Entry{{
6572
Protocol: TCP,
6673
IP: net.IPv4zero,
6774
Port: 8080,
75+
}, {
76+
Protocol: UDP,
77+
IP: net.IPv4zero,
78+
Port: 5353,
6879
}},
6980
},
7081
{

0 commit comments

Comments
 (0)