{{ $runCfg := .Values.runCfg | b64dec }}
---
{{- if .Values.sshPrivateKey }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Values.name }}-ssh-key
type: Opaque
data:
  private: {{ .Values.sshPrivateKey }}
{{- end }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.name }}-run-cfg
data:
  run: |
{{ indent 4 $runCfg }}
---
{{- if .Values.appPorts }}
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.name }}-app
  namespace: {{ .Release.Namespace }}
spec:
  type: ClusterIP
  selector:
    app: {{ .Values.name }}-app
  ports:
  {{- range .Values.appPorts }}
  - name: {{ .name }}
    port: {{ .containerPort }}
    targetPort: {{ .name }}
    protocol: {{ .protocol }}
  {{- end }}
{{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.name }}-api
  namespace: {{ .Release.Namespace }}
spec:
  type: ClusterIP
  selector:
    app: {{ .Values.name }}-app
  ports:
  - name: api
    port: 80
    targetPort: dodo-api
    protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}-app
  namespace: {{ .Release.Namespace }}
  annotations:
    dodo.cloud/config-checksum: {{ sha256sum .Values.runCfg }}
spec:
  selector:
    matchLabels:
      app: {{ .Values.name }}-app
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ .Values.name }}-app
      annotations:
        dodo.cloud/config-checksum: {{ sha256sum .Values.runCfg }}
    spec:
      runtimeClassName: {{ .Values.runtimeClassName }}
      volumes:
      {{- if .Values.sshPrivateKey }}
      - name: ssh-key
        secret:
          secretName: {{ .Values.name }}-ssh-key
      {{- end }}
      - name: run-cfg
        configMap:
          name: {{ .Values.name }}-run-cfg
      {{- range .Values.volumes }}
      - name: volume-{{ .name }}
        persistentVolumeClaim:
          claimName: {{ .name }}
      {{- end }}
      containers:
      - name: app
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: dodo-api
          containerPort: {{ .Values.apiPort }}
          protocol: TCP
        {{- if .Values.appPorts }}
          {{ toYaml .Values.appPorts | nindent 8 }}
        {{- end }}
        env:
        - name: SELF_ID
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: SELF_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        command:
        - app-runner
        - --port={{ .Values.apiPort }}
        - --app-id={{ .Values.appId }}
        - --service={{ .Values.name }}
        - --app-dir={{ .Values.appDir }}
        - --repo-addr={{ .Values.repoAddr }}
        - --branch={{ .Values.branch }}
        - --root-dir={{ .Values.rootDir }}
        {{- if .Values.sshPrivateKey }}
        - --ssh-key=/pcloud/ssh-key/private
        {{- end }}
        - --run-cfg=/pcloud/config/run
        - --manager-addr={{ .Values.managerAddr }}
        volumeMounts:
        {{- if .Values.sshPrivateKey }}
        - name: ssh-key
          readOnly: true
          mountPath: /pcloud/ssh-key
        {{- end }}
        - name: run-cfg
          readOnly: true
          mountPath: /pcloud/config
        {{- range .Values.volumes }}
        - name: volume-{{ .name }}
          mountPath: {{ .mountPath }}
        {{- end }}
      {{- if .Values.extraContainers }}
        {{ toYaml .Values.extraContainers | nindent 6 }}
      {{- end }}
