Use sub directory with kubernetes gitRepo volume mounts

5/27/2016

Following snippet is from kubernetes official documentation (http://kubernetes.io/docs/user-guide/volumes/#gitrepo):

apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath
      name: git-volume
  volumes:
  - name: git-volume
    gitRepo:
      repository: "git@somewhere:me/my-git-repository.git"
      revision: "22f1d8406d464b0c0874075539c1f2e96c253775"

The above will mount complete git repo inside the container at /mypath. Is there a way to mount only specific sub-directory inside of the git repo inside the container at /mypath?

-- Anand Patel
kubernetes

2 Answers

4/27/2017

No, you can't do this via Kubernetes gitRepo volume plugin. You can only do it manually or use your own volume plugin.

-- Crazykev
Source: StackOverflow

4/27/2017

Yes there is a way to do it. See following example. I have git repo which has sub directory configs.

So here is pod file which I am using:

apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath/
      name: git-volume
      subPath: "hitcounter/configs"
  volumes:
  - name: git-volume
    gitRepo:
      repository: "https://github.com/surajssd/hitcounter"
      revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"

Note the field subPath in containers in volumeMounts. There you specify what sub-directory from the volume you want to mount at /mypath inside your container.

docs say:

$ kubectl explain pod.spec.containers.volumeMounts.subPath
FIELD: subPath <string>

DESCRIPTION:
     Path within the volume from which the container's volume should be mounted.
     Defaults to "" (volume's root).

OR

create pod config file like this

apiVersion: v1
kind: Pod
metadata:
  name: server
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /mypath/
      name: git-volume
      subPath: "configs"
  volumes:
  - name: git-volume
    gitRepo:
      repository: "https://github.com/surajssd/hitcounter"
      revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"
      directory: "."

The difference here is I have specified directory: "." this ensures that mount point /mypath will become git repo and also the subPath: "configs" has changed since there is no extra directory hitcounter.

$ kubectl explain pod.spec.volumes.gitRepo.directory
FIELD: directory <string>

DESCRIPTION:
     Target directory name. Must not contain or start with '..'.  If '.' is
     supplied, the volume directory will be the git repository.  Otherwise, if
     specified, the volume will contain the git repository in the subdirectory
     with the given name.

HTH

-- surajd
Source: StackOverflow