2

I need to create a volume to expose the maven .m2 folder to be reused in all my projects but I can't do that at all.

My gitlab runner is running inside my kuberentes cluster as a container.

Follows Deployment and configmap

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab-runner
  namespace: default
spec:
  template:
    metadata:
      labels:
        name: gitlab-runner
    spec:
      serviceAccountName: gitlab-sa
      nodeName: 140.6.254.244
      containers:
        - name: gitlab-runner
          image: gitlab/gitlab-runner
          securityContext:
            privileged: true
          command: ["/bin/bash", "/scripts/entrypoint"]
          env:
            - name: KUBERNETES_NAMESPACE
              value: default 
            - name: KUBERNETES_SERVICE_ACCOUNT
              value: gitlab-sa
          # This references the previously specified configmap and mounts it as a file
          volumeMounts:
            - mountPath: /scripts
              name: configmap
          livenessProbe:
            exec:
              command: ["/usr/bin/pgrep","gitlab.*runner"]
            initialDelaySeconds: 60
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            exec:
              command: ["/usr/bin/pgrep","gitlab.*runner"]
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3

      volumes:
      - configMap:
          name: gitlab-runner-cm
        name: configmap

ConfigMap:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: gitlab-runner-cm
  namespace: default 
data:
  entrypoint: |
    #!/bin/bash

    set -xe

    cp /scripts/config.toml /etc/gitlab-runner/

    # Register the runner
    /entrypoint register --non-interactive --registration-token ###### --url http://gitlab.######.net --clone-url http://gitlab.######.net --executor "kubernetes" --name "Kubernetes Runner" --config "/etc/gitlab-runner/config.toml"

    # Start the runner
    /entrypoint run --user=gitlab-runner \
      --working-directory=/home/gitlab-runner \
      --config "/etc/gitlab-runner/config.toml"
  config.toml: |
    concurrent = 50 
    check_interval = 10
    [[runners]]
      name            = "PC-CVO"
      url             = "http://gitlab.######.net"
      token           = "######"
      executor = "kubernetes"
      cache_dir = "/tmp/gitlab/cache"
      [runners.kubernetes]
        [runners.kubernetes.volumes]
          [[runners.kubernetes.volumes.host_path]]
            name = "maven"
            mount_path = "/.m2/"
            host_path = "/mnt/dados/volumes/maven-gitlab-ci"
            read_only = false

          [[runners.kubernetes.volumes.host_path]]
            name = "gitlab-cache"
            mount_path = "/tmp/gitlab/cache"
            host_path = "/mnt/dados/volumes/maven-gitlab-ci-cache"
            read_only = false



But even putting [[runners.kubernetes.volumes.host_path]] as informed in the documentation my volume is not mounted on the host, I tried to use a pv and pvc, but nothing worked, anyone has a light on how to expose this .m2 folder on the host so all my jobs can share it without caching?

Vinicius Santos
  • 323
  • 4
  • 14
  • Here is an [example](https://stackoverflow.com/questions/55741050/how-to-add-persistent-volume-for-maven-in-gitlab-with-kubernetes-runner/55748306#55748306e) with NFS. Could be interesting to add your `.gitlab-ci.yml` where you refer the `host_path` in your answer – Nicolas Pepinster Sep 24 '19 at 05:35
  • Unfortunately nfs is not an option for me, but I already solved the problem, I will post the solution. – Vinicius Santos Sep 26 '19 at 19:42

2 Answers2

0

After beating my head with name resolution issues with internal DNS, volumes for my m2 and using the docker daemon instead of docker: dind, I finally got a configuration that solves my problem, below is the final configuration files if anyone passes for the same problem. The main problem was that when the runner was registered the config.toml file was modified by the registration process and this overwrites my settings, to solve this I made a cat after the container registration.

Deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab-runner
  namespace: default
spec:
  template:
    metadata:
      labels:
        name: gitlab-runner
    spec:
      serviceAccountName: gitlab-sa
      nodeName: 140.6.254.244
      containers:
        - name: gitlab-runner
          image: gitlab/gitlab-runner
          securityContext:
            privileged: true
          command: ["/bin/bash", "/scripts/entrypoint"]
          env:
            - name: KUBERNETES_NAMESPACE
              value: default 
            - name: KUBERNETES_SERVICE_ACCOUNT
              value: gitlab-sa
          # This references the previously specified configmap and mounts it as a file
          volumeMounts:
            - mountPath: /scripts
              name: configmap
          livenessProbe:
            exec:
              command: ["/usr/bin/pgrep","gitlab.*runner"]
            initialDelaySeconds: 60
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            exec:
              command: ["/usr/bin/pgrep","gitlab.*runner"]
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3

      volumes:
      - configMap:
          name: gitlab-runner-cm
        name: configmap

Config Map (Here is the solution!)

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: gitlab-runner-cm
  namespace: default 
data:
  entrypoint: |
    #!/bin/bash

    set -xe

    cp /scripts/config.toml /etc/gitlab-runner/

    # Register the runner
    /entrypoint register --non-interactive --registration-token ############ --url http://gitlab.######.net --clone-url http://gitlab.######.net --executor "kubernetes" --name "Kubernetes Runner" --config "/etc/gitlab-runner/config.toml"

    cat >> /etc/gitlab-runner/config.toml << EOF
          [[runners.kubernetes.volumes.host_path]]
            name = "docker"
            path = "/var/run/docker.sock"
            mount_path = "/var/run/docker.sock"
            read_only = false
          [[runners.kubernetes.volumes.host_path]]
            name = "maven"
            mount_path = "/.m2/"
            host_path = "/mnt/dados/volumes/maven-gitlab-ci"
            read_only = false
          [[runners.kubernetes.volumes.host_path]]
            name = "resolvedns"
            mount_path = "/etc/resolv.conf"
            read_only = true
            host_path = "/etc/resolv.conf"

    EOF



    # Start the runner
    /entrypoint run --user=gitlab-runner \
      --working-directory=/home/gitlab-runner \
      --config "/etc/gitlab-runner/config.toml"
  config.toml: |
    concurrent = 50 
    check_interval = 10
    [[runners]]
      name            = "PC-CVO"
      url             = "http://gitlab.########.###"
      token           = "##############"
      executor = "kubernetes"
      cache_dir = "/tmp/gitlab/cache"
      [runners.kubernetes]


Vinicius Santos
  • 323
  • 4
  • 14
0

Check if GitLab 15.6 (November 2022) can help:

Mount ConfigMap to volumes with the Auto Deploy chart

The default Auto Deploy Helm chart now supports the extraVolumes and extraVolumeMounts options.

In past releases, you could specify only Persistent Volumes for Kubernetes.

Among other use cases, you can now mount:

  • Secrets and ConfigMaps as files to Deployments, CronJobs, and Workers.
  • Existing or external Persistent Volumes Claims to Deployments, CronJobs, and Workers.
  • Private PKI CA certificates with hostPath mounts to achieve trust with the PKI.

Thanks to Maik Boltze for this community contribution.

See Documentation and Issue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250