apiVersion: v1
kind: Service
metadata:
  name: adopt-a-street-couchdb
  namespace: adopt-a-street
  labels:
    app: couchdb
spec:
  type: ClusterIP  # Regular ClusterIP service (not headless)
  selector:
    app: couchdb
  ports:
    - port: 5984
      targetPort: 5984
      name: couchdb
    - port: 4369
      targetPort: 4369
      name: epmd


---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: adopt-a-street-couchdb
  namespace: adopt-a-street
spec:
  serviceName: adopt-a-street-couchdb
  replicas: 1
  selector:
    matchLabels:
      app: couchdb
  template:
    metadata:
      labels:
        app: couchdb
    spec:
      # Place CouchDB on Pi 5 nodes (more RAM)
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/arch
                    operator: In
                    values:
                      - arm64  # Pi 5 architecture
      containers:
        - name: couchdb
          image: couchdb:3.3
          ports:
            - containerPort: 5984
              name: couchdb
            - containerPort: 4369
              name: epmd
          env:
            - name: COUCHDB_USER
              valueFrom:
                secretKeyRef:
                  name: adopt-a-street-secrets
                  key: COUCHDB_USER
            - name: COUCHDB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: adopt-a-street-secrets
                  key: COUCHDB_PASSWORD
            - name: COUCHDB_SECRET
              valueFrom:
                secretKeyRef:
                  name: adopt-a-street-secrets
                  key: COUCHDB_SECRET
          resources:
            requests:
              memory: "512Mi"
              cpu: "250m"
            limits:
              memory: "2Gi"
              cpu: "1000m"
          volumeMounts:
            - name: couchdb-data
              mountPath: /opt/couchdb/data
          livenessProbe:
            httpGet:
              path: /_up
              port: 5984
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /_up
              port: 5984
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6

  volumeClaimTemplates:
    - metadata:
        name: couchdb-data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 10Gi
        # Uncomment and set your storage class if needed
        # storageClassName: local-path