fix kubectl port-forward for services with explicit local port

This commit is contained in:
Marius Ziemke 2020-03-08 17:49:18 +01:00
parent 672aa55ee4
commit ef2eaa4f8f
2 changed files with 33 additions and 1 deletions

View File

@ -185,7 +185,10 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
return nil, err
}
if int32(portnum) != containerPort || localPort == "" {
// should fail when localPort is empty (=> use random local port)
localportnum, err := strconv.Atoi(localPort)
if int32(portnum) != containerPort || localPort == "" || (int32(localportnum) != containerPort && err == nil) {
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
} else {
translated = append(translated, fmt.Sprintf("%d", containerPort))

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{