mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #89401 from liggitt/fix_kubectl_explicit_local_port_for_service
Fix kubectl explicit local port for service
This commit is contained in:
commit
11277d4aca
@ -185,10 +185,13 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if int32(portnum) != containerPort || localPort == "" {
|
||||
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
|
||||
// convert the resolved target port back to a string
|
||||
remotePort = strconv.Itoa(int(containerPort))
|
||||
|
||||
if localPort != remotePort {
|
||||
translated = append(translated, fmt.Sprintf("%s:%s", localPort, remotePort))
|
||||
} else {
|
||||
translated = append(translated, fmt.Sprintf("%d", containerPort))
|
||||
translated = append(translated, remotePort)
|
||||
}
|
||||
}
|
||||
return translated, nil
|
||||
|
@ -209,6 +209,35 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
|
||||
translated: []string{":8080"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success 1 (int port with explicit local port)",
|
||||
svc: corev1.Service{
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Port: 8080,
|
||||
TargetPort: intstr.FromInt(8080),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
pod: corev1.Pod{
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Ports: []corev1.ContainerPort{
|
||||
{
|
||||
Name: "http",
|
||||
ContainerPort: int32(8080)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ports: []string{"8000:8080"},
|
||||
translated: []string{"8000:8080"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success 2 (clusterIP: None)",
|
||||
svc: corev1.Service{
|
||||
|
Loading…
Reference in New Issue
Block a user