Home > Net >  unable to execute a bash script in k8s cronjob pod's container
unable to execute a bash script in k8s cronjob pod's container

Time:12-06

Team, /bin/bash: line 5: ./repo/clone.sh: No such file or directory

cannot run above file but I can cat it well. I tried my best and still trying to find but no luck so far..

my requirement is to mount bash script from config map to a directory inside container and run it to clone a repo but am getting below message.

cron job

spec:
  concurrencyPolicy: Allow
  jobTemplate:
    metadata:
    spec:
      template:
        metadata:
        spec:
          containers:
          - args:
            - -c
            - |
              set -x
              pwd && ls
              ls -ltr /
              cat /repo/clone.sh
              ./repo/clone.sh
              pwd
            command:
            - /bin/bash
            envFrom:
            - configMapRef:
                name: sonarscanner-configmap
            image: artifactory.build.team.com/product-containers/user/sonarqube-scanner:4.7.0.2747
            imagePullPolicy: IfNotPresent
            name: sonarqube-sonarscanner
            securityContext:
              runAsUser: 0
            volumeMounts:
            - mountPath: /repo
              name: repo-checkout
          dnsPolicy: ClusterFirst
          initContainers:
          - args:
            - -c
            - cd /
            command:
            - /bin/sh
            image: busybox
            imagePullPolicy: IfNotPresent
            name: clone-repo
            securityContext:
              privileged: true
            volumeMounts:
            - mountPath: /repo
              name: repo-checkout
              readOnly: true
          restartPolicy: OnFailure
          securityContext:
            fsGroup: 0
          volumes:
          - configMap:
              defaultMode: 420
              name: product-configmap
            name: repo-checkout
  schedule: '*/1 * * * *'

ConfigMap

kind: ConfigMap
metadata:
apiVersion: v1
data:
  clone.sh: |-
    #!bin/bash
    set -xe
    apk add git curl
    #Containers that fail to resolve repo url can use below step.
    repo_url=$(nslookup ${CODE_REPO_URL} | grep Non -A 2 | grep Name | cut -d: -f2)
    repo_ip=$(nslookup ${CODE_REPO_URL} | grep Non -A 2 | grep Address | cut -d: -f2)
    if grep ${repo_url} /etc/hosts; then
        echo "git dns entry exists locally"
    else
        echo "Adding dns entry for git inside container"
        echo ${repo_ip} ${repo_url} >> /etc/hosts
    fi
    cd / && cat /etc/hosts && pwd
    git clone "https://$RU:$RT@${CODE_REPO_URL}/r/a/${CODE_REPO_NAME}" && \
        (cd "${CODE_REPO_NAME}" && mkdir -p .git/hooks && \
        curl -Lo `git rev-parse --git-dir`/hooks/commit-msg \
        https://$RU:$RT@${CODE_REPO_URL}/r/tools/hooks/commit-msg; \
        chmod  x `git rev-parse --git-dir`/hooks/commit-msg)
    cd ${CODE_REPO_NAME}
    pwd

output pod describe

  Warning  FailedCreatePodSandBox  1s    kubelet, node1  Failed create pod sandbox: rpc error: code = Unknown desc = failed to start sandbox container for pod "sonarqube-cronjob-1670256720-fwv27": Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:303: getting the final child's pid from pipe caused \"EOF\"": unknown

pod logs

  pwd
  ls
/usr/src
  ls -ltr /repo/clone.sh
lrwxrwxrwx    1 root     root            15 Dec  5 16:26 /repo/clone.sh -> ..data/clone.sh
  ls -ltr
total 60
.
drwxr-xr-x    2 root     root          4096 Aug  9 08:58 sbin
drwx------    2 root     root          4096 Aug  9 08:58 root
drwxr-xr-x    2 root     root          4096 Aug  9 08:58 mnt
drwxr-xr-x    5 root     root          4096 Aug  9 08:58 media
drwxrwsrwx    3 root     root          4096 Dec  5 16:12 repo <<<<< MY MOUNTED DIR
.
  cat /repo/clone.sh
#!bin/bash
set -xe
apk add git curl
#Containers that fail to resolve repo url can use below step.
repo_url=$(nslookup ${CODE_REPO_URL} | grep Non -A 2 | grep Name | cut -d: -f2)
repo_ip=$(nslookup ${CODE_REPO_URL} | grep Non -A 2 | grep Address | cut -d: -f2)
if grep ${repo_url} /etc/hosts; then
    echo "git dns entry exists locally"
else
    echo "Adding dns entry for git inside container"
    echo ${repo_ip} ${repo_url} >> /etc/hosts
fi
cd / && cat /etc/hosts && pwd
git clone "https://$RU:$RT@${CODE_REPO_URL}/r/a/${CODE_REPO_NAME}" && \
    (cd "${CODE_REPO_NAME}" && mkdir -p .git/hooks && \
    curl -Lo `git rev-parse --git-dir`/hooks/commit-msg \
    https://$RU:$RT@${CODE_REPO_URL}/r/tools/hooks/commit-msg; \
    chmod  x `git rev-parse --git-dir`/hooks/commit-msg)
cd code_dir
  ./repo/clone.sh
/bin/bash: line 5: ./repo/clone.sh: No such file or directory
  pwd
pwd/usr/src

CodePudding user response:

Assuming the working directory is different thant /:

If you want to source your script in the current process of bash (shorthand .) you have to add a space between the dot and the path:

. /repo/clone.sh

If you want to execute it in a child process, remove the dot:

/repo/clone.sh

  • Related