
{{- if empty (include "concourse.host" .) -}}
###############################################################################
### ERROR: You did not provide an external host in your 'helm install' call ###
###############################################################################

This deployment will be incomplete until you configure Concourse with a resolvable
host. To configure Concourse with the URL of your service:

1. Get the Concourse URL by running:

  {{- if eq .Values.service.web.type "NodePort" }}

  export APP_PORT=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "concourse.web.fullname" . }} -o jsonpath="{.spec.ports[0].nodePort}")
  export APP_HOST=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")

  {{- else if eq .Values.service.web.type "LoadBalancer" }}

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "concourse.web.fullname" . }}'

  export APP_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "concourse.web.fullname" . }} --template "{{ "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}" }}")
  {{- end }}
  export DATABASE_ROOT_PASSWORD=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "concourse.postgresql.secretName" . }} -o jsonpath="{.data.postgresql-postgres-password}" | base64 --decode)
  export APP_DATABASE_PASSWORD=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "concourse.postgresql.secretName" . }} -o jsonpath="{.data.postgresql-password}" | base64 --decode)
  {{- if and .Values.secrets.localAuth.enabled }}
  export APP_CREDENTIALS=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "concourse.web.secretName" . }} -o jsonpath="{.data.local-users}" | base64 --decode)
  {{- end }}

2. Complete your Concourse deployment by running:

{{- if .Values.postgresql.enabled }}

  helm upgrade --namespace {{ .Release.Namespace }} {{ .Release.Name }} bitnami/{{ .Chart.Name }} --set web.externalUrl=$APP_HOST,{{- if and .Values.secrets.localAuth.enabled }}secrets.localUsers=$APP_CREDENTIALS,{{- end }}postgresql.postgresqlPostgresPassword=$DATABASE_ROOT_PASSWORD,postgresql.postgresqlPassword=$APP_DATABASE_PASSWORD{{- if .Values.global }}{{- if .Values.global.imagePullSecrets }},global.imagePullSecrets={{ .Values.global.imagePullSecrets }}{{- end }}{{- end }}
{{- else }}

  ## PLEASE UPDATE THE EXTERNAL DATABASE CONNECTION PARAMETERS IN THE FOLLOWING COMMAND AS NEEDED ##

  helm upgrade --namespace {{ .Release.Namespace }} {{ .Release.Name }} bitnami/{{ .Chart.Name }} --set web.externalUrl=$APP_HOST,{{- if and .Values.secrets.localAuth.enabled }}secrets.localUsers=$APP_CREDENTIALS,{{- end }}service.web.type={{ .Values.service.web.type }},postgresql.enabled=false{{- if not (empty .Values.externalDatabase.host) }},externalDatabase.host={{ .Values.externalDatabase.host }}{{- end }}{{- if not (empty .Values.externalDatabase.user) }},externalDatabase.user={{ .Values.externalDatabase.user }}{{- end }}{{- if not (empty .Values.externalDatabase.password) }},externalDatabase.password={{ .Values.externalDatabase.password }}{{- end }}{{- if not (empty .Values.externalDatabase.database) }},externalDatabase.database={{ .Values.externalDatabase.database }}{{- end }}{{- if .Values.global }}{{- if .Values.global.imagePullSecrets }},global.imagePullSecrets={{ .Values.global.imagePullSecrets }}{{- end }}{{- end }}
{{- end }}

{{- else }}

* Concourse can be accessed:

  * Within your cluster, at the following DNS name at port {{ .Values.web.containerPort }}:

    {{ template "concourse.web.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local

  * From outside the cluster, run these commands in the same shell:
    {{- if contains "NodePort" .Values.service.web.type }}

    export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "concourse.web.fullname" . }})
    export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
    echo http://$NODE_IP:$NODE_PORT
    {{- else if contains "LoadBalancer" .Values.service.web.type }}

     NOTE: It may take a few minutes for the LoadBalancer IP to be available.
           You can watch the status of by running 'kubectl get svc -w {{ template "concourse.web.fullname" . }}'

    echo http://$SERVICE_IP:{{ .Values.web.containerPort }}
    {{- else if contains "ClusterIP"  .Values.service.web.type }}

    export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "concourse.web.fullname" . }}" -o jsonpath="{.items[0].metadata.name}")
    echo "Visit http://127.0.0.1:8080 to use Concourse"
    kubectl port-forward --namespace {{ .Release.Namespace }} $POD_NAME 8080:{{ .Values.web.containerPort }}
    {{- end }}

  echo Username : Password  $(kubectl get secret --namespace {{ .Release.Namespace }} {{ include "concourse.web.secretName" . }} -o jsonpath="{.data.local-users}" | base64 --decode)

* If this is your first time using Concourse, follow the examples at https://concourse-ci.org/examples.html
{{- end }}
{{ include "concourse.validateValues" . }}

{{- include "common.warnings.rollingTag" .Values.image }}

{{- $passwordValidationErrors := list -}}

{{- if and .Values.secrets.localAuth.enabled (not .Values.web.existingSecret) -}}
    {{- $secretName := include "concourse.web.secretName" . -}}
    {{- $requiredWebPassword := dict "valueKey" "secrets.localUsers" "secret" $secretName "field" "local-users" "context" $ -}}
    {{- $requiredWebPasswordError := include "common.validations.values.single.empty" $requiredWebPassword -}}
    {{- $passwordValidationErrors = append $passwordValidationErrors $requiredWebPasswordError -}}
{{- end }}

{{- $postgresqlSecretName := include "concourse.postgresql.secretName" . -}}
{{- $postgresqlPasswordValidationErrors := include "common.validations.values.postgresql.passwords" (dict "secret" $postgresqlSecretName "subchart" true "context" $) -}}
{{- $passwordValidationErrors = append $passwordValidationErrors $postgresqlPasswordValidationErrors -}}

{{- include "common.errors.upgrade.passwords.empty" (dict "validationErrors" $passwordValidationErrors "context" $) -}}
