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

{{/*
Create a default fully qualified app name.
We truncate at 24 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "linkerd.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 24 -}}
{{- end -}}
 # config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: {{ template "linkerd.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "linkerd.fullname" . }}-config
data:
{{ if .Values.config }}
  config.yaml: |-
{{ toYaml .Values.config| indent 4 }}
{{ else }}
  config.yaml: |-
    admin:
      port: 9990

    namers:
    - kind: io.l5d.k8s
      # kubectl proxy forwards localhost:8001 to the Kubernetes master API
      host: localhost
      port: 8001
    routers:
    - protocol: http
      # Incoming requests to linkerd with a Host header of "hello" get assigned
      # a name like /http/1.1/GET/hello.  This dtab transforms that into
      # /#/io.l5d.k8s/{{ .Release.Namespace }}/{{ .Values.linkerd.discoveryPortName }}/hello which indicates that the kubernetes
      # namer should query the API for ports named "{{ .Values.linkerd.discoveryPortName }}" on pods in the
      # "{{ .Release.Namespace }}" namespace named "hello".  linkerd will then load balance over
      # those pods.
      dtab: |
        /http/1.1/* => /#/io.l5d.k8s/{{ .Release.Namespace }}/{{ .Values.linkerd.discoveryPortName }}
      servers:
      - ip: 0.0.0.0
        port: 4140
{{ end }}
 # daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: {{ template "linkerd.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "linkerd.fullname" . }}
spec:
  selector:
    matchLabels:
      app: {{ template "linkerd.fullname" . }}
      release: "{{ .Release.Name }}"
  template:
    metadata:
      annotations:
      {{- range $key, $value := .Values.linkerd.annotations }}
        {{ $key }}: {{ $value }}
      {{- end }}
      labels:
        app: {{ template "linkerd.fullname" . }}
        release: "{{ .Release.Name }}"
    spec:
      volumes:
      - name: {{ template "linkerd.fullname" . }}-config
        configMap:
          name: "{{ template "linkerd.fullname" . }}-config"
      containers:
      - name: {{ .Values.linkerd.name }}
        image: {{ .Values.linkerd.image }}
        env:
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        args:
        - /io.buoyant/linkerd/config/config.yaml
        ports:
        - name: incoming
          containerPort: 4141
          hostPort: 4141
        - name: outgoing
          containerPort: 4140
          hostPort: 4140
        - name: admin
          containerPort: 9990
          hostPort: 9990
        resources:
{{ toYaml .Values.linkerd.resources | indent 10 }}
        livenessProbe:
          httpGet:
            path: /admin
            port: 9990
          initialDelaySeconds: 10
          periodSeconds: 5
        volumeMounts:
        - name: "{{ template "linkerd.fullname" . }}-config"
          mountPath: "/io.buoyant/linkerd/config"
          readOnly: true
      - name: kubectl
        image: {{ .Values.kubectl.image }}
        args:
        - "proxy"
        - "-p"
        - "8001"
        resources:
{{ toYaml .Values.kubectl.resources | indent 10 }}
 # ingress.yaml
{{- if .Values.linkerd.ingress.enabled -}}
{{- $globalContext := . -}}
{{- $servicePort := .Values.linkerd.httpPort -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
  {{- range $key, $value := .Values.linkerd.ingress.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
  labels:
    app: {{ template "linkerd.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    component: "{{ .Values.linkerd.name }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "linkerd.fullname" . }}
spec:
  rules:
  {{- range .Values.linkerd.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - backend:
              serviceName: {{ template "linkerd.fullname" $globalContext }}
              servicePort: {{ $servicePort }}
  {{- end -}}
  {{- if .Values.linkerd.ingress.tls }}
  tls:
{{ toYaml .Values.linkerd.ingress.tls | indent 4 }}
  {{- end -}}
{{- end -}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "linkerd.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  annotations:
    prometheus.io/scrape: {{ .Values.prometheus.scrape | quote }}
    prometheus.io/probe: {{ .Values.prometheus.probe | quote }}
    prometheus.io/path: {{ .Values.prometheus.path | quote }}
    prometheus.io/port: "9990"
spec:
  type: {{ .Values.service.type }}
  ports:
  - name: outgoing
    port: 4140
  - name: incoming
    port: 4141
  - name: admin
    port: 9990
  selector:
    app: {{ template "linkerd.fullname" . }}

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

linkerd:
  ## linkerd Pod annotations:
  ##
  # annotations:
  #   iam.amazonaws.com/role: linkerd

  ## Additional linkerd container arguments
  ##
  # extraArgs:

  ## linkerd service port
  ##
  httpPort: 80

  ## linkerd service port name
  ## Default: 'admin'
  ##
  # httpPortName: admin

  ## linkerd user service discovery port name
  ## Default: 'http'
  discoveryPortName: http

  ## linkerd Docker image
  ##
  image: buoyantio/linkerd:1.1.2

  ingress:
    ## If true, linkerd Ingress will be created
    ##
    enabled: false

    ## linkerd Ingress annotations
    ##
    # annotations:
    #   kubernetes.io/ingress.class: nginx
    #   kubernetes.io/tls-acme: 'true'

    ## linkerd Ingress hostnames
    ## Must be provided if Ingress is enabled
    ##
    # hosts:
    #   - linkerd.domain.com

    ## linkerd Ingress TLS configuration
    ## Secrets must be manually created in the namespace
    ##
    # tls:
    #   - secretName:
    #     hosts:
    #       - linkerd.domain.com

  ## linkerd container name
  ##
  name: l5d
  # Resource configuration for the linkerd pods
  resources:
    limits:
      cpu: 500m
      memory: 512Mi
    requests:
      cpu: 0
      memory: 512Mi

kubectl:
  image: buoyantio/kubectl:v1.6.2
  # Resource configuration for the kubectl pods
  resources:
    # limits:
    #   cpu: 10m
    #   memory: 32Mi
    requests:
      cpu: 0
      memory: 32Mi

prometheus:
  # Enable Prometheus service metrics
  scrape: true
  # Enable Prometheus endpoint metrics
  probe: true
  # Path for Prometheus metrics
  path: /admin/metrics/prometheus
service:
  # Type of service to use for linkerd
  type: ClusterIP

# linkerd configuration. The default configuration is located inside the ConfigMap.
# Allows to overrides the linkerd configuration
config:
