---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jenkins-master
  namespace: default
  labels:
    app: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
      component: jenkins-master
      storageos.com/replicas: "1"
  serviceName: jenkins
  template:
    metadata:
      labels:
        app: jenkins
        component: jenkins-master
        storageos.com/replicas: "1"
    spec:
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
      serviceAccountName: "jenkins"
      initContainers:
        - name: "copy-default-config"
          image: "jenkins/jenkins:lts"
          imagePullPolicy: "Always"
          command: [ "sh", "/var/jenkins_config/apply_config.sh" ]
          env:
            - name: ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: jenkins
                  key: jenkins-admin-password
            - name: ADMIN_USER
              valueFrom:
                secretKeyRef:
                  name: jenkins
                  key: jenkins-admin-user
          resources:
            limits:
              cpu: 2000m
              memory: 4096Mi
            requests:
              cpu: 50m
              memory: 256Mi
          volumeMounts:
            - mountPath: /tmp
              name: tmp
            - mountPath: /var/jenkins_home
              name: jenkins-home
            - mountPath: /var/jenkins_config
              name: jenkins-config
            - mountPath: /usr/share/jenkins/ref/secrets/
              name: secrets-dir
            - mountPath: /usr/share/jenkins/ref/plugins
              name: plugins
            - mountPath: /var/jenkins_plugins
              name: plugin-dir
      containers:
        - name: jenkins
          image: "jenkins/jenkins:lts"
          imagePullPolicy: "Always"
          args: [ "--argumentsRealm.passwd.$(ADMIN_USER)=$(ADMIN_PASSWORD)",  "--argumentsRealm.roles.$(ADMIN_USER)=admin"]
          env:
            - name: JAVA_OPTS
              value: "-Xmx1500m"
            - name: JENKINS_OPTS
              value: ""
            - name: JENKINS_SLAVE_AGENT_PORT
              value: "50000"
            - name: ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: jenkins
                  key: jenkins-admin-password
            - name: ADMIN_USER
              valueFrom:
                secretKeyRef:
                  name: jenkins
                  key: jenkins-admin-user
          ports:
            - containerPort: 8080
              name: http
            - containerPort: 50000
              name: slavelistener
          livenessProbe:
            httpGet:
              path: "/login"
              port: http
            initialDelaySeconds: 90
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: "/login"
              port: http
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          resources:
            limits:
              cpu: 2000m
              memory: 4096Mi
            requests:
              cpu: 50m
              memory: 256Mi
          volumeMounts:
            - mountPath: /tmp
              name: tmp
            - mountPath: /var/jenkins_home
              name: jenkins-home
              readOnly: false
            - mountPath: /var/jenkins_config
              name: jenkins-config
              readOnly: true
            - mountPath: /usr/share/jenkins/ref/secrets/
              name: secrets-dir
              readOnly: false
            - mountPath: /usr/share/jenkins/ref/plugins/
              name: plugin-dir
              readOnly: false
      volumes:
      - name: plugins
        emptyDir: {}
      - name: tmp
        emptyDir: {}
      - name: jenkins-config
        configMap:
          name: jenkins
      - name: secrets-dir
        emptyDir: {}
      - name: plugin-dir
        emptyDir: {}
  volumeClaimTemplates:
  - metadata:
      name: jenkins-home
      labels:
        app: jenkins-master
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "fast" # StorageOS storageClass 
      resources:
        requests:
          storage: 50Gi
