# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "buildkite.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 "buildkite.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # deployment.yaml
{{- if .Values.agent.token }}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "buildkite.fullname" . }}
  labels:
    app: {{ template "buildkite.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "buildkite.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccount: {{ template "buildkite.fullname" . }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag  }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            privileged: true
          env:
            # BUILDKITE AGENT ENV VARS
            - name: BUILDKITE_AGENT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ template "buildkite.fullname" . }}
                  key: agent-token
            - name: BUILDKITE_AGENT_META_DATA
              value: "{{ .Values.agent.meta }}"
            {{- if .Values.privateSshKey }}
            - name: SSH_PRIVATE_RSA_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "buildkite.fullname" . }}
                  key: agent-ssh
            {{- end }}
            # EXTRA BUILDKITE AGENT ENV VARS
{{- if .Values.extraEnv }}
{{ toYaml .Values.extraEnv | indent 12 }}
{{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
            - name: var-run-docker-sock
              mountPath: /var/run/docker.sock
            {{- if .Values.registryCreds.gcrServiceAccountKey }}
            - name: service-key
              mountPath: /etc/service_key
            {{- end }}
            {{- if .Values.registryCreds.dockerConfig }}
            - name: docker-config
              mountPath: /root/.docker
            {{- end }}
      volumes:
        - name: var-run-docker-sock
          hostPath:
            path: /var/run/docker.sock
        {{- if .Values.registryCreds.gcrServiceAccountKey }}
        - name: service-key
          secret:
            secretName: {{ template "buildkite.fullname" . }}-gcr
        {{- end }}
        {{- if .Values.registryCreds.dockerConfig }}
        - name: docker-config
          secret:
            secretName: {{ template "buildkite.fullname" . }}-registry
        {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
{{- end }}
 # secret-gcr.yaml
{{- if .Values.registryCreds.gcrServiceAccountKey }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "buildkite.fullname" . }}-gcr
  labels:
    app: {{ template "buildkite.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  service-account-key.json: |-
    {{ .Values.registryCreds.gcrServiceAccountKey }}
{{- end }}
 # secret-registry.yaml
{{- if .Values.registryCreds.dockerConfig }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "buildkite.fullname" . }}-registry
  labels:
    app: {{ template "buildkite.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  config.json: |-
    {{ .Values.registryCreds.dockerConfig }}
{{- end }}
 # secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "buildkite.fullname" . }}
  labels:
    app: {{ template "buildkite.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  agent-token: {{ .Values.agent.token | b64enc }}
{{- if .Values.privateSshKey }}
  agent-ssh: {{ .Values.privateSshKey | b64enc }}
{{- end }}
 # service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "buildkite.fullname" . }}
  labels:
    app: {{ template "buildkite.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}

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

replicaCount: 1

image:
  repository: buildkite/agent
  tag: 3.0
  pullPolicy: IfNotPresent

# Buildkite Agent settings
agent:
  # Your Buildkite agent token, it must be set
  token: ""
  # Agent meta-data, which can be used to assign jobs
  meta: "role=agent"

# Extra env vars to be passed
# If you do want extra env vars to pass to agent, uncomment the following
# lines, adjust them as necessary.
#
# extraEnv:
#  - name: test1
#    value: "test1"
#  - name: test2
#    value: "test2"

# Your ssh private key if you want to access private git repository
privateSshKey: ""

# Docker registries credentials
registryCreds:
  # GCP credentials for GCR
  # base64 encoded GCP Service account json key file
  gcrServiceAccountKey: ""
  # base64 encoded private registry docker config.json file
  # for quay.io, docker hub, ecr and etc
  dockerConfig: ""

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 500m
  #  memory: 1024Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

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