# helm_charts_install-using-hal.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}-install-using-hal"
  labels:
{{ include "spinnaker.standard-labels" . | indent 4 }}
  annotations:
    "helm.sh/hook": "post-install,post-upgrade"
    "helm.sh/hook-delete-policy": "before-hook-creation"
    "helm.sh/hook-weight": "0"
spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap/halyard-config.yaml") . | sha256sum }}
          {{- if .Values.halyard.annotations }}
{{ toYaml .Values.halyard.annotations | indent 8 }}
          {{- end }}
      labels:
{{ include "spinnaker.standard-labels" . | indent 8 }}
    spec:
      {{- if .Values.serviceAccount.halyardName }}
      serviceAccountName: {{ .Values.serviceAccount.halyardName }}
      {{- else }}
      serviceAccountName: {{ template "spinnaker.fullname" . }}-halyard
      {{- end }}
      securityContext:
        runAsUser: {{ .Values.securityContext.runAsUser }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
      restartPolicy: OnFailure
      volumes:
      {{- if and .Values.kubeConfig.enabled .Values.kubeConfig.secretName }}
      - name: kube-config
        secret:
          secretName: {{ .Values.kubeConfig.secretName }}
      {{- end }}
      - name: halyard-config
        configMap:
          name: {{ template "spinnaker.fullname" . }}-halyard-config
      {{- if .Values.halyard.additionalScripts.enabled }}
      - name: additional-config
        configMap:
          name: {{ .Values.halyard.additionalScripts.configMapName }}
      {{- end }}
      {{- if or .Values.halyard.additionalSecrets.create (hasKey .Values.halyard.additionalSecrets "name") }}
      - name: additional-secrets
        secret:
        {{- if .Values.halyard.additionalSecrets.create }}
          secretName: {{ template "spinnaker.fullname" . }}-additional-secrets
        {{- else if .Values.halyard.additionalSecrets.name }}
          secretName: {{ .Values.halyard.additionalSecrets.name }}
        {{- end }}
      {{- end }}
      {{- if .Values.halyard.additionalConfigMaps.create }}
      - name: additional-config-maps
        configMap:
          name: {{ template "spinnaker.fullname" . }}-additional-config-maps
      {{- end }}
      {{- if .Values.halyard.additionalScripts.create }}
      - name: additional-scripts
        configMap:
          name: {{ template "spinnaker.fullname" . }}-additional-scripts
      {{- end }}
      {{- if .Values.halyard.bom }}
      - name: halyard-bom
        configMap:
          name: {{ template "spinnaker.fullname" . }}-halyard-bom
      {{- end }}
      {{- if .Values.gcs.enabled }}
      - name: gcs-key
        secret:
        {{- if .Values.gcs.secretName }}
          secretName: {{ .Values.gcs.secretName }}
        {{- else }}
          secretName: {{ template "spinnaker.fullname" . }}-gcs
        {{- end }}
      {{- end }}
      {{- if and .Values.s3.enabled .Values.s3.accessKey .Values.s3.secretKey }}
      - name: s3-secrets
        secret:
          secretName: {{ template "spinnaker.fullname" .}}-s3
      {{- end }}
      {{- if .Values.halyard.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.halyard.image.pullSecrets }}
        - name: {{ . }}
        {{- end}}
      {{- end}}
      containers:
      - name: halyard-install
        image: {{ .Values.halyard.image.repository }}:{{ .Values.halyard.image.tag }}
        volumeMounts:
        - name: halyard-config
          mountPath: /opt/halyard/scripts
        {{- if .Values.halyard.additionalScripts.enabled }}
        - name: additional-config
          mountPath: /opt/halyard/additional
        {{- end }}
        {{- if or .Values.halyard.additionalSecrets.create (hasKey .Values.halyard.additionalSecrets "name") }}
        - name: additional-secrets
          mountPath: /opt/halyard/additionalSecrets
        {{- end }}
        {{- if .Values.halyard.additionalConfigMaps.create }}
        - name: additional-config-maps
          mountPath: /opt/halyard/additionalConfigMaps
        {{- end }}
        {{- if .Values.halyard.additionalScripts.create }}
        - name: additional-scripts
          mountPath: /opt/halyard/additionalScripts
        {{- end }}
        {{- if .Values.halyard.bom }}
        - name: halyard-bom
          mountPath: /opt/halyard/bom
        {{- end }}
        {{- if .Values.gcs.enabled }}
        - name: gcs-key
          mountPath: /opt/gcs
        {{- end }}
        {{- if and .Values.s3.enabled .Values.s3.accessKey .Values.s3.secretKey }}
        - name: s3-secrets
          mountPath: /opt/s3
        {{- end }}
        {{- if and .Values.kubeConfig.enabled .Values.kubeConfig.secretName }}
        - name: kube-config
          mountPath: /opt/kube
        {{- end }}
        command:
        - bash
        - -xe
        - "/opt/halyard/scripts/install.sh"