{{- if .Values.ingress.enabled -}}
{{- $fullName := include "keel.fullname" . -}}

{{- if .Values.gcloud.managedCertificates.enabled }}
apiVersion: networking.gke.io/v1beta1
kind: ManagedCertificate
metadata:
  name: {{ $fullName }}
  namespace: {{ .Release.Namespace }}
spec:
  domains:
    {{- range .Values.gcloud.managedCertificates.domains }}
    - {{ . }}
    {{- end }}
{{- end }}
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "keel.name" . }}
    helm.sh/chart: {{ include "keel.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- with .Values.ingress.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  annotations:
  {{- with .Values.ingress.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if .Values.gcloud.managedCertificates.enabled }}
    networking.gke.io/managed-certificates: {{ $fullName }}
  {{- end }}
spec:
{{- if not .Values.ingress.hosts }}
  backend:
    serviceName: {{ $fullName }}
    servicePort: keel
{{- end }}
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
{{- if .Values.ingress.hosts }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
        {{- range .paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: keel
        {{- end }}
  {{- end }}
{{- end }}
{{- end }}
