# FastAPI Backend Deployment
# Python API with 3 replicas + Shared Storage (PVC)
---
# 1. PersistentVolumeClaim (Shared Storage for Repos)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: repos-pvc
  namespace: gition
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nfs-client
  resources:
    requests:
      storage: 5Gi

---
# 2. Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: gition
  labels:
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      imagePullSecrets:
      - name: gitlab-registry
      containers:
      - name: api
        image: 172.100.100.8:5050/root/gition/backend:latest
        ports:
        - containerPort: 3001
        env:
        - name: MYSQL_READ_HOST
          value: "mysql-master" 
        - name: MYSQL_WRITE_HOST
          value: "mysql-master"
        - name: MYSQL_PORT
          value: "3306"
        - name: MYSQL_USER
          value: "pista"
        - name: MYSQL_DATABASE
          value: "gition"
        - name: BASE_URL
          value: "http://gition.local"
        - name: FRONTEND_URL
          value: "http://gition.local"
        - name: ALLOWED_ORIGINS
          value: "http://gition.local,http://127.0.0.1,http://localhost"
        - name: REPOS_PATH
          value: "/repos"
        # GitHub OAuth Configuration
        - name: GITHUB_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name: github-secret
              key: client-id
        - name: GITHUB_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: github-secret
              key: client-secret
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: user-password
        volumeMounts:
        - name: repos-volume
          mountPath: /repos
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 500m
            memory: 512Mi
        livenessProbe:
          httpGet:
            path: /health
            port: 3001
          initialDelaySeconds: 30
          periodSeconds: 60
        readinessProbe:
          httpGet:
            path: /health
            port: 3001
          initialDelaySeconds: 15
          periodSeconds: 60
      volumes:
      - name: repos-volume
        persistentVolumeClaim:
          claimName: repos-pvc

---
# 3. Service
apiVersion: v1
kind: Service
metadata:
  name: api-svc
  namespace: gition
spec:
  selector:
    app: api
  ports:
  - port: 3001
    targetPort: 3001
