---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sonarqube
spec:
  serviceName: "sonarqube"
  selector:
    matchLabels:
      run: sonarqube
  template:
    metadata:
      labels:
        run: sonarqube
        app: sonarqube
    spec:
      nodeSelector:
        beta.kubernetes.io/arch: amd64
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-role.kubernetes.io/cloud
                operator: NotIn
                values:
                - "true"
      initContainers:
      - name: init-sysctl
        image: busybox
        command:
        - /bin/sh
        - -c
        - |
          sysctl -w vm.max_map_count=524288 && \
          sysctl -w fs.file-max=131072 && \
          ulimit -n 131072 && \
          ulimit -u 8192
        securityContext:
          privileged: true
          runAsUser: 0
      containers:
      - name: sonarqube
        image: sonarqube:9.3.0-community
        ports:
        - containerPort: 9000
          name: web
        volumeMounts:
        - name: snr-data
          mountPath: /opt/sonarqube/data
        - name: snr-logs
          mountPath: /opt/sonarqube/logs
        - name: snr-ext
          mountPath: /opt/sonarqube/extensions
        env:
          - name: SONARQUBE_JDBC_USERNAME
            valueFrom:
              secretKeyRef:
                name: sonar-postgres
                key: pgrootuser
          - name: SONARQUBE_JDBC_PASSWORD
            valueFrom:
              secretKeyRef:
                name: sonar-postgres
                key: pgpassword
          - name: SONARQUBE_JDBC_URL
            valueFrom:
              secretKeyRef:
                name: sonar-postgres
                key: jdbc-url
      volumes:
      - name: snr-data
        persistentVolumeClaim:
          claimName: snr-data-pvc-sonarqube-0
      - name: snr-logs
        persistentVolumeClaim:
          claimName: snr-logs-pvc-sonarqube-0
      - name: snr-ext
        persistentVolumeClaim:
          claimName: snr-ext-pvc-sonarqube-0
  volumeClaimTemplates:
  - metadata:
      name: snr-data-pvc
      finalizers:
        - kubernetes.io/pv-protection
    spec:
      storageClassName: "nfs-client"
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi
  - metadata:
      name: snr-logs-pvc
      finalizers:
        - kubernetes.io/pv-protection
    spec:
      storageClassName: "nfs-client"
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 2Gi
  - metadata:
      name: snr-ext-pvc
      finalizers:
        - kubernetes.io/pv-protection
    spec:
      storageClassName: "nfs-client"
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi
