apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: happy2-server
spec:
  serviceName: happy2-server-headless
  replicas: 1
  selector:
    matchLabels:
      app: happy2-server
  template:
    metadata:
      labels:
        app: happy2-server
    spec:
      securityContext:
        fsGroup: 1000
        runAsGroup: 1000
        runAsUser: 1000
        seccompProfile:
          type: RuntimeDefault
      initContainers:
        - name: copy-config
          image: {version}
          command:
            - sh
            - -c
            - mkdir -p /data/config && cp /config-template/happy2.toml /data/config/happy2.toml
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
          resources:
            requests:
              cpu: 25m
              memory: 32Mi
            limits:
              cpu: 100m
              memory: 128Mi
          volumeMounts:
            - name: happy2-data
              mountPath: /data
            - name: config-template
              mountPath: /config-template
              readOnly: true
      containers:
        - name: happy2-server
          image: docker.korshakov.com/happy2:{version}
          args:
            - backend
            - --config
            - /data/config/happy2.toml
          ports:
            - containerPort: 3000
              name: http
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              cpu: "2"
              memory: 2Gi
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
          volumeMounts:
            - name: happy2-data
              mountPath: /data
          livenessProbe:
            httpGet:
              path: /v0/health
              port: http
            initialDelaySeconds: 30
            periodSeconds: 30
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /v0/health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 3
          startupProbe:
            httpGet:
              path: /v0/health
              port: http
            periodSeconds: 10
            failureThreshold: 12
      volumes:
        - name: config-template
          configMap:
            name: happy2-server-config
  volumeClaimTemplates:
    - metadata:
        name: happy2-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: happy2-server
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: happy2-server
---
apiVersion: v1
kind: Service
metadata:
  name: happy2-server-headless
spec:
  clusterIP: None
  selector:
    app: happy2-server
  ports:
    - name: http
      port: 3000
      targetPort: http
---
apiVersion: v1
kind: Service
metadata:
  name: happy2-server
spec:
  selector:
    app: happy2-server
  ports:
    - name: http
      port: 3000
      targetPort: http
  type: ClusterIP
