# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "keel.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- 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 "keel.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "keel.fullname" . }}
  labels:
    app: {{ template "keel.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    kubernetes.io/service-account.name: keel
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "keel.name" . }}
        release: {{ .Release.Name }}
    spec:
      containers:
        - name: keel
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/bin/keel"]
          env:
{{- if .Values.polling.enabled }}
            # Enable polling
            - name: POLL
              value: "1"
{{- else }}
            # Disable polling
            - name: POLL
              value: "0"
{{- end }}
{{- if .Values.helmProvider.enabled }}
            # Enable/disable Helm provider
            - name: HELM_PROVIDER
              value: "1"
{{- end }}
{{- if .Values.gcr.enabled }}
            # Enable GCR with pub/sub support
            - name: PROJECT_ID
              value: "{{ .Values.gcr.projectId }}"
            - name: PUBSUB
              value: "1"
{{- end }}
{{- if .Values.webhook.enabled }}
            # Enable webhook endpoint
            - name: WEBHOOK_ENDPOINT
              value: "{{ .Values.webhook.endpoint }}"
{{- end }}
{{- if .Values.slack.enabled }}
            - name: SLACK_TOKEN
              value: "{{ .Values.slack.token }}"
            - name: SLACK_CHANNELS
              value: "{{ .Values.slack.channel }}"
            - name: SLACK_BOT_NAME
              value: "{{ .Values.slack.botName }}"
            - name: SLACK_APPROVALS_CHANNEL
              value: "{{ .Values.slack.approvalsChannel }}"
{{- end }}
          ports:
            - containerPort: 9300
          livenessProbe:
            httpGet:
              path: /healthz
              port: 9300
            initialDelaySeconds: 30
            timeoutSeconds: 10
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- if .Values.webhookRelay.enabled }}
        - name: webhookrelayd
          image: "{{ .Values.webhookRelay.image.repository }}:{{ .Values.webhookRelay.image.tag }}"
          imagePullPolicy: {{ .Values.webhookRelay.image.pullPolicy }}
          command: ["/webhookrelayd"]
          env:
            - name: KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "keel.fullname" . }}-webhookrelay
                  key: key
            - name: SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "keel.fullname" . }}-webhookrelay
                  key: secret
            - name: BUCKET
              value: "{{ .Values.webhookRelay.bucket }}"
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- end }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
 # secrets-webhookrelay.yaml
{{- if .Values.webhookRelay.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "keel.fullname" . }}-webhookrelay
  labels:
    app: {{ template "keel.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: Opaque
data:
  key: {{ .Values.webhookRelay.key | b64enc }}
  secret:  {{ .Values.webhookRelay.secret | b64enc }}
{{- end }}
 # service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "keel.fullname" . }}
  labels:
    app: {{ template "keel.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
 # service.yaml
{{- if .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "keel.fullname" . }}
  labels:
    app: {{ template "keel.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.externalPort }}
      targetPort: 9300
      protocol: TCP
      name: keel
  selector:
    app: {{ template "keel.name" . }}
    release: {{ .Release.Name }}
  sessionAffinity: None
{{- end }}

# Default values for keel.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

image:
  repository: keelhq/keel
  tag: 0.9.5
  pullPolicy: IfNotPresent

# Polling is enabled by default,
# you can disable it setting value below to false
polling:
  enabled: true

# Helm provider support
helmProvider:
  enabled: false

# Google Container Registry
# GCP Project ID
gcr:
  enabled: false
  projectId: ""
  pubSub:
    enabled: false

# Webhook Notification
# Remote webhook endpoint for notification delivery
webhook:
  enabled: false
  endpoint: ""

# Slack Notification
slack:
  enabled: false
  token: ""
  channel: ""
  botName: ""
  approvalsChannel: ""

# Keel service
# Enable to receive webhooks from Docker registries
service:
  enabled: false
  type: LoadBalancer
  externalPort: 9300

# Webhook Relay service
# If you don’t want to expose your Keel service, you can use https://webhookrelay.com/
# which can deliver webhooks to your internal Keel service through Keel sidecar container.
webhookRelay:
  enabled: false
  # webhookrelay.com credentials
  key: ""
  secret: ""
  bucket: ""
  # webhookrelay docker image
  image:
    repository: webhookrelay/webhookrelayd
    tag: 0.6.3
    pullPolicy: IfNotPresent

# Keel self-update
# Comment lines below if you do not want Keel to automatically
# self-update to the latest release version
keel:
  # keel policy (all/major/minor/patch/force)
  policy: all
  # trigger type, defaults to events such as pubsub, webhooks
  trigger: poll
  # polling schedule
  pollSchedule: "@every 3m"
  # images to track and update
  images:
    - repository: image.repository
      tag: image.tag

# Resources
# resources:
#  limits:
#    cpu: 100m
#    memory: 128Mi
#  requests:
#    cpu: 50m
#    memory: 64Mi

# NodeSelector
nodeSelector: {}
