kuberneteskubernetes-helm

cp Command Fails to Copy JAR File to Target Folder


I am trying to copy a file (jar file) so that I can run the jar (java -jar) in my pod. But the copy command just doesn't work. The pod logs don't throw any error also.

My deployment.yaml looks like (in brief):

- name: glowroot-jar-init-container
image: "{{ .Values.images.repository }}/{{ .Values.config.aptm.image }}"
securityContext:
  runAsUser: 1000
  runAsGroup: 1000
  runAsNonRoot: true
  readOnlyRootFilesystem: true
imagePullPolicy: {{ .Values.images.pullPolicy }}
command: ["cp","/opt/tools/aptm/glowroot.jar","/aptm"]
volumeMounts:
  - name: aptm-data-glowroot
    mountPath: /aptm
.
.
.
.
.
.
containers:
- name: {{ template "name" . }}
  image: "{{ .Values.images.repository }}/com.gtt.ecomp.vod.dev/vod:{{ .Values.images.vodTag }}"
  imagePullPolicy: {{ .Values.images.pullPolicy }}
  securityContext:
    runAsUser: 1000
    runAsGroup: 1000
    runAsNonRoot: true
    readOnlyRootFilesystem: true
  command:
    - sh
    - -c
    - -x
    - >
      .
      .
      .
      .
      
      echo "Copying aptm JAR."
      cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm;

      .
      .
      .

      bash /mounted-config/start_tomcat.sh;
  args:
  - "30000"
.
.
.
.
- name: aptm-data
  mountPath: /opt/tools/aptm
- name: aptm-data-glowroot
  mountPath: /aptm
.
.
.
.
- name: aptm-data
  emptyDir: {}
- name: aptm-data-glowroot
  emptyDir: {}
 .
 .
 .

The file hram-agent-0.13.jar is present in the WEB-INF/lib/ folder. But when I do a bash and get into the pod to check the if the jar file was copied or not I do not see it.

vodadmin@vod-58867c5dc6-lg8ch:/usr/local/tomcat/webapps/vod/WEB-INF/lib$ ls -lrt hr*
-rw-r--r-- 1 vodadmin vodadmin 13864793 Apr 25 12:56 hram-agent-0.13.jar
vodadmin@vod-58867c5dc6-lg8ch:/usr/local/tomcat/webapps/vod/WEB-INF/lib$

But when cd to the target folder:

vodadmin@vod-58867c5dc6-lg8ch:/opt/tools/aptm$ ls -lrt
total 0

All the trouble started when I changed everything to read only root file system in my pod.


Solution

  • Just a quick guess -

    When you use > in a yaml, it stacks the lines of its data together into one line.

       - >
          echo "Copying aptm JAR."
          cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm;
          . . .
    

    becomes

    echo "Copying aptm JAR." cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm;
    

    which I bet outputs

    Copying aptm JAR. cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm
    

    So add a semicolon after the echo statement. Then

       - >
          echo "Copying aptm JAR.";
          cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm;
          . . .
    

    becomes

    echo "Copying aptm JAR."; cp /usr/local/tomcat/webapps/vod/WEB-INF/lib/hram-agent-0.13.jar /opt/tools/aptm;
    

    and might work.

    Or use a | instead, which preserves the internal newlines.

    I'm still suspicious of how the syntax gets delivered to the parser, though. Maybe write a script that wraps all that in a simpler call, and rebuild it into your image?