# SAMMS ENTERPRISE SOVEREIGN OPERATING SYSTEM - Database Configuration
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  labels:
    app: sesos
    component: database
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  storageClassName: gp3
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  labels:
    app: sesos
    component: database
spec:
  serviceName: postgres-headless
  replicas: 1
  selector:
    matchLabels:
      app: sesos
      component: database
  template:
    metadata:
      labels:
        app: sesos
        component: database
    spec:
      securityContext:
        fsGroup: 999
      containers:
        - name: postgres
          image: postgres:15-alpine
          ports:
            - containerPort: 5432
              name: postgres
          env:
            - name: POSTGRES_DB
              value: sesos
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: sesos-secrets
                  key: database-user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: sesos-secrets
                  key: database-password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          resources:
            requests:
              cpu: "500m"
              memory: "1Gi"
            limits:
              cpu: "2000m"
              memory: "4Gi"
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
          livenessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - $(POSTGRES_USER)
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - $(POSTGRES_USER)
            initialDelaySeconds: 5
            periodSeconds: 5
  volumeClaimTemplates:
    - metadata:
        name: postgres-storage
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: gp3
        resources:
          requests:
            storage: 100Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: sesos
    component: database
spec:
  type: ClusterIP
  ports:
    - port: 5432
      targetPort: postgres
      name: postgres
  selector:
    app: sesos
    component: database
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-headless
  labels:
    app: sesos
    component: database
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 5432
      targetPort: postgres
      name: postgres
  selector:
    app: sesos
    component: database
---
# Redis Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: sesos
    component: cache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sesos
      component: cache
  template:
    metadata:
      labels:
        app: sesos
        component: cache
    spec:
      containers:
        - name: redis
          image: redis:7-alpine
          ports:
            - containerPort: 6379
              name: redis
          command:
            - redis-server
            - --appendonly
            - "yes"
            - --maxmemory
            - "1gb"
            - --maxmemory-policy
            - allkeys-lru
          resources:
            requests:
              cpu: "100m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "1Gi"
          livenessProbe:
            exec:
              command:
                - redis-cli
                - ping
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            exec:
              command:
                - redis-cli
                - ping
            initialDelaySeconds: 5
            periodSeconds: 5
          volumeMounts:
            - name: redis-data
              mountPath: /data
      volumes:
        - name: redis-data
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: sesos
    component: cache
spec:
  type: ClusterIP
  ports:
    - port: 6379
      targetPort: redis
      name: redis
  selector:
    app: sesos
    component: cache