Coder Jones
Coder Jones

Reputation: 93

Use kustomize to set hostPath path

Is it possible to use kustomize to specify a volume hostPath from an env variable?

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.

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.

kustomization.yaml

kind: Kustomization

bases:
- ../../base

patchesStrategicMerge:
- my-service.yaml

my-service.yaml

---
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

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.

Is it possible to do this with kustomize ?

Upvotes: 5

Views: 3237

Answers (1)

Justin Tamblyn
Justin Tamblyn

Reputation: 733

Create following directory structure

  • k8s
  • k8s/base
  • k8s/overlays
  • k8s/overlays/bob
  • k8s/overlays/sue

First we need to create the base. The base is the default template and it provide the bits that apply to both people. In k8s/base create a file called app.yaml and populate with the following (actually paste yours here. You can put other common bits in there too separated by a --- and new line).

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

Next in the same directory (k8s/base) create another file called kustomization.yaml and populate with:

resources:
 - app.yaml

Next we will create two overlays: one for Bob and one for Sue.

In k8s/overlays/bob let's create Bob's custom changes as app.yaml and populate with the following:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myservice
  namespace: default
spec:
  template:
    spec:
      volumes:
      - name: code
        hostPath:
          path: /users/bob/code

Now also in k8s/overlays/bob create another file called kustomization.yaml with the following:

resources:
  - ../../base
patchesStrategicMerge:
  - app.yaml

We can copy the two files in k8s/overlays/bob into the k8s/overlays/sue directory and just change the path in the volumes: bit.

Next we need to do a kustomize build to generate the resulting versions - bob and sue.

If the k8s directory is in your code directory, open terminal (with Kustomize installed and run:

kustomize build k8s/overlays/bob

That should show you what Bob's kustomization will look like:

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

To apply that you can run:

kustomize build k8s/overlays/bob | kubectl apply -f -

To apply Sue you can run:

kustomize build k8s/overlays/sue| kubectl apply -f -

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: https://github.com/just1689/kustomize-local-storage

Upvotes: 5

Related Questions