Merge pull request #85511 from oke-py/port-forward-named-port

Fix a bug in port-forward: named port not working with service
This commit is contained in:
Kubernetes Prow Robot 2019-12-12 17:20:54 -08:00 committed by GitHub
commit 92a14f4f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -184,10 +184,10 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
return nil, err
}
if int32(portnum) != containerPort {
if int32(portnum) != containerPort || localPort == "" {
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
} else {
translated = append(translated, port)
translated = append(translated, fmt.Sprintf("%d", containerPort))
}
}
return translated, nil

View File

@ -413,6 +413,36 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
translated: []string{":8080", ":8443"},
err: false,
},
{
name: "test success 4 (named service port and named pod container port)",
svc: corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Port: 80,
Name: "http",
TargetPort: intstr.FromString("http"),
},
},
},
},
pod: corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: int32(80)},
},
},
},
},
},
ports: []string{"http"},
translated: []string{"80"},
err: false,
},
{
name: "test success (targetPort omitted)",
svc: corev1.Service{