---
# yaml-language-server: $schema=https://raw.githubusercontent.com/bjw-s/helm-charts/main/charts/other/app-template/schemas/helmrelease-helm-v2beta2.schema.json
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: &app qbittorrent-protonvpn
spec:
  interval: 30m
  chart:
    spec:
      chart: app-template
      version: 3.3.2
      interval: 30m
      sourceRef:
        kind: HelmRepository
        name: bjw-s
        namespace: flux-system
  install:
    remediation:
      retries: 3
  upgrade:
    cleanupOnFail: true
    remediation:
      retries: 3
      strategy: rollback
  values:
    controllers:
      qbittorrent:
        annotations:
          configmap.reloader.stakater.com/reload: qbittorrent-scripts
          secret.reloader.stakater.com/reload: qbittorrent-secret
        pod:
          securityContext:
            fsGroup: 568
            fsGroupChangePolicy: "OnRootMismatch"
        containers:
          app:
            nameOverride: qbittorrent
            image:
              repository: ghcr.io/onedr0p/qbittorrent
              tag: 4.6.6@sha256:e21c95568c9175f40390bacd7f778d8d2af5331d1e663e1a6860140891c65742
            env:
              UMASK: "022"
              QBITTORRENT__PORT: &port 80
              QBT_Preferences__WebUI__AlternativeUIEnabled: false
              QBT_Preferences__WebUI__AuthSubnetWhitelistEnabled: true
              QBT_Preferences__WebUI__AuthSubnetWhitelist: |-
                10.244.0.0/16, 10.1.2.0/24
              QBT_Preferences__WebUI__LocalHostAuth: false
              QBT_BitTorrent__Session__Interface: wg0
              QBT_BitTorrent__Session__InterfaceName: wg0
            resources:
              requests:
                cpu: 49m
                memory: 1024Mi
              limits:
                memory: 24Gi
            securityContext:
              runAsUser: 568
              runAsGroup: 568
              runAsNonRoot: true
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: true
              capabilities:
                drop:
                  - ALL

          gluetun:
            image:
              repository: ghcr.io/qdm12/gluetun
              tag: latest@sha256:fb448a2eb8755b68106a386d1e5a78c781bf28a0eea0fb712824cd2dc0ec19a7
            env:
              VPN_SERVICE_PROVIDER: custom
              VPN_TYPE: wireguard
              VPN_INTERFACE: wg0
              WIREGUARD_ENDPOINT_PORT: 51820
              VPN_PORT_FORWARDING: on
              VPN_PORT_FORWARDING_PROVIDER: protonvpn
              FIREWALL_INPUT_PORTS: *port
              FIREWALL_OUTBOUND_SUBNETS: 10.32.0.0/16 # Allow access to k8s subnets
            envFrom:
              - secretRef:
                  name: qbittorrent-secret
            securityContext:
              # until I can debug the issues on talos 1.8.
              privileged: true
              capabilities:
                add:
                  - NET_ADMIN
                drop:
                  - ALL

          port-forward:
            image:
              repository: ghcr.io/bjw-s/gluetun-qb-port-sync
              tag: v0.0.2
            env:
              GLUETUN_CONTROL_SERVER_HOST: localhost
              GLUETUN_CONTROL_SERVER_PORT: 8000
              QBITTORRENT_HOST: localhost
              QBITTORRENT_WEBUI_PORT: *port
              CRON_ENABLED: true
              CRON_SCHEDULE: "*/5 * * * *"
              LOG_TIMESTAMP: false
            securityContext:
              runAsUser: 568
              runAsGroup: 568
              runAsNonRoot: true
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: false
              capabilities:
                drop:
                  - ALL

    service:
      app:
        controller: qbittorrent
        type: LoadBalancer
        annotations:
          io.cilium/lb-ipam-ips: 10.1.1.34
        nameOverride: qbittorrent
        ports:
          http:
            port: *port
          # bittorrent port is set by gluetun-qb-port-sync
          # So we don't need to set it for forwarding-to from firewall here.
          # bittorrent:
          #   enabled: true
          #   port: *bittorrentPort
          #   protocol: TCP

    ingress:
      app:
        className: "internal-nginx"
        hosts:
          - host: "qb.jahanson.tech"
            paths:
              - path: /
                service:
                  identifier: app
                  port: http
        tls:
          - hosts:
              - "qb.jahanson.tech"

    persistence:
      config:
        existingClaim: qbittorrent
      media:
        type: nfs
        server: 10.1.1.11
        path: /volume1/Media
        advancedMounts:
          qbittorrent:
            app:
              - path: /data/nas-media
      qbtun:
        type: hostPath
        hostPath: /dev/net
        advancedMounts:
          qbittorrent:
            gluetun:
              - path: /dev/net