Skip to content

Commit f1e2e54

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

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

hack/bats/extras/k8s.bats

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,34 @@ 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+
k expose deployment coredns --port=53 --type=NodePort \
72+
--overrides='{"spec":{"ports":[{"port":53,"protocol":"UDP","targetPort":53,"nodePort":32053}]}}'
73+
run dig @127.0.0.1 -p 32053 lima-vm.io
74+
assert_success
75+
76+
# Cleanup
77+
for svc in "${services[@]}"; do
78+
k delete service "$svc"
79+
k delete deployment "$svc"
80+
done
6281
}
6382

6483
# 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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ func (s *ServiceWatcher) GetPorts() []Entry {
134134
}
135135

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

0 commit comments

Comments
 (0)