# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "heapster.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 "heapster.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 a service name that defaults to app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "heapster.service.fullname" -}}
{{- .Values.service.nameOverride | default .Chart.Name }}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "heapster.fullname" . }}
  annotations:
{{ toYaml .Values.annotations | indent 4 }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    {{- range $key, $value := .Values.labels }}
    {{ $key }}: {{ $value }}
    {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "heapster.fullname" . }}
  template:
    metadata:
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
      labels:
        app: {{ template "heapster.fullname" . }}
    spec:
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "heapster.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
{{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
{{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 6 }}
{{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - containerPort: {{ .Values.service.internalPort }}
        livenessProbe:
          httpGet:
            path: /healthz
            port: {{ .Values.service.internalPort }}
          initialDelaySeconds: 180
          timeoutSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 12 }}
      {{- if .Values.env }}
        env:
{{ toYaml .Values.env | indent 10 }}
      {{- end }}
        command:
          {{- range .Values.command }}
          - {{ . | quote }}
          {{- end }}
{{ if .Values.resizer.enabled }}
      - name: {{ .Chart.Name }}-nanny
        {{ with .Values.resizer }}
        image: "{{ .image.repository }}:{{ .image.tag }}"
        imagePullPolicy: {{ .image.pullPolicy }}
        resources:
{{ toYaml .resources | indent 12 }}
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        {{- end }}
        command:
          - "/pod_nanny"
          - "--deployment={{ template "heapster.fullname" . }}"
          - "--container={{ .Chart.Name }}"
          {{- range .Values.resizer.flags }}
          - {{ . | quote }}
          {{- end }}
{{- end -}}
{{ if .Values.eventer.enabled }}
      - name: {{ .Chart.Name }}-eventer
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
        - /eventer
        {{- range .Values.eventer.flags }}
        - {{ . | quote }}
        {{- end }}
        resources:
{{ toYaml .Values.eventer.resources | indent 12 }}
{{ if .Values.eventer.resizer.enabled }}
      - name: {{ .Chart.Name }}-eventer-nanny
        image: "{{ .Values.resizer.image.repository }}:{{ .Values.resizer.image.tag }}"
        imagePullPolicy: {{ .Values.resizer.image.pullPolicy }}
        resources:
{{ toYaml .Values.eventer.resizer.resources | indent 12 }}
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        command:
          - /pod_nanny
          - "--deployment={{ template "heapster.fullname" . }}"
          - "--container={{ .Chart.Name }}-eventer"
          {{- range .Values.eventer.resizer.flags }}
          - {{ . | quote }}
          {{- end }}
{{- end -}}
{{- end -}}
 # heapster-crb.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  labels:
    app: {{ template "heapster.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "heapster.fullname" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:heapster
subjects:
  - kind: ServiceAccount
    name: {{ template "heapster.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- end -}}
 # pod-nanny-role.yaml
{{- if .Values.rbac.create -}}
{{- if .Values.resizer.enabled -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: {{ template "heapster.fullname" . }}-pod-nanny
  labels:
    app: {{ template "heapster.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - "extensions"
  resources:
  - deployments
  verbs:
  - get
  - update
{{- end -}}
{{- end -}}
 # pod-nanny-rolebinding.yaml
{{ if .Values.resizer.enabled -}}
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: {{ template "heapster.fullname" . }}-pod-nanny
  labels:
    app: {{ template "heapster.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "heapster.fullname" . }}-pod-nanny
subjects:
- kind: ServiceAccount
  name: {{ template "heapster.fullname" . }}
  namespace: {{ .Release.Namespace }}
{{- end -}}
{{- end -}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "heapster.service.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: {{ template "heapster.fullname" . }}
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
    {{- range $key, $value := .Values.service.labels }}
    {{ $key }}: {{ $value }}
    {{- end }}    
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
  - port: {{ .Values.service.externalPort }}
    targetPort: {{ .Values.service.internalPort }}
    protocol: TCP
  selector:
    app: {{ template "heapster.fullname" . }}
 # serviceaccount.yaml
{{- if .Values.rbac.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template "heapster.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "heapster.fullname" . }}
{{- end -}}

## Default values for heapster.
##
replicaCount: 1
image:
  repository: k8s.gcr.io/heapster-amd64
  tag: v1.5.4
  pullPolicy: IfNotPresent
## Here labels can be added to the heapster deployment
# labels:
#   kubernetes.io/cluster-service: "true"
#   kubernetes.io/name: "Heapster"
labels: {}

## Here labels can be added to the heapster deployment
# annotations:
#   scheduler.alpha.kubernetes.io/critical-pod: ''
annotations: {}

## Here annotations can be added for the heapster Pod
# podAnnotations:
#   prometheus.io/scrape: "true"
podAnnotations: {}

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

## Tolerations for pod assignment
## Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
##
tolerations: []

## Affinity for pod assignment
## Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
affinity: {}

service:
  type: ClusterIP
  externalPort: 8082
  internalPort: 8082
  ## This allows an override of the heapster service name
  ## Default: {{ .Chart.Name }}
  # nameOverride:

  ## Here labels can be added to the heapster service
  # labels:
  #   kubernetes.io/cluster-service: "true"
  #   kubernetes.io/name: "Heapster"
  labels:

  ## Here annotations can be added to the heapster service
  # annotations:
  #   prometheus.io/path: /metrics
  #   prometheus.io/port: "8082"
  #   prometheus.io/scrape: "true"
  annotations: {}
resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi

## Heapster command and arguments
## Default source=kubernetes.summary_api:''
## ref: https://github.com/kubernetes/heapster/blob/master/docs/source-configuration.md
##
## By default sink not set
## ref: https://github.com/kubernetes/heapster/blob/master/docs/sink-configuration.md
##
command:
- "/heapster"
- "--source=kubernetes.summary_api:''"

## heapster env variables
env: []

## Resizer scales resources linearly with the number of nodes in the cluster
## Resizer is enabled by default
##
resizer:
  enabled: true
  image:
    repository: k8s.gcr.io/addon-resizer
    tag: 1.7
    pullPolicy: IfNotPresent
  resources:
    limits:
      cpu: 50m
      memory: 90Mi
    requests:
      cpu: 50m
      memory: 90Mi

  ## Flags used for /pod_nanny command
  ## container and deployment flags already determined chart name
  ## ref: https://github.com/kubernetes/contrib/blob/master/addon-resizer/README.md
  ##
  flags:
  - "--cpu=150m"
  - "--extra-cpu=10m"
  - "--memory=200Mi"
  - "--extra-memory=6Mi"
  - "--threshold=5"
  - "--poll-period=300000"

## For RBAC support:
rbac:
  create: true

  ## Ignored if rbac.create is true
  ##
  serviceAccountName: default

## eventer can send the kubernetes event logs to a remote destination
## it uses the same image as heapster but has its own resizer nanny pod
## eventer is disabled by default
## see https://github.com/kubernetes/heapster/blob/master/docs/overview.md for the flags you can use
## you will probably want to change the --sink parameter
eventer:
  enabled: false
  flags:
  - "--source=kubernetes:https://kubernetes.default"
  - "--sink=log"
  resources: {}
#    limits:
#      cpu: 100m
#      memory: 250Mi
#    requests:
#      cpu: 100m
#      memory: 250Mi
  resizer:
    enabled: true
    resources: {}
#      limits:
#        cpu: 50m
#        memory: 90Mi
#      requests:
#        cpu: 50m
#        memory: 90Mi
    flags:
    - "--cpu=150m"
    - "--extra-cpu=10m"
    - "--memory=200Mi"
    - "--extra-memory=6Mi"
    - "--threshold=5"
    - "--poll-period=300000"
