---
# yaml-language-server: $schema=https://raw.githubusercontent.com/bjw-s/helm-charts/main/charts/other/app-template/schemas/helmrelease-helm-v2.schema.json
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: plex
spec:
  interval: 30m
  chart:
    spec:
      chart: app-template
      version: 3.5.1
      sourceRef:
        kind: HelmRepository
        name: bjw-s
        namespace: flux-system
  install:
    remediation:
      retries: 3
  upgrade:
    cleanupOnFail: true
    remediation:
      strategy: rollback
      retries: 3
  dependsOn:
    - name: nvidia-device-plugin
      namespace: kube-system
    - name: rook-ceph-cluster
      namespace: rook-ceph
    - name: volsync
      namespace: volsync-system
  values:
    controllers:
      plex:
        annotations:
          reloader.stakater.com/auto: "true"
        containers:
          app:
            image:
              repository: ghcr.io/onedr0p/plex
              tag: 1.41.2.9200-c6bbc1b53@sha256:47c6f3d85f4e739210860934a0bb24126170fa2f6a602fb909467f17a035c311
            env:
              TZ: America/Chicago
              PLEX_ADVERTISE_URL: https://plex.hsn.dev:443,http://10.1.1.39:32400
              PLEX_NO_AUTH_NETWORKS: 10.1.1.0/24,10.244.0.0/16
            probes:
              liveness: &probes
                enabled: true
                custom: true
                spec:
                  httpGet:
                    path: /identity
                    port: 32400
                  initialDelaySeconds: 0
                  periodSeconds: 10
                  timeoutSeconds: 1
                  failureThreshold: 3
              readiness: *probes
              startup:
                enabled: true
                spec:
                  failureThreshold: 30
                  periodSeconds: 10
            securityContext:
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: true
              capabilities: { drop: ["ALL"] }
            resources:
              requests:
                cpu: 100m
              limits:
                nvidia.com/gpu: 1 # requesting 1 GPU
                memory: 16Gi
    defaultPodOptions:
      securityContext:
        runAsNonRoot: true
        runAsUser: 568
        runAsGroup: 568
        fsGroup: 568
        fsGroupChangePolicy: OnRootMismatch
        supplementalGroups: [44, 10000]
        seccompProfile: { type: RuntimeDefault }
      nodeSelector:
        nvidia.com/gpu.present: "true"
      runtimeClassName: nvidia
    service:
      app:
        controller: plex
        type: LoadBalancer
        annotations:
          io.cilium/lb-ipam-ips: 10.1.1.39
        ports:
          http:
            port: 32400
    ingress:
      app:
        annotations:
          external-dns.alpha.kubernetes.io/target: external.hsn.dev
          nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
          external-dns.alpha.kubernetes.io/cloudflare-proxied: "true"
        className: external-nginx
        hosts:
          - host: "{{ .Release.Name }}.hsn.dev"
            paths:
              - path: /
                service:
                  identifier: app
                  port: http
    persistence:
      config:
        existingClaim: plex
        # TODO: If setting up Plex for the first time, you'll want to add the globalMounts section
        globalMounts:
          - path: /config/Library/Application Support/Plex Media Server
      # Separate PVC for cache to avoid backing up cache files
      cache:
        existingClaim: plex-cache
        globalMounts:
          - path: /config/Library/Application Support/Plex Media Server/Cache
      logs:
        type: emptyDir
        globalMounts:
          - path: /config/Library/Application Support/Plex Media Server/Logs
      tmp:
        type: emptyDir
      transcode:
        type: emptyDir
      media:
        type: nfs
        server: 10.1.1.61
        path: /moria/media
        globalMounts:
          - path: /media
            readOnly: true