# prep:
#  Also run:
#    ansible microk8s --become -a 'chmod g+rw,o+rw /dev/vchiq'
#    ansible microk8s --become -a 'chmod g+rw,o+rw /dev/video*'
#    ansible microk8s --become -a 'chmod -R g+rw,o+rw /dev/dri'
apiVersion: v1
kind: Service
metadata:
  name: jellyfin
  labels:
    run: jellyfin
spec:
  ports:
    - name: http
      port: 80
      targetPort: 8096
  selector:
    run: jellyfin
#  type: LoadBalancer
#status:
#  loadBalancer: {}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jellyfin-tv
spec:
  capacity:
    storage: 10Ti
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain # Keep 4eva
  storageClassName: default
  mountOptions:
    - hard
    - nfsvers=3
  nfs:
    path: /mnt/DroboFS/Shares/Public/media/tv
    server: 10.42.42.10
  claimRef:
    name: jellyfin-tv
    namespace: default
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jellyfin-movies
spec:
  capacity:
    storage: 10Ti
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain # Keep 4eva
  storageClassName: default
  mountOptions:
    - hard
    - nfsvers=3
  nfs:
    path: /mnt/DroboFS/Shares/Public/media/movies
    server: 10.42.42.10
  claimRef:
    name: jellyfin-movies
    namespace: default
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jellyfin-tv
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Ti
status: {}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jellyfin-movies
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Ti
status: {}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jellyfin-config
spec:
  capacity:
    storage: 5Mi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain # Keep 4eva
  storageClassName: default
  mountOptions:
    - hard
    - nfsvers=3
  nfs:
    path: /mnt/DroboFS/Shares/Kubernetes/volumes/static/jellyfin-config
    server: 10.42.42.10
  claimRef:
    name: jellyfin-config
    namespace: default
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jellyfin-config
  annotations:
    nfs.io/storage-path: "jellyfin-config"
spec:
  storageClassName: default
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Mi
status: {}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jellyfin-downloads
  annotations:
    nfs.io/storage-path: "jellyfin-downloads"
spec:
  storageClassName: default
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Mi
status: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jellyfin
spec:
  replicas: 1
  selector:
    matchLabels:
      run: jellyfin
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        run: jellyfin
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - image: linuxserver/jellyfin:latest
          imagePullPolicy: "Always"
          name: jellyfin
          env:
            - name: PUID
              value: "1001"
            - name: GUID
              value: "1001"
            - name: TZ
              value: US/Michigan
#            - name: JELLYFIN_PublishedServerUrl
#              value: jellyfin.monkeybox.org
          ports:
            - containerPort: 8096
          resources:
            limits:
              memory: "2Gi"
              cpu: "1500m"
            requests:
              memory: "2Gi"
              cpu: "1500m"
          volumeMounts:
            - mountPath: /config
              name: jellyfin-config
            - mountPath: /data/tvshows
              name: jellyfin-tv
            - mountPath: /data/movies
              name: jellyfin-movies

 
#            - mountPath: /opt/vc/lib
#              name: vc-lib
            - mountPath: /dev/vcsm-cma
              name: vcsm-cma
            - mountPath: /dev/vchiq
              name: vchiq
            - mountPath: /dev/video10
              name: video10
            - mountPath: /dev/video11
              name: video11
            - mountPath: /dev/video12
              name: video12
            - mountPath: /dev/video13
              name: video13
            - mountPath: /dev/video14
              name: video14
            - mountPath: /dev/video15
              name: video15
            - mountPath: /dev/video16
              name: video16
            - mountPath: /dev/dri
              name: dri
      restartPolicy: Always
      volumes:
        - name: jellyfin-config
          persistentVolumeClaim:
            claimName: jellyfin-config
        - name: jellyfin-tv
          persistentVolumeClaim:
            claimName: jellyfin-tv
        - name: jellyfin-movies
          persistentVolumeClaim:
            claimName: jellyfin-movies

#        - name: vc-lib
#          hostPath:
#            path: /opt/vc/lib
        - name: vcsm-cma
          hostPath:
            path: /dev/vcsm-cma
        - name: vchiq
          hostPath:
            path: /dev/vchiq
        - name: video10
          hostPath:
            path: /dev/video10
        - name: video11
          hostPath:
            path: /dev/video11
        - name: video12
          hostPath:
            path: /dev/video12
        - name: video13
          hostPath:
            path: /dev/video13
        - name: video14
          hostPath:
            path: /dev/video14
        - name: video15
          hostPath:
            path: /dev/video15
        - name: video16
          hostPath:
            path: /dev/video16
        - name: dri
          hostPath:
            path: /dev/dri
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 10.42.42.239
          - 10.42.42.1
        searches:
          - default.svc.cluster.local
          - svc.cluster.local 
          - cluster.local
        options:
          - name: ndots
            value: "2"
          - name: edns0
          - name: trust-ad
status: {}
---
# Hosting
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jellyfin-ingress
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    ## No basic auth for jellyfin
    ## type of authentication
    #nginx.ingress.kubernetes.io/auth-type: basic
    ## name of the secret that contains the user/password definitions
    #nginx.ingress.kubernetes.io/auth-secret: basic-auth
    ## message to display with an appropriate context why the authentication is required
    #nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - Octoprint'
spec:
  tls:
  - hosts:
    - jellyfin.monkeybox.org
    secretName: jellyfin-tls
  rules:
  - host: jellyfin.monkeybox.org
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: jellyfin
            port:
              number: 8096
