kubectl icon indicating copy to clipboard operation
kubectl copied to clipboard

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

Open tthvo opened this issue 4 months ago • 2 comments

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:

  • Kubernetes client and server versions (use kubectl version):
    clientVersion:
      buildDate: "2024-04-17T17:36:05Z"
      compiler: gc
      gitCommit: 7c48c2bd72b9bf5c44d21d7338cc7bea77d0ad2a
      gitTreeState: clean
      gitVersion: v1.30.0
      goVersion: go1.22.2
      major: "1"
      minor: "30"
      platform: linux/amd64
    
  • Cloud provider or hardware configuration: OpenShift Local (CRC) v2.37.1 (Openshift v4.15.14)
  • OS (e.g: cat /etc/os-release): Fedora Linux Workstation 40

tthvo avatar Oct 17 '24 07:10 tthvo