Reputation: 79
I noticed a strange behavior while experimenting with kubectl run
:
When the command to be executed is passed as option flag --command -- /bin/sh -c "ls -lah"
> OK
kubectl run nodejs --image=node:lts-alpine \
--restart=Never --quiet -i --rm \
--command -- /bin/sh -c "ls -lah"
When command to be executed is passed in --overrides
with "command": [ "ls", "-lah" ]
> OK
kubectl run nodejs --image=node:lts-alpine \
--restart=Never \
--overrides='
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "nodejs"
},
"spec": {
"volumes": [
{
"name": "host-volume",
"hostPath": {
"path": "/home/dferlay/Sources/df-sdc/web/themes/custom/"
}
}
],
"containers": [
{
"name": "nodejs",
"image": "busybox",
"command": [
"ls",
"-lah"
],
"workingDir": "/app",
"volumeMounts": [
{
"name": "host-volume",
"mountPath": "/app"
}
],
"terminationMessagePolicy": "FallbackToLogsOnError",
"imagePullPolicy": "IfNotPresent"
}
],
"restartPolicy": "Never",
"securityContext": {
"runAsUser": 1000,
"runAsGroup": 1000
}
}
}
' \
--quiet -i --rm
When the command to be executed is passed as option flag --command -- /bin/sh -c "ls -lah"
and --overrides
is used for something else (volume for instance) > KO
kubectl run nodejs --image=node:lts-alpine --restart=Never \
--overrides='
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "nodejs"
},
"spec": {
"volumes": [
{
"name": "host-volume",
"hostPath": {
"path": "/home/dferlay/Sources/df-sdc/web/themes/custom/"
}
}
],
"containers": [
{
"name": "nodejs",
"image": "busybox",
"workingDir": "/app",
"volumeMounts": [
{
"name": "host-volume",
"mountPath": "/app"
}
],
"terminationMessagePolicy": "FallbackToLogsOnError",
"imagePullPolicy": "IfNotPresent"
}
],
"restartPolicy": "Never",
"securityContext": {
"runAsUser": 1000,
"runAsGroup": 1000
}
}
}
' \
--quiet -i --rm --command -- /bin/sh -c "ls -lah"
So it looks like using --overrides
prevents --command
to be used.
However, I precisely need to use --command
to bypass the array format expected by --overrides
(ie. "command": [ "ls", "-lah" ]
) because in my use case the command is a placeholder and cannot be known in advance.
FYI: kubectl version=v1.23.1+k3s2
Upvotes: 4
Views: 6914
Reputation: 21
You can bypass the array format by using the args
field:
"command": [
"sh",
"-c"
],
"args": [ "pwd && id && node YOUR_COMMAND" ]
Upvotes: 2