---
apiVersion: v1
kind: Service
metadata:
  name: boidas
spec:
  selector:
    app: boidas
  ports:
    - port: 8443
      protocol: TCP
      targetPort: 8443
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: boidas
  labels:
    app: boidas
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: boidas
  template:
    metadata:
      name: boidas
      labels:
        app: boidas
    spec:
      initContainers:
        - name: boidas-init-volumes
          image: busybox
          command: ['sh', '-c', 'chown 33:33 /opt/boidas_backend/media /var/log/boidas && chmod 775 /opt/boidas_backend/media /var/log/boidas']
          volumeMounts:
            - mountPath: /opt/boidas_backend/media
              name: boidas-media
            - mountPath: /var/log/boidas
              name: boidas-logs
      containers:
        - name: boidas-postgresql
          image: postgres:16.6
          env:
            - name: POSTGRES_USER
              value: "boidas"
            - name: POSTGRES_DB
              value: "boidas"
            - name: POSTGRES_PASSWORD
              value: "boidas"
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: boidas-pgdata
        - name: boidas-api
          image: boidas:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: DB_HOST
              value: "localhost"
            - name: DB_USER
              value: "boidas"
            - name: DB_PASS
              value: "boidas"
            - name: DB_NAME
              value: "boidas"
            - name: WORKERS
              value: "1"
            - name: ENABLE_SSL
              value: "TRUE"
            - name: VALIDAS_URL
              value: "https://api-play.eu.veri-das.com/validas/v1"
            - name: API_KEY
              value: "your API key here"
            - name: BASE_URL
              value: "https://localhost:8443"
            - name: MIGRATIONS
              value: "yes"
            - name: TZ
              value: "Europe/Madrid"
            - name: EMAIL_HOST_NAME
              value: "your server name here"
            - name: EMAIL_HOST_PORT
              value: "your server port here"
            - name: EMAIL_HOST_USER
              value: "your email here"
            - name: EMAIL_HOST_PASSWORD
              value: "your email pass here"
          ports:
            - name: api-port
              containerPort: 8850
          livenessProbe:
            httpGet:
              scheme: HTTPS
              path: /api/v1/alive
              port: api-port
            failureThreshold: 3
            periodSeconds: 10
            timeoutSeconds: 10
          startupProbe:
            httpGet:
              scheme: HTTPS
              path: /api/v1/alive
              port: api-port
            failureThreshold: 30
            periodSeconds: 10
            timeoutSeconds: 10
          volumeMounts:
          - mountPath: /opt/boidas_backend/media
            name: boidas-media
          - mountPath: /var/log/boidas
            name: boidas-logs
        - name: boidas-nginx
          image: boidas:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: SERVER_NAME
              value: "localhost"
            - name: NGINX_UPSTREAM
              value: "localhost"
            - name: PORT
              value: "8850"
          ports:
            - containerPort: 8443
          command: ["/opt/boidas_backend/run_nginx.sh"]
      volumes:
        - name: boidas-media
          persistentVolumeClaim:
            claimName: boidas-media
        - name: boidas-logs
          persistentVolumeClaim:
            claimName: boidas-logs
        - name: boidas-pgdata
          persistentVolumeClaim:
            claimName: boidas-pgdata
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: boidas-pgdata
  labels:
    type: local
spec:
  storageClassName: ""
  claimRef:
    namespace: boidas
    name: boidas-pgdata
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/boidas/pgdata
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: boidas-pgdata
  labels:
    type: local
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  volumeName: boidas-pgdata
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: boidas-media
  labels:
    type: local
spec:
  storageClassName: ""
  claimRef:
    namespace: boidas
    name: boidas-media
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/boidas/media
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: boidas-media
  labels:
    type: local
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: boidas-media
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: boidas-logs
  labels:
    type: local
spec:
  storageClassName: ""
  claimRef:
    namespace: boidas
    name: boidas-logs
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/boidas/logs
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: boidas-logs
  labels:
    type: local
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  volumeName: boidas-logs
