{%- set ns = project_name -%}
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-server
  namespace: {{ ns }}
  labels:
    app: mongo-server
    app.kubernetes.io/name: freva
    app.kubernetes.io/instance: mongo-server
    app.kubernetes.io/component: rest-api
    app.kubernetes.io/part-of: freva
    app.kubernetes.io/version: {{ mongodb_server_version }}
    app.kubernetes.io/managed-by: ansible
    freva.org/stateful: 'true'
    freva.org/tier: database
spec:
  type: ClusterIP
  selector: { app: mongo-server }
  ports: [{ port: 27017, targetPort: 27017, name: mongo }]
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo-server
  namespace: {{ ns }}
  labels:
    app: mongo-server
    app.kubernetes.io/name: freva
    app.kubernetes.io/instance: mongo-server
    app.kubernetes.io/component: rest-api
    app.kubernetes.io/part-of: freva
    app.kubernetes.io/version: {{ mongodb_server_version }}
    app.kubernetes.io/managed-by: ansible
    freva.org/stateful: 'true'
    freva.org/tier: database
spec:
  serviceName: mongo-server
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  selector: { matchLabels: { app: mongo-server } }
  template:
    metadata:
      name: mongo-server
      namespace: {{ ns }}
      labels:
        app: mongo-server
        app.kubernetes.io/name: freva
        app.kubernetes.io/instance: mongo-server
        app.kubernetes.io/component: rest-api
        app.kubernetes.io/part-of: freva
        app.kubernetes.io/version: {{ mongodb_server_version }}
        app.kubernetes.io/managed-by: ansible
        freva.org/stateful: 'true'
        freva.org/tier: database
    spec:
      securityContext: { fsGroup: {{ fs_group }} }
      containers:
        - name: mongodb
          image: ghcr.io/freva-org/freva-mongo:{{ mongodb_server_version }}
          imagePullPolicy: {{ image_pull_policy }}
          ports: [{ containerPort: 27017, name: mongo }]
          resources: {{ resources['mongo-server'] }}
          env:
            - name: API_MONGO_USER
              valueFrom: { secretKeyRef: { name: freva-secrets, key: MONGO_APP_USER } }
            - name: API_MONGO_PASSWORD
              valueFrom: { secretKeyRef: { name: freva-secrets, key: MONGO_APP_PASSWORD } }
            - { name: API_MONGO_DB, value: "search_stats" }
          volumeMounts:
            - { name: mongo-data, mountPath: /data/db }
      volumes:
        - name: mongo-data
          persistentVolumeClaim: { claimName: mongo-data }
