<p>I have a situation where I want to speed up deployment time by caching the git resources into a shared PVC.</p>
<p>This is the bash script I use for checkout the resource and save into a share PVC folder</p>
<pre class="lang-bash prettyprint-override"><code>#!/bin/bash
src=&quot;$1&quot;
dir=&quot;$2&quot;

echo &quot;Check for existence of directory: $dir&quot;
if [ -d &quot;$dir&quot; ]
   then
     echo &quot;$dir found, no need to clone the git&quot;
  else
     echo &quot;$dir not found, clone $src into $dir&quot;
     mkdir -p $dir
     chmod -R 777 $dir
     git clone $src $dir
     echo &quot;cloned $dir&quot;
fi
</code></pre>
<p>Given I have a Deployment with more than 1 pods and each of them have an <code>initContainer</code>. The problem with this approach is all <code>initContainers</code> will start almost at the same time.</p>
<p>They all check for the existence of the git resource directory. Let's say first deployment we dont have the git directory yet. Then it will create the directory, then clone the resource. Now, the second and third initContainers see that the directory is already there so they finish immediately.</p>
<p>Is there a way to make other initContainers wait for the first one to finish?
After reading the kubernetes documentation, I don't think it's supported by default</p>
<p>Edit 1:</p>
<ul>
<li>The second solution I can think of is to deploy with 1 pod only, after a successful deployment, we will scale it out automatically. However I still don't know how to do this</li>
</ul>

Loc Truong
<p>I have found a workaround. The idea is to create a lock file, and write a script to wait until the lock file exists. In the initContainer, I prepare the script like this</p>
<pre class="lang-bash prettyprint-override"><code>#!/bin/bash
src=&quot;$1&quot;
dir=&quot;$2&quot;

echo &quot;Check for existence of directory: $dir/src&quot;
if [ -d &quot;$dir/src&quot; ]
   then
      echo &quot;$dir/src found, check if .lock is exist&quot;
      until [ ! -f $dir/.lock ]
      do
         sleep 5
         echo 'After 5 second, .lock is still there, I will check again'
      done
      echo &quot;Finish clone in other init container, I can die now&quot;
      exit
   else
      echo &quot;$dir not found, clone $src into $dir&quot;
      mkdir -p $dir/src
      echo &quot;create .lock, make my friends wait for me&quot;
      touch $dir/.lock
      ls -la $dir
      chmod -R 777 $dir
      git clone $src $dir/src
      echo &quot;cloned $dir&quot;
      echo &quot;remove .lock now&quot;
      rm $dir/.lock
fi

</code></pre>
<p>This kind of like a cheat, but it works. The script will make other <code>initContainers</code> wait until the .lock is remove. By then, the project is cloned already.</p>
