# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "kube-lego.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 "kube-lego.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:
  labels:
    app: {{ template "kube-lego.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "kube-lego.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "kube-lego.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "kube-lego.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      containers:
        - name: {{ template "kube-lego.name" . }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.pullPolicy }}"
          env:
            - name: LEGO_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: LEGO_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
          {{- range $key, $value := .Values.config }}
            - name: "{{ $key }}"
              value: "{{ $value }}"
          {{- end }}
          ports:
            - containerPort: {{ .Values.config.LEGO_PORT }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.config.LEGO_PORT }}
            initialDelaySeconds: 5
            timeoutSeconds: 1
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
 # role.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  labels:
    app: {{ template "kube-lego.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "kube-lego.fullname" . }}
rules:
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - create
  - get
  - delete
  - update
- apiGroups:
  - extensions
  resources:
  - ingresses
  verbs:
  - get
  - update
  - create
  - list
  - patch
  - delete
  - watch
- apiGroups:
  - ""
  resources:
  - endpoints
  - secrets
  verbs:
  - get
  - create
  - update
{{- end -}}
 # rolebinding.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  labels:
    app: {{ template "kube-lego.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "kube-lego.fullname" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kube-lego.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "kube-lego.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- end -}}
 # serviceaccount.yaml
{{- if .Values.rbac.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template "kube-lego.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "kube-lego.fullname" . }}
{{- end -}}

## kube-lego configuration
## Ref: https://github.com/jetstack/kube-lego
##
config:
  ## Email address to use for registration with Let's Encrypt
  ##
  LEGO_EMAIL: foo@example.com

  ## Let's Encrypt API endpoint
  ## Production: https://acme-v01.api.letsencrypt.org/directory
  ## Staging: https://acme-staging.api.letsencrypt.org/directory
  ##
  LEGO_URL: https://acme-staging.api.letsencrypt.org/directory

  ## kube-lego port
  ##
  LEGO_PORT: 8080

  ## Specify which ingress class to watch
  ##
  # LEGO_SUPPORTED_INGRESS_CLASS: nginx

  ## Specify which ingress provider should be watched
  ## nginx and gce are the only two options presently
  ##
  # LEGO_SUPPORTED_INGRESS_PROVIDER: nginx

  ## Specify what ingress class should something be
  ## if no ingress class is specified
  ##
  # LEGO_DEFAULT_INGRESS_CLASS: nginx

## kube-lego image
##
image:
  repository: jetstack/kube-lego
  tag: 0.1.6
  pullPolicy: IfNotPresent

## Node labels for pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
##
nodeSelector: {}

## Annotations to be added to pods
##
podAnnotations: {}

replicaCount: 1

## kube-lego resource limits & requests
## Ref: https://kubernetes.io/docs/user-guide/compute-resources/
##
resources: {}
  # limits:
  #   cpu: 20m
  #   memory: 8Mi
  # requests:
  #   cpu: 20m
  #   memory: 8Mi

rbac:
  create: false
  serviceAccountName: default
