#@ load("@ytt:data", "data")

#@ ns = data.values.application.namespace
#@ app = data.values.application

---
apiVersion: v1
kind: Service
metadata:
  name: garage
  namespace: #@ ns
spec:
  type: ClusterIP
  selector:
    app: garage
  ports:
    - name: s3-api
      port: 3900
      targetPort: 3900
    - name: rpc
      port: 3901
      targetPort: 3901
    - name: s3-web
      port: 3902
      targetPort: 3902
    - name: admin
      port: 3903
      targetPort: 3903

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: garage
  namespace: #@ ns
spec:
  replicas: 1
  serviceName: garage
  selector:
    matchLabels:
      app: garage
  template:
    metadata:
      labels:
        app: garage
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        runAsGroup: 1000
        fsGroup: 1000
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: garage
          image: #@ app.image
          imagePullPolicy: IfNotPresent
          command: ["/garage"]
          args: ["server"]
          ports:
            - name: s3-api
              containerPort: 3900
            - name: rpc
              containerPort: 3901
            - name: s3-web
              containerPort: 3902
            - name: admin
              containerPort: 3903
          volumeMounts:
            - name: data
              mountPath: /var/lib/garage
            - name: config
              mountPath: /etc/garage.toml
              subPath: garage.toml
              readOnly: true
            - name: secrets
              mountPath: /etc/garage
              readOnly: true
          env:
            - name: GARAGE_CONFIG_FILE
              value: /etc/garage.toml
            - name: GARAGE_ALLOW_WORLD_READABLE_SECRETS
              value: "true"
          resources:
            requests:
              cpu: 50m
              memory: 128Mi
            limits:
              memory: 512Mi
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop:
                - ALL
          readinessProbe:
            httpGet:
              path: /health
              port: 3903
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: config
          configMap:
            name: garage-config
        - name: secrets
          secret:
            secretName: garage-secrets
            defaultMode: 0400
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: #@ data.values.cluster.storageClass.local
        resources:
          requests:
            storage: #@ app.storage.size
