kubernetes `--dry-run=client` with `--command` depends on ordering of commands in a weird way

9/3/2021

I found that creating a yaml object description using --dry-run=client and providing --command only works when the provided arguments are in a very specific order.

This works:

k run nginx --image=nginx --restart=Never --dry-run=client -o yaml --command -- env > nginx.yaml

This does NOT:

k run nginx --image=nginx --restart=Never --command -- env --dry-run=client -o yaml > nginx.yaml

I feel a bit confused because the version that does not work looks a lot more intuitive to me then the one that does work. Ideally both should work in my opinion. Is this intended behavior? I can't find any documentation about it.

-- Max
kubectl
kubernetes

2 Answers

9/3/2021

Everything after -- is a positional argument (until the > which is a shell metachar), not an option.

-- coderanger
Source: StackOverflow

9/6/2021

Ideally both should work in my opinion.

Unfortunately, the commands you presented are not the same. They will never work the same either. This is correct behaviour. Double dash (--) is of special importance here:

a double dash (--) is used in most Bash built-in commands and many other commands to signify the end of command options, after which only positional arguments are accepted.

So you can't freely swap "parameters" places. Only these options can be freely set

--image=nginx --restart=Never --dry-run=client -o yaml --command

Then you have -- env (double dash, space, and another command). After -- (double dash and space) only positional arguments are accepted.

Additionally, > is shell meta-character to set redirection.

-- Mikołaj Głodziak
Source: StackOverflow