# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "g2.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 "g2.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # cluster-role-binding.yaml
{{ if .Values.rbac.install }}
{{- $serviceName := include "g2.fullname" . -}}
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
kind: ClusterRoleBinding
metadata:
  name: {{ $serviceName }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "g2.fullname" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}
  namespace: {{ .Release.Namespace }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ $serviceName }}
{{ end }}
 # cluster-role.yaml
{{ if .Values.rbac.install }}
{{- $serviceName := include "g2.fullname" . -}}
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
kind: ClusterRole
metadata:
  name: {{ $serviceName }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "g2.fullname" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
rules:
- apiGroups:
  - extensions
  resources:
  - deployments
  verbs: ["get", "create", "update"]
- apiGroups: [""]
  resources:
  - services
  verbs: ["create", "update"]
- apiGroups: [""]
  resources:
  - pods
  verbs: ["list", "create", "update", "delete"]
{{ end }}
 # deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "g2.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "g2.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "g2.name" . }}"
        release: "{{ .Release.Name }}"
    spec:
{{- if .Values.rbac.install }}
      serviceAccountName: {{ template "g2.fullname" . }}
{{- end }}
      containers:
      - args:
        - run
        - --storage-dir=/var/db
        - --v=5
        image: '{{ .Values.g2.image }}:{{ .Values.g2.tag }}'
        imagePullPolicy: '{{ .Values.g2.pullPolicy }}'
        name: gearman-server
        ports:
        - containerPort: 4730
          name: http
          protocol: TCP
        - containerPort: 3000
          name: restapi
          protocol: TCP
        volumeMounts:
        - mountPath: /var/db
          name: data-volume
      volumes:
      - emptyDir: {}
        name: data-volume
 # service-account.yaml
{{ if .Values.rbac.install }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "g2.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "g2.fullname" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
{{ end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "g2.fullname" . }}
  labels:
    app: "{{ template "g2.name" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  ports:
  - name: http
    port: 4730
    protocol: TCP
    targetPort: http
  - name: restapi
    port: 3000
    protocol: TCP
    targetPort: restapi
  selector:
    app: "{{ template "g2.name" . }}"
    release: "{{ .Release.Name }}"
  type: '{{ .Values.g2.serviceType }}'

replicaCount: 1
g2:
  image: appscode/gearmand
  pullPolicy: IfNotPresent
  tag: 0.5.0
  serviceType: ClusterIP
rbac:
  install: false
  apiVersion: v1beta1
