kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.84k stars 919 forks source link

port-forward: Empty remote port should cause the cmd to exit error? #1672

Open tthvo opened 4 hours ago

tthvo commented 4 hours ago

What happened:

From the usage description of the kubectl port-forward command:

Usage:
  kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

I suppose the REMOTE_PORT is required, meaning non-empty? However, when I used kubectl port-forward with an empty remote port, for example, kubectl port-forward pods/my-chart-app-v4-79747999fd-fw7nz 8080: (didn't mean to do that but happened to), the command picks up the first port without a name.

What you expected to happen:

I expect the command to exit error and print a warning about it.

How to reproduce it (as minimally and precisely as possible):

Follow the tutorial here: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

$ kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-deployment.yaml
$ kubectl wait --for=condition=available --timeout=60s deployment/mongo
# Change mongo-75f59d57f4-4nd6q to the name of the Pod
# Notice how the REMOTE_PORT is empty
$ kubectl port-forward mongo-75f59d57f4-4nd6q 28015:

You should see the output:

Forwarding from 127.0.0.1:28015 -> 27017
Forwarding from [::1]:28015 -> 27017

I expect this to fail instead.

Anything else we need to know?:

Sorry if this is the intended behaviour for kubectl. Then, this issue is invalid.

Otherwise, I think this block of code just needs to check for empty remotePort and returns an error.

https://github.com/kubernetes/kubectl/blob/f829c4b78958aade1b0585e4b1816db636e3098d/pkg/cmd/portforward/portforward.go#L228-L236

Environment:

k8s-ci-robot commented 4 hours ago

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

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](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.