<p>Is it possible to use kustomize to specify a volume hostPath from an env variable?</p>
<p>I have a Kubernetes manifest that describes my deployment consisting of a container.
During development, I use a different image (that contains dev tools) and mount code from my host into the container.   This way I can make code changes without having to re-deploy.</p>
<p>I'm using a patchStategicMerge to replace the production image, with the one I want to use during dev and mount the code into the container i.e.</p>
<p>kustomization.yaml</p>
<pre><code>kind: Kustomization

bases:
- ../../base

patchesStrategicMerge:
- my-service.yaml
</code></pre>
<p>my-service.yaml</p>
<pre><code>---
apiVersion: apps/v1
...
...
    spec:
      containers:
        - name: myservice
          image: myservice-dev-image:1.0.0
          command: ['CompileDaemon',  '--build=make build', '--command=./myservice']
          volumeMounts:
          - name: code
            mountPath: /go/src/app
      volumes:
      - name: code
        hostPath:
          path: /source/mycodepath/github.com/myservice
</code></pre>
<p>What I'd like to do is make the path configurable via an environment variable, so that I don't have to check my specific path (/source/mycodepath/) into git, and so that other developers can easily use it in their own environment.</p>
<p>Is it possible to do this with kustomize ?</p>

<p>Create following directory structure</p>
<ul>
<li>k8s</li>
<li>k8s/base</li>
<li>k8s/overlays</li>
<li>k8s/overlays/bob</li>
<li>k8s/overlays/sue</li>
</ul>
<p>First we need to create the base. The base is the default template and it provide the bits that apply to both people. In <code>k8s/base</code> create a file called <code>app.yaml</code> and populate with the following (actually paste yours here. You can put other common bits in there too separated by a <code>---</code> and new line).</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myservice
  namespace: default
spec:
  strategy:
    type: RollingUpdate
  replicas: 1
  template:
    metadata:
      labels:
        name: myservice
        app: myservice
    spec:
      containers:
        - name: myservice
          image: myservice-dev-image:1.0.0
          command: ['CompileDaemon',  '--build=make build', '--command=./myservice']
          volumeMounts:
          - name: code
            mountPath: /go/src/app
      volumes:
      - name: code
        hostPath:
          path: /xxx
</code></pre>
<p>Next in the same directory (<code>k8s/base</code>) create another file called <code>kustomization.yaml</code> and populate with:</p>
<pre class="lang-yaml prettyprint-override"><code>resources:
 - app.yaml
</code></pre>
<p>Next we will create two overlays: one for Bob and one for Sue.</p>
<p>In <code>k8s/overlays/bob</code> let's create Bob's custom changes as <code>app.yaml</code> and populate with the following:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myservice
  namespace: default
spec:
  template:
    spec:
      volumes:
      - name: code
        hostPath:
          path: /users/bob/code
</code></pre>
<p>Now also in <code>k8s/overlays/bob</code> create another file called <code>kustomization.yaml</code> with the following:</p>
<pre class="lang-yaml prettyprint-override"><code>resources:
  - ../../base
patchesStrategicMerge:
  - app.yaml
</code></pre>
<p>We can copy the two files in <code>k8s/overlays/bob</code> into the <code>k8s/overlays/sue</code> directory and just change the path in the <code>volumes:</code> bit.</p>
<p>Next we need to do a <code>kustomize build</code> to generate the resulting versions - bob and sue.</p>
<p>If the <code>k8s</code> directory is in your code directory, open terminal (with Kustomize installed and run:</p>
<pre class="lang-sh prettyprint-override"><code>kustomize build k8s/overlays/bob
</code></pre>
<p>That should show you what Bob's kustomization will look like:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myservice
  namespace: default
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: myservice
        name: myservice
    spec:
      containers:
      - command:
        - CompileDaemon
        - --build=make build
        - --command=./myservice
        image: myservice-dev-image:1.0.0
        name: myservice
        volumeMounts:
        - mountPath: /go/src/app
          name: code
      volumes:
      - hostPath:
          path: /users/bob/code
        name: code
</code></pre>
<p>To apply that you can run:</p>
<pre class="lang-sh prettyprint-override"><code>kustomize build k8s/overlays/bob | kubectl apply -f -
</code></pre>
<p>To apply Sue you can run:</p>
<pre class="lang-sh prettyprint-override"><code>kustomize build k8s/overlays/sue| kubectl apply -f -
</code></pre>
<p>Yaml is sensitive about spaces and I'm not sure this will sit well in a Stackoverflow answer so I've put on Github as well: <a href="https://github.com/just1689/kustomize-local-storage" rel="noreferrer">https://github.com/just1689/kustomize-local-storage</a></p>
