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:
Kubernetes Prow Robot 2020-03-23 23:10:47 -07:00 committed by GitHub
commit 11277d4aca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 3 deletions

View File

@ -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

View File

@ -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{