{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.healthIngress.enabled -}}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ printf "%s-health" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  annotations:
    {{- if .Values.healthIngress.certManager }}
    kubernetes.io/tls-acme: "true"
    {{- end }}
    {{- if or .Values.healthIngress.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.healthIngress.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
spec:
  {{- if .Values.healthIngress.ingressClassName }}
  ingressClassName: {{ .Values.healthIngress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.healthIngress.hostname }}
    - host: {{ .Values.healthIngress.hostname }}
      http:
        paths:
          {{- if .Values.healthIngress.extraPaths }}
          {{- include "common.tplvalues.render" (dict "value" .Values.healthIngress.extraPaths  "context" $) | nindent 10 }}
          {{- end }}
          - path: {{ .Values.healthIngress.path }}
            pathType: {{ .Values.healthIngress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "common.names.fullname" .) "servicePort" "http" "context" $)  | nindent 14 }}
    {{- end }}
    {{- range .Values.healthIngress.extraHosts }}
    - host: {{ .name }}
      http:
        paths:
          {{- if .Values.healthIngress.extraPaths }}
          {{- include "common.tplvalues.render" (dict "value" .Values.healthIngress.extraPaths  "context" $) | nindent 10 }}
          {{- end }}
          - path: {{ .Values.healthIngress.path }}
            pathType: {{ .Values.healthIngress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "common.names.fullname" $) "servicePort" "http" "context" $)  | nindent 14 }}
    {{- end }}
    {{- if .Values.healthIngress.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.healthIngress.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or .Values.healthIngress.tls .Values.healthIngress.extraTls .Values.healthIngress.hosts }}
  tls:
    {{- if .Values.healthIngress.tls }}
    - hosts:
        - {{ .Values.healthIngress.hostname }}
      secretName: {{ printf "%s-tls" .Values.healthIngress.hostname | trunc 63 | trimSuffix "-" }}
    {{- end }}
    {{- if .Values.healthIngress.extraTls }}
    {{- toYaml .Values.healthIngress.extraTls | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
