kubernetes: kubectl exec does not accept pod/foobar syntax

I feel like I should be able to do:

$ P=$(kubectl get pods -l run=hairpin-test -o name)

$ kubectl exec --tty -i $P sh

but I get error: invalid resource name "pod/hairpin-test-1119317711-4qmtf": name may not contain "/"

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Has this been reverted again? I cannot use “pod/podname” for kubectl exec v1.11:

error: invalid resource name "pod/pop3-65987b5954-gtzvl": [may not contain '/']

^ Seconding the above. I’m getting the same ‘may not contain /’ error. Naturally it’s trivial to use unix commands to filter this, but this is such a basic feature and matches the way the rest of kubectl works, so it’s quite a shocking omission.

Just ran into this on a brand new default installation.

Local Mac Docker for Desktop Enabled k8 in preferences

helm init
helm repo update
helm install stable/postgresql
kubectl port-forward --namespace default svc/broken-gorilla-postgresql 5432:5432 &

error: invalid resource name “svc/broken-gorilla-postgresql”: [may not contain ‘/’]

Had the same problem. Updating kubectl resolved the issue for me.

Just ran into this on a brand new default installation.

Local Mac Docker for Desktop Enabled k8 in preferences

helm init
helm repo update
helm install stable/postgresql
kubectl port-forward --namespace default svc/broken-gorilla-postgresql 5432:5432 &

error: invalid resource name “svc/broken-gorilla-postgresql”: [may not contain ‘/’]

@laurieboyes If you use selectors, you can do something like this:

kubectl exec -it "$(kubectl get po -l app=$YOUR_APP_NAME -o name | cut -d '/' -f 2)" -c $YOUR_CONTAINER_NAME -- /bin/sh

Otherwise you’ll need to manually grep, something like this: kubectl exec -it "$(kubectl get po -o name | grep $YOUR_REGEX | cut -d '/' -f 2)" -c $YOUR_CONTAINER_NAME -- /bin/sh

$() is the syntax in bash to run the command and use its output as part of another command. You can also use backticks to achieve the same effect but that’s considered bad form by many (myself included). For example: example_ls_result=$(ls)

If going the grep route, take care to make sure your regex will only return one result, otherwise you’ll have to pipe it to xargs or something similar.