Skip to content

Commit 28fc772

Browse files
authored
Merge pull request kubernetes#94728 from amorenoz/portforward_udp
kubectl: Fix TCP and UDP port forward
2 parents 09b3f6d + 21b598c commit 28fc772

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,35 +260,37 @@ func checkUDPPorts(udpOnlyPorts sets.Int, ports []string, obj metav1.Object) err
260260
// checkUDPPortInService returns an error if remote port in Service is a UDP port
261261
// TODO: remove this check after #47862 is solved
262262
func checkUDPPortInService(ports []string, svc *corev1.Service) error {
263-
udpOnlyPorts := sets.NewInt()
263+
udpPorts := sets.NewInt()
264+
tcpPorts := sets.NewInt()
264265
for _, port := range svc.Spec.Ports {
265266
portNum := int(port.Port)
266267
switch port.Protocol {
267268
case corev1.ProtocolUDP:
268-
udpOnlyPorts.Insert(portNum)
269+
udpPorts.Insert(portNum)
269270
case corev1.ProtocolTCP:
270-
udpOnlyPorts.Delete(portNum)
271+
tcpPorts.Insert(portNum)
271272
}
272273
}
273-
return checkUDPPorts(udpOnlyPorts, ports, svc)
274+
return checkUDPPorts(udpPorts.Difference(tcpPorts), ports, svc)
274275
}
275276

276277
// checkUDPPortInPod returns an error if remote port in Pod is a UDP port
277278
// TODO: remove this check after #47862 is solved
278279
func checkUDPPortInPod(ports []string, pod *corev1.Pod) error {
279-
udpOnlyPorts := sets.NewInt()
280+
udpPorts := sets.NewInt()
281+
tcpPorts := sets.NewInt()
280282
for _, ct := range pod.Spec.Containers {
281283
for _, ctPort := range ct.Ports {
282284
portNum := int(ctPort.ContainerPort)
283285
switch ctPort.Protocol {
284286
case corev1.ProtocolUDP:
285-
udpOnlyPorts.Insert(portNum)
287+
udpPorts.Insert(portNum)
286288
case corev1.ProtocolTCP:
287-
udpOnlyPorts.Delete(portNum)
289+
tcpPorts.Insert(portNum)
288290
}
289291
}
290292
}
291-
return checkUDPPorts(udpOnlyPorts, ports, pod)
293+
return checkUDPPorts(udpPorts.Difference(tcpPorts), ports, pod)
292294
}
293295

294296
// Complete completes all the required options for port-forward cmd.

staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ func TestCheckUDPPort(t *testing.T) {
880880
expectError: true,
881881
},
882882
{
883-
name: "Pod has ports with both TCP and UDP protocol",
883+
name: "Pod has ports with both TCP and UDP protocol (UDP first)",
884884
pod: &corev1.Pod{
885885
Spec: corev1.PodSpec{
886886
Containers: []corev1.Container{
@@ -895,6 +895,22 @@ func TestCheckUDPPort(t *testing.T) {
895895
},
896896
ports: []string{":53"},
897897
},
898+
{
899+
name: "Pod has ports with both TCP and UDP protocol (TCP first)",
900+
pod: &corev1.Pod{
901+
Spec: corev1.PodSpec{
902+
Containers: []corev1.Container{
903+
{
904+
Ports: []corev1.ContainerPort{
905+
{Protocol: corev1.ProtocolTCP, ContainerPort: 53},
906+
{Protocol: corev1.ProtocolUDP, ContainerPort: 53},
907+
},
908+
},
909+
},
910+
},
911+
},
912+
ports: []string{":53"},
913+
},
898914

899915
{
900916
name: "forward to a UDP port in a Service",
@@ -921,7 +937,7 @@ func TestCheckUDPPort(t *testing.T) {
921937
expectError: true,
922938
},
923939
{
924-
name: "Service has ports with both TCP and UDP protocol",
940+
name: "Service has ports with both TCP and UDP protocol (UDP first)",
925941
service: &corev1.Service{
926942
Spec: corev1.ServiceSpec{
927943
Ports: []corev1.ServicePort{
@@ -932,6 +948,18 @@ func TestCheckUDPPort(t *testing.T) {
932948
},
933949
ports: []string{"53"},
934950
},
951+
{
952+
name: "Service has ports with both TCP and UDP protocol (TCP first)",
953+
service: &corev1.Service{
954+
Spec: corev1.ServiceSpec{
955+
Ports: []corev1.ServicePort{
956+
{Protocol: corev1.ProtocolTCP, Port: 53},
957+
{Protocol: corev1.ProtocolUDP, Port: 53},
958+
},
959+
},
960+
},
961+
ports: []string{"53"},
962+
},
935963
}
936964
for _, tc := range tests {
937965
var err error

0 commit comments

Comments
 (0)