# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "weave-cloud.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).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "weave-cloud.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "weave-cloud.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "weave-cloud.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "weave-cloud.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Return the apiVersion of deployment.
*/}}
{{- define "deployment.apiVersion" -}}
{{- if semverCompare "<1.14-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: {{ template "deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ .Values.agent.name }}
  labels:
    app: {{ template "weave-cloud.name" . }}
    chart: {{ template "weave-cloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  minReadySeconds: 30
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      name: weave-agent
  template:
    metadata:
      labels:
        name: weave-agent
    spec:
      {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ template "weave-cloud.serviceAccountName" . }}
      {{- end }}
      terminationGracePeriodSeconds: 30
      containers:
      - name: agent
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - -agent.report-errors=true
        - -agent.poll-url=https://get.weave.works/k8s/agent.yaml?instanceID={{"{{.InstanceID}}"}}
        - -wc.hostname=cloud.weave.works
        - -wc.token=$(WEAVE_CLOUD_TOKEN)
        env:
        - name: WEAVE_CLOUD_TOKEN
          valueFrom:
            secretKeyRef:
              key: token
              name: weave-cloud
 # rbac.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: {{ template "weave-cloud.fullname" . }}
  labels:
    app: {{ template "weave-cloud.name" . }}
    chart: {{ template "weave-cloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  - apiGroups:
      - '*'
    resources:
      - '*'
    verbs:
      - '*'
  - nonResourceURLs:
      - '*'
    verbs:
      - '*'
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: {{ template "weave-cloud.fullname" . }}
  labels:
    app: {{ template "weave-cloud.name" . }}
    chart: {{ template "weave-cloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "weave-cloud.fullname" . }}
subjects:
  - name: {{ template "weave-cloud.serviceAccountName" . }}
    namespace: {{ .Release.Namespace | quote }}
    kind: ServiceAccount
{{- end -}} # secret.yaml
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: weave-cloud
data:
  token: {{ .Values.token | b64enc }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "weave-cloud.serviceAccountName" . }}
  labels:
    app: {{ template "weave-cloud.name" . }}
    chart: {{ template "weave-cloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- end -}}

# Default values for weave-cloud.

token: ""

agent:
  name: weave-agent

image:
  repository: weaveworks/launcher-agent
  tag: 1.4.0
  pullPolicy: IfNotPresent

rbac:
  # Specifies whether RBAC resources should be created
  create: true

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name:
