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

kubectl should be able to be used to run (apply) config files via shebang #73874

Open
ssbarnea opened this Issue Feb 9, 2019 · 4 comments

Comments

Projects
None yet
4 participants
@ssbarnea
Copy link

ssbarnea commented Feb 9, 2019

Having to add kubectl -f filename.yaml to apply config files each time may prove to waste valuable keystrokes and we could make much easier for people to just execute the config files.

Partial support for implementing this is already made as kubectl already accepts stdin input via kubetl apply -f - but still the is not able to create config files that looks like:

#!/usr/bin/env kubectl apply -f -
---
# rest of yaml file

I mention that this feature is already supported by other tools, and one notable example ansible-playbook.

I was even able to make create a proxy bash script kubex that implements this behavior:

#!/usr/bin/env bash
# Add this shebang and make the yaml file executable:
#!/usr/bin/env kubex
set -euo pipefail
[ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
cat $input | kubectl apply -f - $*

When trying to use kubectl itself for this, it seems that the CLI parsing in kubectl is failing to detect that it was called via shebang call and fails with something like:

Error: unknown command "./foo.yaml" for "kubectl"

This is happening because when shebang execution happen the first argument is the script name and not the kubectl command. I implemented detection for this and in the past and doing a pop on args should make it work.

While it is possible to implement this missing feature in another utilitu script, it would be the best to implement this directly in kubectl as it could improve the user experience.

@kubernetes/sig-cli-feature-requests

@ssbarnea

This comment has been minimized.

Copy link
Author

ssbarnea commented Feb 9, 2019

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli and removed needs-sig labels Feb 9, 2019

@smarterclayton

This comment has been minimized.

Copy link
Contributor

smarterclayton commented Feb 10, 2019

That’s in interesting bug that kubectl is dependent on the args[0], which is not what I would expect.

We should see if we can fix the bug, but I’m not sure shebang support is something we anticipated supporting. Why isn’t the shim sufficient?

@ssbarnea

This comment has been minimized.

Copy link
Author

ssbarnea commented Feb 11, 2019

@smarterclayton I will investigate more during the weekend as it would be really nice to make it work without external tools. I raised the bug to get some feedback before trying to implement that feature as I wanted to get some confirmation first.

@soltysh

This comment has been minimized.

Copy link
Contributor

soltysh commented Feb 11, 2019

That’s in interesting bug that kubectl is dependent on the args[0], which is not what I would expect.

The quoted error clearly states it's working as expected:

Error: unknown command "./foo.yaml" for "kubectl"

that's cobra returning it can't find a command named ./foo.yaml, which is perfectly fine and I'm not quite sure what you'd like to fix here.

but I’m not sure shebang support is something we anticipated supporting. Why isn’t the shim sufficient?

Yeah, I'm not comfortable with that either. Somehow text files (yaml being one of them) are not meant for shebangs imho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment