apiVersion: apps/v1
kind: Deployment
metadata:
  name: nextcloud
  namespace: nextcloud
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nextcloud
  template:
    metadata:
      labels:
        app: nextcloud
    spec:
      # fsGroup sorgt dafür, dass gemountete Volumes die Gruppe www-data (33) bekommen
      securityContext:
        fsGroup: 33
      # Auf knode0 zwingen (hat Kernel 6.1 mit NFS - Kompatibilität)
      nodeSelector:
        kubernetes.io/hostname: knode0
      # hostAliases mappt die öffentliche Domain intern auf die Service-ClusterIP,
      # damit der Pod henryathome.home64.de direkt intern erreicht (vermeidet externe Loopback/Firewall/403)
      hostAliases:
        - ip: "10.43.107.87"
          hostnames:
            - "henryathome.home64.de"
      initContainers:
        - name: wait-for-mariadb
          image: busybox:1.34
          command: ['sh', '-c', 'until nc -z mariadb.nextcloud.svc.cluster.local 3306; do echo waiting for mariadb; sleep 2; done;']
      containers:
        - name: nextcloud
          image: nextcloud:33-apache
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
          env:
            # - name: NEXTCLOUD_ADMIN_USER
            #   value: admin
            # - name: NEXTCLOUD_ADMIN_PASSWORD
            #   value: admin
            - name: MYSQL_HOST
              value: mariadb.nextcloud.svc.cluster.local
            - name: MYSQL_DATABASE
              value: nextcloud
            - name: MYSQL_USER
              value: nextcloud
            - name: MYSQL_PASSWORD
              value: "nextcloud"
            - name: REDIS_HOST
              value: redis.nextcloud.svc.cluster.local
            - name: NEXTCLOUD_TRUSTED_DOMAINS
              value: "cloud.henryathome.home64.de,192.168.178.0/24,192.168.178.138,nextcloud.nextcloud.svc.cluster.local"
            - name: TRUSTED_PROXIES
              value: "192.168.178.120"
            - name: OVERWRITEHOST
              value: "cloud.henryathome.home64.de"
            - name: OVERWRITEPROTOCOL
              value: "https"
            - name: OVERWRITECLIURL
              value: "https://cloud.henryathome.home64.de"
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "4Gi"
              cpu: "3000m"
          volumeMounts:
            - name: data
              mountPath: /var/www/html/data   
            - name: config
              mountPath: /var/www/html/config
            - name: apps
              mountPath: /var/www/html/custom_apps
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: nextcloud-data-pvc
        - name: config
          persistentVolumeClaim:
            claimName: nextcloud-config-pvc-longhorn
        - name: apps
          persistentVolumeClaim:
            claimName: nextcloud-apps-pvc-longhorn