{{ if .Values.camunda2.enabled }}
# apiVersion: v1
# kind: Secret
# metadata:
#   name: camunda-db-credentials
# type: Opaque
# stringData:
#   username: user
#   password: password # TODO generate random password or create secret manually
# ---
apiVersion: v1
kind: Service
metadata:
  name: camunda
spec:
  type: ClusterIP
  selector:
    app: camunda
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: camunda
spec:
  replicas: 1
  selector:
    matchLabels:
      app: camunda
  template:
    metadata:
      name: camunda
      labels:
        app: camunda
    spec:
      containers:
        - name: camunda
          image: camunda/camunda-bpm-platform:{{.Values.camunda2.camunda_version}}
          ports:
            - containerPort: 8080
          env:
            - name: LOG4J_FORMAT_MSG_NO_LOOKUPS
              value: "true"
            - name: DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: username
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: password
{{ if eq "postgres" .Values.camunda2.camunda_db }}
            - name: DB_DRIVER
              value: "org.postgresql.Driver"
            - name: DB_URL
              value: jdbc:postgresql://camunda-postgres:5432/camunda
            - name: WAIT_FOR
              value: camunda-postgres:5432
{{ else if eq "mysql" .Values.camunda2.camunda_db }}
            - name: DB_DRIVER
              value: "com.mysql.cj.jdbc.Driver"
            - name: DB_URL
              value: jdbc:mysql://camunda-mysql:3306/camunda            
            - name: WAIT_FOR
              value: camunda-mysql:3306
{{ end }}
---
apiVersion: v1
kind: Service
metadata:
  name: camunda-api
spec:
  type: ClusterIP
  selector:
    app: camunda-api
  ports:
    - name: http
      protocol: TCP
      port: 4002
      targetPort: 4002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: camunda-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: camunda-api
  template:
    metadata:
      name: camunda-api
      labels:
        app: camunda-api
    spec:
      containers:
        - name: camunda-api
          image: goalprofit/camunda-api:{{.Values.camunda2.api_version}}
          imagePullPolicy: Always
          ports:
            - containerPort: 4002
          env:
            - name: API_URL
              value: http://server-api:8080
            - name: SERVICE_LOGIN
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: username
            - name: SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: password
            - name: CAMUNDA_DB_TYPE
              value: {{ .Values.camunda2.camunda_db }}
            - name: CAMUNDA_DB_NAME
              value: camunda
{{ if eq "postgres" .Values.camunda2.camunda_db }}
            - name: CAMUNDA_DB_HOST
              value: camunda-postgres
            - name: CAMUNDA_DB_PORT
              value: "5432"
{{ else if eq "mysql" .Values.camunda2.camunda_db }}
            - name: CAMUNDA_DB_HOST
              value: camunda-mysql
            - name: CAMUNDA_DB_PORT
              value: "3306"
{{ end }}
            - name: CAMUNDA_DB_LOGIN
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: username
            - name: CAMUNDA_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: password
            - name: CAMUNDA_REST_URL
              value: http://camunda:8080/engine-rest
            - name: SCHEMA_UPDATER_URL
              value: http://api-router-schema-updater:8080
      
      imagePullSecrets:
      - name: regcred
---
{{ if eq "postgres" .Values.camunda2.camunda_db }}
apiVersion: v1
kind: Service
metadata:
  name: camunda-postgres
spec:
  type: ClusterIP
  selector:
    app: camunda-postgres
  ports:
    - name: http
      protocol: TCP
      port: 5432
      targetPort: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: camunda-postgres
spec:
  replicas: 1
  serviceName: camunda-postgres
  selector:
    matchLabels:
      app: camunda-postgres
  template:
    metadata:
      name: camunda-postgres
      labels:
        app: camunda-postgres
    spec:
{{ if .Values.local_user_id }}
      securityContext:
        runAsUser: {{ .Values.local_user_id }}
{{ end }}
      containers:
        - name: camunda-postgres
          image: postgres
          ports:
            - containerPort: 5432
              name: postgres
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: password
            - name: POSTGRES_DB
              value: camunda
            - name: PGDATA
              value: /var/lib/postgresql/data
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: camunda-postgres

      volumes:
        - name: camunda-postgres
          hostPath:
            path: /data/{{.Values.customer}}/camunda/postgres
            type: Directory
{{ else if eq "mysql" .Values.camunda2.camunda_db }}
apiVersion: v1
kind: Service
metadata:
  name: camunda-mysql
spec:
  type: ClusterIP
  selector:
    app: camunda-mysql
  ports:
    - name: http
      protocol: TCP
      port: 3306
      targetPort: 3306
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: camunda-mysql
spec:
  replicas: 1
  serviceName: camunda-mysql
  selector:
    matchLabels:
      app: camunda-mysql
  template:
    metadata:
      name: camunda-mysql
      labels:
        app: camunda-mysql
    spec:
{{ if .Values.local_user_id }}
      securityContext:
        runAsUser: {{ .Values.local_user_id }}
{{ end }}
      containers:
        - name: camunda-mysql
          image: mysql
          ports:
            - containerPort: 3306
              name: mysql
          env:
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: username
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: camunda-db-credentials
                  key: password
            - name: MYSQL_RANDOM_ROOT_PASSWORD
              value: "true"
            - name: MYSQL_DATABASE
              value: camunda
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: camunda-mysql

      volumes:
        - name: camunda-mysql
          hostPath:
            path: /data/{{.Values.customer}}/camunda/mysql
            type: Directory
{{ else }}
  {{ fail "Unknown camunda_db. Must be ether 'postgres' or 'mysql'" }}
{{ end }}

{{ end }}
