Execute command in Helm Chart

7/24/2020

I have a Helm Chart for Cassandra, which is running fine, I am able to connect to it and run cqlsh commands.
I want to add a Helm Hook to the chart. I've managed how to do it, however, I cannot execute cqlsh in the container. This is my Kubernetes Job I want to execute in post-install phase.

apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  template:
    metadata:
     name: hook-job
     annotations:
       "helm.sh/hook": post-install
       "helm.sh/hook-delete-policy": hook-succeeded
   spec:
     containers:
     - name: cqlsh-cmd
       image: <cassandra-image>
       command: ["bin/sh", "-c", "cqlsh"]
     restartPolicy: OnFailure
 

However, the cqlsh command is not found.

In general it seems odd I have to re-use the same container I have defined in Helm Chart. Am I doing something wrong?

-- Forin
cassandra
cql
cqlsh
kubernetes
kubernetes-helm

1 Answer

7/30/2020

your pod/container may not up at that time. Use it under post life cycle.

  spec:
    containers:
    - name: cqlsh-cmd
      image: <cassandra-image>
      lifecycle:
        postStart:
          exec:
            command: ["/bin/sh", "-c", "
                set -x\n
                while true;\n
                do\n
                  echo 'looking cassandra...,'\n
                  timeout 1 bash -c 'cat < /dev/null > /dev/tcp/localhost/9042'\n
                  exitCode=$?\n

                  if [ $exitCode = 0 ]; then\n
                    cqlsh /** your command **/ \n
                    break;\n
                  fi\n
                  sleep 1s\n
                done\n
              "]
-- Madhu Potana
Source: StackOverflow