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

ability to run a local command with port-forward #97592

Closed

Conversation

carlory
Copy link
Member

@carlory carlory commented Dec 30, 2020

What type of PR is this?

/kind feature

What this PR does / why we need it:

This would allow scripting around port-forwarding. That will be especially useful for developers, but can also be useful in production. Currently, we can do that, but we have to use one terminal to create the port-forwarding, use another to run any tool that uses the port, then finally go back to the first terminal and terminate the forwarding. This is not scriptable.

Which issue(s) this PR fixes:

Fixes #97549

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Support exec in `kubectl port-forward`. For example, kubectl port-forward service/postgres 5432 -- psql -h localhost -p 5432 -c "select 1"

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 30, 2020
@k8s-ci-robot
Copy link
Contributor

@carlory: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Dec 30, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @carlory. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Dec 30, 2020
@carlory carlory changed the title ability to run a local command with port-forward [WIP] ability to run a local command with port-forward Dec 30, 2020
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 30, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: carlory
To complete the pull request process, please assign deads2k after the PR has been reviewed.
You can assign the PR to them by writing /assign @deads2k in a comment when ready.

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 30, 2020
@carlory carlory force-pushed the feature-kubectl-port-forward branch from 26159f6 to 46802fe Compare December 30, 2020 06:41
@carlory
Copy link
Member Author

carlory commented Jan 4, 2021

Example:

➜  kubernetes git:(feature-kubectl-port-forward) ✗ _output/bin/kubectl  port-forward backend-mysql-55c8958756-b6sh2 3306 -- mysql -h 127.0.0.1 -uroot -p
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306
Enter password:
Handling connection for 3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.21 sec)

mysql> exit
Bye

@carlory carlory force-pushed the feature-kubectl-port-forward branch from 46802fe to 487c5cd Compare January 4, 2021 08:42
@carlory carlory changed the title [WIP] ability to run a local command with port-forward ability to run a local command with port-forward Jan 4, 2021
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 4, 2021
@carlory
Copy link
Member Author

carlory commented Jan 4, 2021

/assign @deads2k @FranciscoKurpiel

@k8s-ci-robot
Copy link
Contributor

@carlory: GitHub didn't allow me to assign the following users: FranciscoKurpiel.

Note that only kubernetes members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @deads2k @FranciscoKurpiel

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@FranciscoKurpiel
Copy link

FranciscoKurpiel commented Jan 4, 2021

@carlory, while trying your PR I come up with a few considerations regarding the standard streams (stdin/oiut/err). I described the considerations here the original ticket, so that they can be taken into consideration for the consideration on sig.

edit: durp!

@carlory
Copy link
Member Author

carlory commented Jan 5, 2021

@FranciscoKurpiel

Thanks for your guide. It is a good idea. I will try to implement it this week.

@carlory carlory changed the title ability to run a local command with port-forward [WIP] ability to run a local command with port-forward Jan 5, 2021
@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jan 5, 2021
@carlory carlory force-pushed the feature-kubectl-port-forward branch from 487c5cd to 31bbd51 Compare January 7, 2021 02:47
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 7, 2021
@carlory
Copy link
Member Author

carlory commented Jan 7, 2021

@FranciscoKurpiel suppress kubectl is implemented.

case 1:

➜  kubernetes git:(feature-kubectl-port-forward) ./_output/bin/kubectl port-forward -q backend-mysql-55c8958756-b6sh2 3306 -- mysql -h 127.0.0.1 -uroot -pdangerous -e "select version();"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| version() |
+-----------+
| 5.7.22    |
+-----------+

case 2:

➜  kubernetes git:(feature-kubectl-port-forward) ./_output/bin/kubectl port-forward -q backend-mysql-55c8958756-b6sh2 3306 -- mysql -h 127.0.0.1 -uroot -pdangerous -e "select version();" > /tmp/out.log 2> /tmp/err.log

➜  kubernetes git:(feature-kubectl-port-forward) cat /tmp/out.log
version()
5.7.22

➜  kubernetes git:(feature-kubectl-port-forward) cat /tmp/err.log
mysql: [Warning] Using a password on the command line interface can be insecure.

case 3:

➜  kubernetes git:(feature-kubectl-port-forward) ./_output/bin/kubectl port-forward -q backend-mysql-55c8958756-b6sh2 3306 -- mysql -h 127.0.0.1 -uroot -pdangerous -e "select version();" 2> /tmp/err.log | grep -v "version"
5.7.22

➜  kubernetes git:(feature-kubectl-port-forward) ./_output/bin/kubectl port-forward -q backend-mysql-55c8958756-b6sh2 3306 -- mysql -h 127.0.0.1 -uroot -pdangerous -e "select version();" | grep -v "version"
mysql: [Warning] Using a password on the command line interface can be insecure.
5.7.22

@carlory
Copy link
Member Author

carlory commented Jan 7, 2021

There is still a problem to be considered here.

If during the execution of the local command, the goroutine responsible for forwarding the network traffic exits suddenly, immediately returns the error of the goroutine, and exit the executing command, is it really reasonable?

go func() {
 	errChan <- o.PortForwarder.ForwardPorts("POST", req.URL(), o)
 }()

It may be a good idea to only print the port-forwarder error to stderr without interrupting the execution of the local command.

@FranciscoKurpiel Any ideas about it?

@carlory carlory changed the title [WIP] ability to run a local command with port-forward ability to run a local command with port-forward Jan 7, 2021
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 7, 2021
@FranciscoKurpiel
Copy link

If during the execution of the local command, the goroutine responsible for forwarding the network traffic exits suddenly, immediately returns the error of the goroutine, and exit the executing command, is it really reasonable?

That should be handled a broken pipe, we should send a SIGPIPE. Unfortunately there might be some platform-specific details there.

I'm just a bit worried with the fact that we are moving quite a lot before the ticket being accepted or discussed on the sig. I provided the ticket and I'm providing my input, but the actual specification will come from the special group.

@carlory
Copy link
Member Author

carlory commented Jan 14, 2021

/ok-to-test

@k8s-ci-robot
Copy link
Contributor

@carlory: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@carlory
Copy link
Member Author

carlory commented Jan 14, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 14, 2021
@carlory
Copy link
Member Author

carlory commented Jan 14, 2021

/test pull-kubernetes-bazel-test

@carlory
Copy link
Member Author

carlory commented Feb 5, 2021

/retest

@k8s-ci-robot
Copy link
Contributor

@carlory: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-kubernetes-bazel-test 71b4994 link /test pull-kubernetes-bazel-test
pull-kubernetes-verify 71b4994 link /test pull-kubernetes-verify
pull-kubernetes-bazel-build 71b4994 link /test pull-kubernetes-bazel-build

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 6, 2021
@carlory carlory closed this May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. 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.

Support exec in kubectl port-forward
5 participants