# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" | lower -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "serviceAccountName" -}}
{{- if .Values.ingressMonitorController.serviceAccount.create -}}
    {{ default (include "fullname" .) .Values.ingressMonitorController.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.ingressMonitorController.serviceAccount.name }}
{{- end -}}
{{- end -}} # clusterrole.yaml
{{- if and ( .Values.ingressMonitorController.rbac.create ) (eq .Values.ingressMonitorController.watchNamespace "") }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role
{{- else }}
  name: {{ template "name" . }}-role
{{- end }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.rbac.labels }}
{{ toYaml .Values.ingressMonitorController.rbac.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
rules:
  - apiGroups:
      - ""
      - "extensions"
      - "route.openshift.io"
    resources:
      - ingresses
      - routes
    verbs:
      - list
      - get
      - update
      - patch
      - watch
      - delete
  - apiGroups:
      - ""
    resources:
      - services
      - pods
    verbs:
      - list
      - get
      - watch
{{- end }} # clusterrolebinding.yaml
{{- if and ( .Values.ingressMonitorController.rbac.create ) (eq .Values.ingressMonitorController.watchNamespace "") }}
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role-binding
{{- else }}
  name: {{ template "name" . }}-role-binding
{{- end }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.rbac.labels }}
{{ toYaml .Values.ingressMonitorController.rbac.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
roleRef:
  kind: ClusterRole
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role
{{- else }}
  name: {{ template "name" . }}-role
{{- end }}
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: {{ template "serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
{{- end }} # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}
{{- else }}
  name: {{ template "name" . }}
{{- end }}
  annotations:
{{- if .Values.ingressMonitorController.config.annotations }}
{{ toYaml .Values.ingressMonitorController.config.annotations | indent 4 }}
{{- end }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.config.labels }}
{{ toYaml .Values.ingressMonitorController.config.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
data:
  config.yaml: |-
    providers:
    {{- range .Values.ingressMonitorController.providers }}
    - name: {{ .name }}
      apiKey: {{ .apiKey }}
      apiURL: {{ .apiURL }}
      username: {{ .username | default "null" }}
      password: {{ .password | default "null" }}
      alertContacts: {{ .alertContacts }}
    {{- end }}
    enableMonitorDeletion: {{ .Values.ingressMonitorController.enableMonitorDeletion }}
    monitorNameTemplate: "{{ .Values.ingressMonitorController.monitorNameTemplate }}" # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
{{- if .Values.ingressMonitorController.deployment.annotations }}
{{ toYaml .Values.ingressMonitorController.deployment.annotations | indent 4 }}
{{- end }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.deployment.labels }}
{{ toYaml .Values.ingressMonitorController.deployment.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}
{{- else }}
  name: {{ template "name" . }}
{{- end }}
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: {{ template "name" . }}
      release: {{ .Release.Name | quote }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 6 }}
{{- end }}
  template:
    metadata:
      labels:
        app: {{ template "name" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: {{ .Release.Name | quote }}
        heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.deployment.labels }}
{{ toYaml .Values.ingressMonitorController.deployment.labels | indent 8 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 8 }}
{{- end }}
    spec:
      containers:
      - env:
      {{- if .Values.ingressMonitorController.watchNamespace }}
        - name: KUBERNETES_NAMESPACE
          value: {{ .Values.ingressMonitorController.watchNamespace | quote }}
      {{- end }}
        - name: CONFIG_FILE_PATH
          value: {{ .Values.ingressMonitorController.configFilePath }}
        image: "{{ .Values.ingressMonitorController.image.name }}:{{ .Values.ingressMonitorController.image.tag }}"
        imagePullPolicy: {{ .Values.ingressMonitorController.image.pullPolicy }}
      {{- if .Values.ingressMonitorController.useFullName }}
        name: {{ template "fullname" . }}
      {{- else }}
        name: {{ template "name" . }}
      {{- end }}
        volumeMounts:
        - mountPath: /etc/IngressMonitorController
          name: config-volume
      serviceAccountName: {{ template "serviceAccountName" . }}
      volumes:
      - configMap:
        {{- if .Values.ingressMonitorController.useFullName }}
          name: {{ template "fullname" . }}
        {{- else }}
          name: {{ template "name" . }}
        {{- end }}
        name: config-volume # role.yaml
{{- if and ( .Values.ingressMonitorController.rbac.create ) (.Values.ingressMonitorController.watchNamespace) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role
{{- else }}
  name: {{ template "name" . }}-role
{{- end }}
  namespace: {{ .Values.ingressMonitorController.watchNamespace | quote }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.rbac.labels }}
{{ toYaml .Values.ingressMonitorController.rbac.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
rules:
  - apiGroups:
      - ""
      - "extensions"
      - "route.openshift.io"
    resources:
      - ingresses
      - routes
    verbs:
      - list
      - get
      - update
      - patch
      - watch
      - delete
  - apiGroups:
      - ""
    resources:
      - services
      - pods
    verbs:
      - list
      - get
      - watch
{{- end }} # rolebinding.yaml
{{- if and ( .Values.ingressMonitorController.rbac.create ) (.Values.ingressMonitorController.watchNamespace) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role-binding
{{- else }}
  name: {{ template "name" . }}-role-binding
{{- end }}
  namespace: {{ .Values.ingressMonitorController.watchNamespace | quote }}
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.rbac.labels }}
{{ toYaml .Values.ingressMonitorController.rbac.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
{{- if .Values.ingressMonitorController.useFullName }}
  name: {{ template "fullname" . }}-role
{{- else }}
  name: {{ template "name" . }}-role
{{- end }}
subjects:
  - kind: ServiceAccount
    name: {{ template "serviceAccountName" . }}
    namespace: {{ .Release.Namespace | quote }}
{{- end }} # serviceaccount.yaml
{{- if .Values.ingressMonitorController.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template "name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- if .Values.ingressMonitorController.serviceAccount.labels }}
{{ toYaml .Values.ingressMonitorController.serviceAccount.labels | indent 4 }}
{{- end }}
{{- if .Values.ingressMonitorController.matchLabels }}
{{ toYaml .Values.ingressMonitorController.matchLabels | indent 4 }}
{{- end }}
  name: {{ template "serviceAccountName" . }}
{{- end }}
ingressMonitorController:
  watchNamespace: ""
  useFullName: false
  matchLabels: {}
  deployment:
    labels: {}
    annotations: {}
  config:
    labels: {}
    annotations:
      fabric8.io/target-platform: kubernetes
  rbac:
    create: true
    labels: {}
  # Service account config for the agent pods
  serviceAccount:
    # Specifies whether a ServiceAccount should be created
    create: true
    labels: {}
    # The name of the ServiceAccount to use.
    # If not set and create is true, a name is generated using the fullname template
    name: ingressmonitorcontroller
  image:
    name: stakater/ingressmonitorcontroller
    tag: "1.0.47"
    pullPolicy: IfNotPresent
  providers:
  - name: UptimeRobot
    apiKey: your-api-key
    apiURL: https://api.uptimerobot.com/v2
    alertContacts: some-alert-contacts
  enableMonitorDeletion: true
  configFilePath: /etc/IngressMonitorController/config.yaml
  monitorNameTemplate: "{{.Namespace}}-{{.IngressName}}"
