# Namespace
---
apiVersion: v1
kind: Namespace
metadata:
  name: gitea

# PV + PVC: Gitea (NFS)
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitea-pv
spec:
  storageClassName: nfs
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.178.166
    path: /export/fastData/gitea/repos
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-pvc
  namespace: gitea
spec:
  storageClassName: nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi

# PV + PVC: PostgreSQL (NFS)
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
spec:
  storageClassName: nfs
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.178.166
    path: /export/fastData/gitea/postgres
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: gitea
spec:
  storageClassName: nfs
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

# Deployment: PostgreSQL
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:13
          env:
            - name: POSTGRES_DB
              value: gitea
            - name: POSTGRES_USER
              value: gitea
            - name: POSTGRES_PASSWORD
              value: giteapassword
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
          securityContext:
            runAsUser: 1001
            runAsGroup: 1000
#            fsGroup: 1000
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc

# Service: PostgreSQL
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: gitea
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432

# Deployment: Gitea
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitea
  namespace: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea
  template:
    metadata:
      labels:
        app: gitea
    spec:
      containers:
        - name: gitea
          image: gitea/gitea:latest
          env:
            - name: USER_UID
              value: "1000"
            - name: USER_GID
              value: "1000"
            - name: GITEA__database__DB_TYPE
              value: postgres
            - name: GITEA__database__HOST
              value: postgres:5432
            - name: GITEA__database__NAME
              value: gitea
            - name: GITEA__database__USER
              value: gitea
            - name: GITEA__database__PASSWD
              value: giteapassword
            - name: GITEA__server__ROOT_URL
              value: "https://git.henryathome.home64.de"
            - name: GITEA__server__SSH_DOMAIN
              value: git.henryathome.home64.de
            - name: GITEA__server__START_SSH_SERVER
              value: "true"
            - name: GITEA__server__SSH_LISTEN_PORT
              value: "32000"
            - name: GITEA__server__SSH_PORT
              value: "32000"
          ports:
            - containerPort: 3000   # HTTP
            - containerPort: 32000     # SSH
          volumeMounts:
            - name: gitea-storage
              mountPath: /data
          securityContext:
#             runAsUser: 1001
#            runAsGroup: 1000
#            fsGroup: 1000
      volumes:
        - name: gitea-storage
          persistentVolumeClaim:
            claimName: gitea-pvc

# Service: Gitea (inkl. SSH)
---
apiVersion: v1
kind: Service
metadata:
  name: gitea
  namespace: gitea
spec:
  selector:
    app: gitea
  type: NodePort  # Alternativ: LoadBalancer für Clouds
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 3000
    - name: ssh
      protocol: TCP
      port: 32000
      targetPort: 32000
      nodePort: 32000
