apiVersion: v1
kind: Service
metadata:
  name: gmusic
spec:
  type: LoadBalancer
  selector:
    app: gmusic
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: gmusic-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: gmusic
                port:
                  number: 80
---
apiVersion: v1
kind: Service
metadata:
  name: gmusic-database
  labels:
    app: gmusic-database
spec:
  ports:
    - port: 5432
  selector:
    app: gmusic-database
    tier: postgres
  clusterIP: None
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: standard
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    app: gmusic-database
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /var/local/gmusic-database
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
  labels:
    app: gmusic-database
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmusic-database
  labels:
    app: gmusic-database
spec:
  selector:
    matchLabels:
      app: gmusic-database
      tier: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: gmusic-database
        tier: postgres
    spec:
      containers:
        - image: postgres:10.4
          name: postgres
          env:
            - name: POSTGRES_USER
              value: gmusic
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pass
                  key: password
          ports:
            - containerPort: 5432
              name: postgres
          volumeMounts:
            - name: postgres-persistent-storage
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-persistent-storage
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: gmusic-scan-library
spec:
  schedule: "45 5 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: gmusic-scan
              image: docker.fela.space/gmusic:0
              args:
                - scan
              envFrom:
                - configMapRef:
                    name: gmusic
              env:
                - name: DATABASE_URL
                  valueFrom:
                    secretKeyRef:
                      name: postgres-pass
                      key: url
          restartPolicy: Never
