# helm_charts_halyard.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "spinnaker.fullname" . }}-halyard
  labels:
{{ include "spinnaker.standard-labels" . | indent 4 }}
spec:
  serviceName: {{ template "spinnaker.fullname" . }}-halyard
  replicas: 1
  selector:
    matchLabels:
{{ include "spinnaker.standard-selector-labels" . | indent 6 }}
      component: halyard
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap/halyard-init-script.yaml") . | sha256sum }}
      {{- if .Values.halyard.annotations }}
{{ toYaml .Values.halyard.annotations | indent 8 }}
      {{- end }}
      labels:
{{ include "spinnaker.standard-labels" . | indent 8 }}
        component: halyard
    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 }}
      initContainers:
      - name: "create-halyard-local"
        image: {{ .Values.halyard.image.repository }}:{{ .Values.halyard.image.tag }}
        command:
        - bash
        - /tmp/initscript/init.sh
        volumeMounts:
        - name: halyard-config
          mountPath: /tmp/config
        - name: service-settings
          mountPath: /tmp/service-settings
        {{- if .Values.halyard.serviceConfigs }}
        - name: service-configs
          mountPath: /tmp/service-configs
        {{- end }}
        - name: halyard-home
          mountPath: /tmp/spinnaker
        - name: additional-profile-config-maps
          mountPath: /tmp/additionalProfileConfigMaps
        {{- if .Values.halyard.bom }}
        - name: halyard-bom
          mountPath: /tmp/halyard-bom
        {{- end }}
        - name: halyard-initscript
          mountPath: /tmp/initscript
        {{- if .Values.halyard.customCerts.enabled }}
        - mountPath: /etc/ssl/certs/java/cacerts
          subPath: cacerts
          name: cacerts
        {{- end }}
        {{- if .Values.halyard.env }}
        env:
{{ toYaml .Values.halyard.env | indent 8 }}
        {{- end }}
      volumes:
      {{- if not .Values.halyard.persistence.enabled }}
      - name: halyard-home
        emptyDir: {}
      {{- end }}
      {{- if and .Values.kubeConfig.enabled .Values.kubeConfig.secretName }}
      - name: kube-config
        secret:
          secretName: {{ .Values.kubeConfig.secretName }}
      {{- 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 }}
      - name: reg-secrets
        secret:
          {{- if .Values.dockerRegistryAccountSecret }}
          secretName: {{ .Values.dockerRegistryAccountSecret }}
          {{- else }}
          secretName: {{ template "spinnaker.fullname" . }}-registry
          {{- end }}
      {{- if and .Values.s3.enabled .Values.s3.accessKey .Values.s3.secretKey }}
      - name: s3-secrets
        secret:
          secretName: {{ template "spinnaker.fullname" . }}-s3
      {{- 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 or .Values.halyard.additionalConfigMaps.create (hasKey .Values.halyard.additionalConfigMaps "name") }}
      - name: additional-config-maps
        configMap:
        {{- if .Values.halyard.additionalConfigMaps.create }}
          name: {{ template "spinnaker.fullname" . }}-additional-config-maps
        {{- else if .Values.halyard.additionalConfigMaps.name }}
          name: {{ .Values.halyard.additionalConfigMaps.name }}
        {{- end }}
      {{- end }}
      - name: additional-profile-config-maps
        configMap:
          name: {{ template "spinnaker.fullname" . }}-additional-profile-config-maps
      {{- if .Values.halyard.bom }}
      - name: halyard-bom
        configMap:
          name: {{ template "spinnaker.fullname" . }}-halyard-bom
      {{- end }}
      - name: halyard-config
        emptyDir: {}
      - name: service-settings
        configMap:
          name: {{ template "spinnaker.fullname" . }}-service-settings
      {{- if .Values.halyard.serviceConfigs }}
      - name: service-configs
        configMap:
          name: {{ template "spinnaker.fullname" . }}-service-configs
      {{- end }}
      - name: halyard-initscript
        configMap:
          name: {{ template "spinnaker.fullname" . }}-halyard-init-script
      {{- if .Values.halyard.customCerts.enabled }}
      - name: cacerts
        secret:
          secretName: {{ .Values.halyard.customCerts.secretName }}
          items:
          - key: cacerts
            path: cacerts
      {{- end }}
      {{- if .Values.halyard.image.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.halyard.image.pullSecrets }}
        - name: {{ . }}
        {{- end}}
      {{- end}}
      containers:
      - name: halyard
        image: {{ .Values.halyard.image.repository }}:{{ .Values.halyard.image.tag }}
        ports:
        - containerPort: 8064
          name: daemon
        {{- if .Values.halyard.resources }}
        resources:
{{ toYaml .Values.halyard.resources | indent 10 }}
        {{- end }}
        volumeMounts:
        {{- 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 }}
        {{- if or .Values.halyard.additionalSecrets.create (hasKey .Values.halyard.additionalSecrets "name") }}
        - name: additional-secrets
          mountPath: /opt/halyard/additionalSecrets
        {{- end }}
        {{- if or .Values.halyard.additionalConfigMaps.create (hasKey .Values.halyard.additionalConfigMaps "name") }}
        - name: additional-config-maps
          mountPath: /opt/halyard/additionalConfigMaps
        {{- end }}
        - name: halyard-home
          mountPath: /home/spinnaker
        - name: halyard-config
          mountPath: /opt/halyard/config
        - name: reg-secrets
          mountPath: /opt/registry/passwords
        {{- if .Values.halyard.customCerts.enabled }}
        - mountPath: /etc/ssl/certs/java/cacerts
          subPath: cacerts
          name: cacerts
        {{- end }}
        {{- if .Values.halyard.env }}
        env:
{{ toYaml .Values.halyard.env | indent 8 }}
        {{- end }}
{{- if .Values.halyard.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: halyard-home
      labels:
{{ include "spinnaker.standard-labels-base" . | indent 8 }}
    spec:
      accessModes: [ "ReadWriteOnce" ]
      {{- with .Values.halyard.persistence.storageClass }}
      storageClassName: {{ . }}
      {{- end }}
      resources:
        requests:
          storage: 10Gi
{{- end }}