Fix a bug in port-forward: named port not working with service

This commit is contained in:
Naoki Oketani 2019-11-22 02:59:26 +09:00
parent 3c5dad61f7
commit 73c1ab71e2
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{