Skip to content

Commit dfeb617

Browse files
committed
Simplify dual or single port logic
1 parent ef2eaa4 commit dfeb617

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
185185
return nil, err
186186
}
187187

188-
// should fail when localPort is empty (=> use random local port)
189-
localportnum, err := strconv.Atoi(localPort)
188+
// convert the resolved target port back to a string
189+
remotePort = strconv.Itoa(int(containerPort))
190190

191-
if int32(portnum) != containerPort || localPort == "" || (int32(localportnum) != containerPort && err == nil) {
192-
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
191+
if localPort != remotePort {
192+
translated = append(translated, fmt.Sprintf("%s:%s", localPort, remotePort))
193193
} else {
194-
translated = append(translated, fmt.Sprintf("%d", containerPort))
194+
translated = append(translated, remotePort)
195195
}
196196
}
197197
return translated, nil

0 commit comments

Comments
 (0)