apiVersion: v1
kind: PersistentVolume
metadata:
  name: photoprism-storage
  labels: { type: local }
spec:
  storageClassName: manual
  hostPath: { path: "/photoprism" }
  capacity: { storage: 5Gi }
  accessModes: ["ReadWriteOnce"]
  persistentVolumeReclaimPolicy: Retain
  claimRef: { namespace: default, name: photoprism-storage }

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: photoprism-originals
  labels: { type: local }
spec:
  storageClassName: manual
  hostPath: { path: "/my/pic" }
  capacity: { storage: 5Gi }
  accessModes: ["ReadWriteOnce"]
  persistentVolumeReclaimPolicy: Retain
  claimRef: { namespace: default, name: photoprism-originals }

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: photoprism-storage }
spec:
  storageClassName: ""
  volumeName: "photoprism-storage"
  accessModes: ["ReadWriteOnce"]
  resources: { requests: { storage: 5Gi } }

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: photoprism-originals }
spec:
  storageClassName: ""
  volumeName: "photoprism-originals"
  accessModes: ["ReadWriteOnce"]
  resources: { requests: { storage: 5Gi } }

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: photoprism
spec:
  replicas: 1
  selector:
    matchLabels:
      app: photoprism
  template:
    metadata:
      labels:
        app: photoprism
      annotations:
        prometheus.io/port: "9104"
    spec:
      volumes:
        - { name: photoprism-storage, persistentVolumeClaim: { claimName: photoprism-storage } }
        - { name: photoprism-originals, persistentVolumeClaim: { claimName: photoprism-originals } }
        - { name: run-mysqld, emptyDir: {}}
      containers:
        - name: photoprism
          # https://github.com/photoprism/photoprism/releases
          # image: docker.io/photoprism/photoprism:221118-jammy
          image: docker.io/photoprism/photoprism:preview
          env:
            - { name: PHOTOPRISM_UPLOAD_NSFW, value: "true" }
            - { name: PHOTOPRISM_READONLY, value: "true" }
            - { name: PHOTOPRISM_LOG_LEVEL, value: "trace" }
            - { name: PHOTOPRISM_TRACE, value: "true"}
            - { name: PHOTOPRISM_SITE_URL, value: "https://bigasterisk.com/photoprism/" }
            - { name: PHOTOPRISM_AUTH_MODE, value: "public" }
            - { name: PHOTOPRISM_WORKERS, value: "2" }
   
            - { name: PHOTOPRISM_DISABLE_WEBDAV, value: "true" }
            - { name: PHOTOPRISM_DISABLE_CLASSIFICATION, value: "true" }
            - { name: PHOTOPRISM_DISABLE_RAW, value: "true" }
            - { name: PHOTOPRISM_JPEG_SIZE, value: "720" }
            - { name: PHOTOPRISM_THUMB_SIZE, value: "720" }
   
            - { name: PHOTOPRISM_DATABASE_DRIVER, value: "mysql" }
            - { name: PHOTOPRISM_DATABASE_SERVER, value: "/run/mysqld/mysqld.sock" }
            - { name: PHOTOPRISM_DATABASE_NAME, value: "photoprism" }
            - { name: PHOTOPRISM_DATABASE_USER, value: "photoprism" }
            - { name: PHOTOPRISM_DATABASE_PASSWORD, value: "insecure" }

            - { name: PHOTOPRISM_AUTO_INDEX, value: "-1" }
            - { name: PHOTOPRISM_AUTO_IMPORT, value: "-1" }
          ports:
            - containerPort: 2342
          volumeMounts:
            - { name: photoprism-storage, mountPath: "/photoprism/storage" }
            - { name: photoprism-originals, mountPath: "/photoprism/originals" }
            - { name: run-mysqld, mountPath: "/run/mysqld" }
          resources:
            limits:
              # tensorflow analysis goes to like 1200Mi
              memory: 2500Mi
        - name: mariadb
          image: docker.io/mariadb:10.10.3
          securityContext:
            runAsUser: 1000
            # let https://github.com/MariaDB/mariadb-docker/blob/749c720c63306d1572849afc6ab1cfa02fd08338/10.8/docker-entrypoint.sh 
            # do the startup. We could write to /etc/mysql/mariadb.conf.d if we really needed to config things.
          # command: suggested from photoprism docs
          #   - mysqld
          #   - --innodb-buffer-pool-size=512M
          ## https://mariadb.com/kb/en/set-transaction/#isolation-levels current default is REPEATABLE-READ
          #   - --transaction-isolation=READ-COMMITTED
          #   - --character-set-server=utf8mb4
          #   - --collation-server=utf8mb4_unicode_ci
          ## default 151
          #   - --max-connections=512
          ## default 0
          #   - --innodb-rollback-on-timeout=OFF
          ## default 50
          #   - --innodb-lock-wait-timeout=120
          volumeMounts:
            - { name: photoprism-storage, subPath: "mariadb", mountPath: "/var/lib/mysql" }
            - { name: run-mysqld, mountPath: "/run/mysqld" }
          env:
          # https://hub.docker.com/_/mariadb/#:~:text=latest%20%2D%2Dverbose%20%2D%2Dhelp-,Environment%20Variables,-When%20you%20start
          # says these only matter upon the first run.
            - { name: MARIADB_AUTO_UPGRADE, value: "1" }
            - { name: MARIADB_INITDB_SKIP_TZINFO, value: "1" }
            - { name: MARIADB_DATABASE, value: "photoprism" }
            - { name: MARIADB_USER, value: "photoprism" }
            - { name: MARIADB_PASSWORD, value: "insecure" }
            - { name: MARIADB_ROOT_PASSWORD, value: "insecure" }
        - name: mariadb-exporter
          # for this to work, run this once:
          # 
          # k exec -it deploy/photoprism mariadb -- mysql --user=root --password=insecure
          # 
          # CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX';
          # GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'localhost';
          # GRANT SELECT ON performance_schema.* TO 'exporter'@'localhost';
          # GRANT ALL ON photoprism.* TO 'exporter'@'localhost';
          image: prom/mysqld-exporter:v0.14.0
          args:
           # see: k exec -it deploy/photoprism -c mariadb-exporter -- /bin/mysqld_exporter --help
           - "--collect.info_schema.tables.databases=photoprism"
           - "--collect.info_schema.tables"
           - "--no-collect.slave_status"
           - "--collect.info_schema.processlist"
           - "--no-collect.info_schema.query_response_time"
           - "--collect.perf_schema.tablelocks"
           - "--collect.perf_schema.eventsstatements"
           - "--collect.perf_schema.eventswaits"
           - "--collect.perf_schema.tableiowaits"
           - "--collect.perf_schema.indexiowaits"
           - "--collect.perf_schema.file_events"
           - "--collect.perf_schema.file_instances"
           - "--collect.perf_schema.memory_events"
           # need to turn on 'userstat' in mysql or something for these
           #- "--collect.info_schema.tablestats"
           #- "--collect.info_schema.schemastats"
           - "--log.level=info"
          ports:
          - containerPort: 9104
          env:
          - name: DATA_SOURCE_NAME
            value: "exporter:XXXXXXXX@unix(/run/mysqld/mysqld.sock)/"
          volumeMounts:
            - { name: run-mysqld, mountPath: "/run/mysqld" }
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: "kubernetes.io/hostname"
                    operator: In
                    values: ["ditto"]

---
apiVersion: v1
kind: Service
metadata:
  name: photoprism
spec:
  type: LoadBalancer
  ports:
    - { port: 80, targetPort: 2342, name: web }
    - { port: 9104, targetPort: 9104, name: metrics }
  selector:
    app: photoprism
