Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export a method to expose which ports were forwarded #67513

Merged
merged 1 commit into from Sep 25, 2018

Conversation

novas0x2a
Copy link
Contributor

What this PR does / why we need it:

The port-forwarding mechanism in client-go currently doesn't provide any
convenient method to retrieve the locally-bound port in cases where the
port requested was "0" (use any port). (It's only possible by parsing it
out of the output log stream).

Special notes for your reviewer:

Because this is a non-breaking client API change with no end-user-visible
changes (just end-developer changes :) ). I've marked this "no release
note." Let me know if you disagree and I'll add one.

Release note:

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 16, 2018
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 16, 2018
@neolit123
Copy link
Member

@novas0x2a
thanks for the PR!

/ok-to-test
/sig api-machinery

@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 16, 2018
@novas0x2a
Copy link
Contributor Author

Hm, maybe that one's flaky?
/retest

@lavalamp
Copy link
Member

/assign @yliaog

@yliaog
Copy link
Contributor

yliaog commented Aug 20, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 20, 2018
@novas0x2a
Copy link
Contributor Author

@liggitt / @caesarxuchao anything else this needs?

// GetPorts will return the ports that were forwarded; this can be used to
// retrieve the locally-bound port in cases where the input was port 0.
func (pf *PortForwarder) GetPorts() []ForwardedPort {
return pf.ports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calls to this for purposes of retrieving auto-assigned port numbers are necessarily made from a separate goroutine than the one that called ForwardPorts() (since that method blocks until the operation is complete). That means that anything using the value returned from this method is subject to data races with the port.Local = uint16(localPortUInt) assignment.

At the very least, we need to protect against that data race.

A bigger question is whether this method should block until the ForwardPorts() call has established listeners and the actual local ports are available.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I was going to leave it up to the caller to avoid the race, since any user of this would also have to know about Ready, and once Ready is signaled, the local ports are available, too. I perhaps should have documented that in the func doc.

(This code is meant specifically to close races like this.)

If you prefer, though, I can move the exclusion to here, no big deal. My first inclination would perhaps to be something like

	close(pf.listenersReady)
	if pf.Ready != nil {
		close(pf.Ready)
	}
...
func (pf *PortForwarder) GetPorts() ([]ForwardedPort, error) {
	select {
	case <-pf.listenersReady:
		return pf.ports, nil
	default:
		return nil, fmt.Errorf("listeners not ready")
	}
}

A caller would already be doing its own blocking on the Ready channel (if they cared to) so leaving GetPorts nonblocking seems fine, but I'll defer toYourJudgement() on that (obviously, not a huge code change either way). :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, could require a Ready channel be specified in order to use GetPorts() (which they have to provide if they want to call GetPorts() safely)... what do you think of something like this:

func (pf *PortForwarder) GetPorts() ([]ForwardedPort, error) {
	if pf.Ready == nil {
		return nil, fmt.Errorf("no Ready channel provided")
	}
	select {
	case <-pf.Ready:
		return pf.ports, nil
	default:
		return nil, fmt.Errorf("listeners not ready")
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. Updated the PR.

Without this change, the only method to discover what local port was
bound (if port 0 was requested) is to parse it out of the "out" stream,
which isn't the most reliable method.
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 30, 2018
@liggitt
Copy link
Member

liggitt commented Aug 30, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Aug 30, 2018
@yliaog
Copy link
Contributor

yliaog commented Aug 30, 2018

/lgtm

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: liggitt, novas0x2a, yliaog

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@novas0x2a
Copy link
Contributor Author

I guess the next step here is a milestone assignment? I'm not sure what the process is for that (this is my first contribution, obviously). Is there something I need to do?

@liggitt
Copy link
Member

liggitt commented Sep 2, 2018

Is there something I need to do?

Master is currently limited to features and bug fixes specifically targeted at 1.12. Once it opens again, this will merge without any further action.

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel comment for consistent failures.

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel comment for consistent failures.

1 similar comment
@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel comment for consistent failures.

@k8s-ci-robot k8s-ci-robot merged commit 4a627e9 into kubernetes:master Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants