# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "gcp-night-king.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 "gcp-night-king.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "gcp-night-king.fullname" . }}-config
  labels:
    app: {{ template "gcp-night-king.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
data:
  project.id: {{ quote .Values.projectID }}
{{- if .Values.subscriptionName }}
  subscription.name: {{ quote .Values.subscriptionName }}
{{- end }}
 # deployment.yaml
{{- if ne (printf "%s" .Values.projectID) "MUST PROVIDE PROJECT ID" }}
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "gcp-night-king.fullname" . }}
  labels:
    app: {{ template "gcp-night-king.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "gcp-night-king.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum | quote }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /usr/bin/nightking
            - "-project"
            - $(PROJECT_ID)
{{- if .Values.subscriptionName }}
            - "-subscription-name"
            - $(SUBSCRIPTION_NAME)
{{- end }}
          env:
            - name: "PROJECT_ID"
              valueFrom:
                configMapKeyRef:
                  name: {{ template "gcp-night-king.fullname" . }}-config
                  key: project.id
{{- if .Values.subscriptionName }}
            - name: "SUBSCRIPTION_NAME"
              valueFrom:
                configMapKeyRef:
                  name: {{ template "gcp-night-king.fullname" . }}-config
                  key: subscription.name
{{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
{{- end }}

# Default values for gcp-night-king.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
  repository: itamarost/gcp-night-king
  tag: v1-golang
  pullPolicy: IfNotPresent
## Node labels for pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
##
nodeSelector: {}
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: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

# GCE project ID for GCE API usage - mandatory parameter
projectID: "MUST PROVIDE PROJECT ID"
# Pub/Sub subscription name to listen on
# subscriptionName: your-pubsub-subscription-name
