1

I have misunderstanding with how to execute $() commands in exec. i'm creating a job in kubernetes with this params:

command:
    - ./kubectl
    - -n
    - $MONGODB_NAMESPACE
    - exec
    - -ti
    - $(kubectl
    - -n
    - $MONGODB_NAMESPACE
    - get
    - pods
    - --selector=app=$MONGODB_CONTAINER_NAME
    - -o
    - jsonpath='{.items[*].metadata.name}')
    - --
    - /opt/mongodb-maintenance.sh

but the part with $(kubectl -n ... --selector ...) is treated as a string and don't execute. Please tell me how to do it properly. Thanks!

2 Answers2

0

As far as I know this is not achievable by putting each section as an array element. Instead you can do something like the following:

command:
    - /bin/sh
    - -c
    - |        
      ./kubectl -n $MONGODB_NAMESPACE exec -ti $(kubectl -n $MONGODB_NAMESPACE get pods --selector=app=$MONGODB_CONTAINER_NAME -o jsonpath='{.items[*].metadata.name}') -- /opt/mongodb-maintenance.sh
dippynark
  • 2,743
  • 20
  • 58
  • don't u now also how to add arguments to /opt/mongodb-maintenance.sh script? if i add any, i get Error: unknown flag: --selector. i.e. `/opt/mongodb-maintenance.sh -u user -p password` – Alexandr Kondratiev Mar 13 '18 at 10:27
  • @AlexandrKondratiev that sounds like a issue with the rest of the command, I can't see any problems with adding those flags to the end. Double check that removing the `-u` and `-p` flags and leaving the rest of command exactly the same works as expected – dippynark Mar 13 '18 at 11:48
  • The above notation should work just like a normal shell script, so you may want to try getting it to work within the container using a normal shell script file and then copying the script (including the shebang) into your Pod manifest – dippynark Mar 13 '18 at 11:51
  • 1
    pod=$(kubectl -n $MONGODB_NAMESPACE get pods -l app=$MONGODB_CONTAINER_NAME -o jsonpath='{.items[*].metadata.name}'); kubectl -n $MONGODB_NAMESPACE exec -ti $pod -- /opt/mongodb-maintenance.sh that's finally worked for me – Alexandr Kondratiev Mar 13 '18 at 11:58
0

From the output of the kubectl exec, I noticed that you can use -- to separate your arguments

# List contents of /usr from the first container of pod 123456-7890 and sort by modification time.
# If the command you want to execute in the pod has any flags in common (e.g. -i),
# you must use two dashes (--) to separate your command's flags/arguments.
# Also note, do not surround your command and its flags/arguments with quotes
# unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr").

kubectl exec 123456-7890 -i -t -- ls -t /usr
What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42