client-go: Dynamic local port not accessible when port-forwarding

When setting up a port forwarding with the client-go library (using the
`k8s.io/client-go/tools/portforward.PortForwarder`) with a non-defined local
port (i.e. passing `:80` as `ports` parameter to `portforward.New(...)`), a
local port will be assigned dynamically.

Currently, the local port will be _always_ 0 if it was not specified initially.
This is because the assigned local port is only set on a _copy_ of the actual
`ForwardedPort` type that is obtained in a `range` loop. This PR changes this
behaviour to set the local port at the correct instance by passing a pointer
instead of a copy to the relevant functions.

Kubernetes-commit: bbddd27f0dfffe6623763afe2c02c876ba925a7c
This commit is contained in:
Martin Helmich
2019-02-03 19:01:19 +01:00
committed by Kubernetes Publisher
parent 91013a0646
commit e70639fd33
2 changed files with 79 additions and 2 deletions

View File

@@ -205,8 +205,9 @@ func (pf *PortForwarder) forward() error {
var err error
listenSuccess := false
for _, port := range pf.ports {
err = pf.listenOnPort(&port)
for i := range pf.ports {
port := &pf.ports[i]
err = pf.listenOnPort(port)
switch {
case err == nil:
listenSuccess = true