apiVersion: v1
kind: Service
metadata:
  name: gmus-backend
spec:
  type: LoadBalancer
  selector:
    app: gmus-backend
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: gmus-web
spec:
  type: LoadBalancer
  selector:
    app: gmus-web
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmus-backend
  labels:
    app: gmus-backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: gmus-backend
  template:
    metadata:
      labels:
        app: gmus-backend
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: gmus-backend
          image: docker.fela.space/gmus-backend:0
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef:
                name: gmus-backend
          env:
            - name: GO_ENV
              value: production
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pass
                  key: password
          volumeMounts:
            - name: gmus-library
              mountPath: /music
              readOnly: true
          livenessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /liveness
              port: 8080
          readinessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /readiness
              port: 8080
      volumes:
        - name: gmus-library
          hostPath:
            path: LIBRARY_DIRECTORY
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmus-web
  labels:
    app: gmus-web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gmus-web
  template:
    metadata:
      labels:
        app: gmus-web
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: gmus-web
          image: docker.fela.space/gmus-web:0
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef:
                name: gmus-web
          livenessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /liveness
              port: 8080
          readinessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /readiness
              port: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: gmus-database
  labels:
    app: gmus-database
spec:
  ports:
    - port: 5432
  selector:
    app: gmus-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-gmus
  labels:
    app: gmus-database
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /var/local/gmus-database
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim-gmus
  labels:
    app: gmus-database
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmus-database
  labels:
    app: gmus-database
spec:
  selector:
    matchLabels:
      app: gmus-database
      tier: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: gmus-database
        tier: postgres
    spec:
      containers:
        - image: postgres:10.4
          name: postgres
          env:
            - name: POSTGRES_USER
              value: gmus
            - 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-gmus
---
apiVersion: v1
kind: Service
metadata:
  name: gmus-redis
  labels:
    app: gmus-redis
spec:
  ports:
    - port: 6379
  selector:
    app: gmus-redis
    tier: redis
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmus-redis
  labels:
    app: gmus-redis
spec:
  selector:
    matchLabels:
      app: gmus-redis
      tier: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: gmus-redis
        tier: redis
    spec:
      containers:
        - image: redis:6-alpine
          name: redis
          ports:
            - containerPort: 6379
              name: redis
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: gmus-scan-library
spec:
  schedule: "45 5 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: gmus-scan
              image: docker.fela.space/gmus-backend:0
              args:
                - gmus.scan
              envFrom:
                - configMapRef:
                    name: gmus-backend
              env:
                - name: GO_ENV
                  value: production
                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: postgres-pass
                      key: password
              volumeMounts:
                - name: gmus-library
                  mountPath: /music
                  readOnly: true
          restartPolicy: Never
          volumes:
            - name: gmus-library
              hostPath:
                path: LIBRARY_DIRECTORY
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gmus-file-watcher
  labels:
    app: gmus-file-watcher
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gmus-file-watcher
  template:
    metadata:
      labels:
        app: gmus-file-watcher
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: gmus-file-watcher
          image: docker.fela.space/gmus-backend:0
          ports:
            - containerPort: 8081
          args:
            - gmus.watch
          envFrom:
            - configMapRef:
                name: gmus-backend
          env:
            - name: GO_ENV
              value: production
            - name: PORT
              value: "8081"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pass
                  key: password
          volumeMounts:
            - name: gmus-library
              mountPath: /music
              readOnly: true
          livenessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /liveness
              port: 8081
          readinessProbe:
            initialDelaySeconds: 5
            periodSeconds: 5
            httpGet:
              path: /readiness
              port: 8081
      volumes:
        - name: gmus-library
          hostPath:
            path: LIBRARY_DIRECTORY
