Assign variable within kubernetes yaml job

7/30/2019

I would like to run a command within the yaml file for kubernetes:

Here is the part of the yaml file that i use The idea is to calculate a precent value based on mapped and unmapped values. mapped and unmapped are set properly but the percent line fails I think the problem comes from the single quotes in the BEGIN statement of the awk command which i guess need to escape ???

If mapped=8 and unmapped=7992 Then percent is (8/(8+7992)*100) = 0.1%

  command: ["/bin/sh","-c"]
            args: [' 
                ...
                echo "Executing command" &&
                map=${grep -c "^@" outfile.mapped.fq} &&
                unmap=${grep -c "^@" outfile.unmapped.fq} && 
                percent=$(awk -v CONVFMT="%.10g" -v map="$map" -v unmap="$unmap" "BEGIN { print ((map/(unmap+map))*100)}") &&


                echo "finished" 
                  ']
-- david
awk
kubernetes

1 Answer

8/2/2019

Thanks to the community comments: Ed Morton & david

For those files with data, please create configmap:

outfile.mapped.fq
outfile.unmapped.fq

kubectl create configmap config-volume --from-file=/path_to_directory_with_files/

Create pod:

apiVersion: v1 
kind: Pod 
metadata: 
  name: awk-ubu 
spec: 
  containers: 
    - name: awk-ubuntu 
      image: ubuntu 
      workingDir: /test
      command: [ "/bin/sh", "-c" ] 
      args: 
        - echo Executing_command; 
          map=$(grep -c "^@" outfile.mapped.fq); 
          unmap=$(grep -c "^@" outfile.unmapped.fq); 
          percent=$(awk -v CONVFMT="%.10g" -v map="$map" -v unmap="$unmap" "BEGIN { print ((map/(unmap+map))*100)}"); 
          echo $percent;  
          echo Finished; 
      volumeMounts: 
      - name: special-config 
        mountPath: /test 
  volumes: 
    - name: special-config 
      configMap: 
        # Provide the name of the ConfigMap containing the files you want 
        # to add to the container 
        name: config-volume 
  restartPolicy: Never

Once completed verify the result:

kubectl logs awk-ubu

Executing_command
53.3333
Finished
-- Hanx
Source: StackOverflow