# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "kubed.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 "kubed.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" $name .Release.Name | trunc 63 -}}
{{- end -}}
 # cluster-role-binding.yaml
{{ if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "kubed.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kubed.fullname" . }}
subjects:
- kind: ServiceAccount
  name: {{ template "kubed.fullname" . }}
  namespace: {{ .Release.Namespace }}
{{ end }}
 # cluster-role.yaml
{{ if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "kubed.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
rules:
- apiGroups:
  - voyager.appscode.com
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - monitoring.appscode.com
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - stash.appscode.com
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - kubedb.com
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - monitoring.coreos.com
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - extensions
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups:
  - apps
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: [""]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  - secrets
  verbs: ["get", "create", "update"]
- apiGroups: ["autoscaling"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["batch"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["authorization.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["rbac.authorization.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["abac.authorization.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["authentication.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["storage.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
- apiGroups: ["certificates.k8s.io"]
  resources: ["*"]
  verbs: ["list", "watch"]
{{ end }}
 # deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "kubed.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "kubed.name" . }}"
        release: "{{ .Release.Name }}"
{{- if and .Values.criticalAddon (eq .Release.Namespace "kube-system") }}
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
{{- end }}
    spec:
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "kubed.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      containers:
      - name: kubed
        args:
        - run
        - --v={{ .Values.logLevel }}
        - --api.address=:8080
        - --web.address=:56790
        image: '{{ .Values.kubed.image }}:{{ .Values.kubed.tag }}'
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        {{- if .Values.imagePullSecrets }}
        imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
        {{- end }}
        ports:
        - containerPort: 8080
          name: api
          protocol: TCP
        - containerPort: 56790
          name: web
          protocol: TCP
        volumeMounts:
          - name: config
            mountPath: /srv/kubed
          - name: scratch
            mountPath: /tmp
      volumes:
        - name: config
          secret:
            secretName: {{ template "kubed.fullname" . }}
        - name: scratch
          emptyDir: {}
{{- if and .Values.criticalAddon (eq .Release.Namespace "kube-system") }}
      tolerations:
      - key: "CriticalAddonsOnly"
        operator: "Exists"
{{- end -}}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end -}}
 # secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    app: "{{ template "kubed.name" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
data:
  config.yaml: {{ `
apiServer:
  address: :8080
  enableReverseIndex: true
  enableSearchIndex: true
clusterName: unicorn
enableConfigSyncer: true
` | trim | b64enc | quote }}
 # service-account.yaml
{{ if .Values.rbac.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "kubed.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
{{ end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kubed.fullname" . }}
  labels:
    app: "{{ template "kubed.name" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  ports:
  - name: api
    port: 8080
    protocol: TCP
    targetPort: api
  - name: web
    port: 56790
    protocol: TCP
    targetPort: web
  selector:
    app: "{{ template "kubed.name" . }}"
    release: "{{ .Release.Name }}"

# Default values for kubed.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
kubed:
  image: appscode/kubed
  tag: 0.4.0
## Optionally specify an array of imagePullSecrets.
## Secrets must be manually created in the namespace.
## ref: https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod
##
# imagePullSecrets:
#   - name: myRegistryKeySecretName
## Specify a imagePullPolicy
## ref: http://kubernetes.io/docs/user-guide/images/#pre-pulling-images
##
imagePullPolicy: IfNotPresent
## Installs Searchlight operator as critical addon
## https://kubernetes.io/docs/tasks/administer-cluster/guaranteed-scheduling-critical-addon-pods/
criticalAddon: false
## Log level for kubed
logLevel: 3
## Node labels for pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
##
nodeSelector: {}
## Install Default RBAC roles and bindings
rbac:
  ## If true, create & use RBAC resources
  create: false
  ## Ignored if rbac.create is true
  serviceAccountName: default
