---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab
  namespace: spack
  labels:
    app: gitlab
    svc: web
spec:
  selector:
    matchLabels:
      app: gitlab
      svc: web
  replicas: 1
  template:
    metadata:
      labels:
        app: gitlab
        svc: web
    spec:
      initContainers:
      # - name: nuke-it-all
      #   image: busybox
      #   imagePullPolicy: IfNotPresent
      #   command: [
      #     "find", "/data",
      #       "-mindepth", "1",
      #       "-maxdepth", "1",
      #       "-exec", "rm", "-rf", "{}", "+"]
      #   volumeMounts:
      #   - name: data
      #     mountPath: /data

      - name: update-permissions
        image: "gitlab/gitlab-ee:12.1.0-ee.0"
        imagePullPolicy: IfNotPresent
        args: ["update-permissions"]
        volumeMounts:
        - name: data
          mountPath: /etc/gitlab
          subPath: etc
        - name: data
          mountPath: /gitlab-data
          subPath: data
        - name: data
          mountPath: /var/opt/gitlab/git-data/repositories
          subPath: repo
        - name: data
          mountPath: /var/opt/gitlab/gitlab-ci
          subPath: ci
        - name: data
          mountPath: /var/opt/gitlab/gitlab-rails/shared/artifacts
          subPath: artifacts

      # [WORKAROUND] https://gitlab.com/gitlab-org/omnibus-gitlab/issues/2976
      - name: update-permission-fix
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ["chmod", "-R", "2770", "/repos"]
        volumeMounts:
        - name: data
          mountPath: /repos
          subPath: repo

      containers:
      - name: gitlab
        image: "gitlab/gitlab-ee:12.1.0-ee.0"
        imagePullPolicy: IfNotPresent
        # command: ["bash", "-c"]
        # args: ["echo WAITING ; sleep 360000"]
        env:
        - name: GITLAB_OMNIBUS_CONFIG
          value: |
            external_url 'http://gitlab'

            root_pass = ENV['GITLAB_ROOT_PASSWORD'];
            gitlab_rails['initial_root_password'] = (
                root_pass unless root_pass.to_s == '');

            gitlab_rails['gitlab_shell_ssh_port'] = 22;

            # NOTE(opadron): While setting the shared runner registration token
            # is possible, it only works when initially creating and seeding the
            # database.  Even though this token is required to register shared
            # runners, it is automatically changed on a regular basis and the
            # only way to view the value of the token is by manually visiting an
            # admin page.
            #
            # So, automatically creating and managing shared runners is simply
            # NOT SUPPORTED by Gitlab, since they require a secret that changes
            # without human intervention, but for which there is no supported
            # way to automatically query its value.
            #
            # https://gitlab.com/gitlab-org/gitlab-ce/issues/3703
            # https://gitlab.com/gitlab-org/gitlab-ce/issues/24030
            # https://gitlab.com/gitlab-org/gitlab-runner/issues/1727
            #
            # runner_token = ENV['GITLAB_RUNNER_TOKEN'];
            # gitlab_rails['initial_shared_runners_registration_token'] = (
            #     runner_token unless runner_token.to_s == '');

            prometheus_monitoring['enable'] = false
            postgresql['enable'] = false;
            gitlab_rails['db_adapter'] = 'postgresql';
            gitlab_rails['db_encoding'] = 'utf8';
            gitlab_rails['db_host'] = ENV['DB_HOST'];
            gitlab_rails['db_password'] = ENV['DB_PASSWORD'];
            gitlab_rails['db_username'] = ENV['DB_USER'];
            gitlab_rails['db_database'] = ENV['DB_DATABASE'];

            gitlab_rails['auto_migrate'] = (
              !File.exist?('/meta/initial_db_migrate'));

            redis['enable'] = false;
            gitlab_rails['redis_host'] = ENV['REDIS_HOST'];
            gitlab_rails['redis_password'] = ENV['REDIS_PASSWORD'];

            require 'fileutils'
            FileUtils.touch('/meta/initial_db_migrate');

        - name: GITLAB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-root-password
        - name: GITLAB_RUNNER_TOKEN
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-runner-token
        - name: DB_HOST
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-host
        - name: DB_USER
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-user
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef: {'name': 'gitlab-secrets', 'key': 'gitlab-db-password'}
        - name: DB_DATABASE
          valueFrom:
            secretKeyRef: {'name': 'gitlab-secrets', 'key': 'gitlab-db-name'}
        - name: REDIS_HOST
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-cache-host
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-cache-password
        ports:
        - name: http
          containerPort: 80
        livenessProbe:
          httpGet:
            path: /help
            port: 80
          initialDelaySeconds: 180
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 10
        readinessProbe:
          httpGet:
            path: /help
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        volumeMounts:
        - name: data
          mountPath: /etc/gitlab
          subPath: etc
        - name: data
          mountPath: /gitlab-data
          subPath: data
        - name: data
          mountPath: /var/opt/gitlab/git-data/repositories
          subPath: repo
        - name: data
          mountPath: /var/opt/gitlab/gitlab-ci
          subPath: ci
        - name: data
          mountPath: /var/opt/gitlab/gitlab-rails/shared/artifacts
          subPath: artifacts
        - name: data
          mountPath: /meta
          subPath: meta
      nodeSelector:
        "beta.kubernetes.io/instance-type": "m4.large"
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: gitlab-data
