# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "presto.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 "presto.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 -}}

{{- define "presto.coordinator" -}}
{{ template "presto.fullname" . }}-coordinator
{{- end -}}

{{- define "presto.worker" -}}
{{ template "presto.fullname" . }}-worker
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "presto.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # configmap-coordinator.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "presto.coordinator" . }}
  labels:
    app: {{ template "presto.name" . }}
    chart: {{ template "presto.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: coordinator
data:
  node.properties: |
    node.environment={{ .Values.server.node.environment }}
    node.data-dir={{ .Values.server.node.dataDir }}
    plugin.dir={{ .Values.server.node.pluginDir }}

  jvm.config: |
    -server
    -Xmx{{ .Values.server.jvm.maxHeapSize }}
    -XX:+{{ .Values.server.jvm.gcMethod.type }}
    -XX:G1HeapRegionSize={{ .Values.server.jvm.gcMethod.g1.heapRegionSize }}
    -XX:+UseGCOverheadLimit
    -XX:+ExplicitGCInvokesConcurrent
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:+ExitOnOutOfMemoryError
    -Djdk.attach.allowAttachSelf=true

  config.properties: |
    coordinator=true
{{- if gt (int .Values.server.workers) 0 }}
    node-scheduler.include-coordinator=false
{{- else }}
    node-scheduler.include-coordinator=true
{{- end }}
    http-server.http.port={{ .Values.server.config.http.port }}
    query.max-memory={{ .Values.server.config.query.maxMemory }}
    query.max-memory-per-node={{ .Values.server.config.query.maxMemoryPerNode }}
    discovery-server.enabled=true
    discovery.uri=http://localhost:{{ .Values.server.config.http.port }}

  log.properties: |
    io.prestosql={{ .Values.server.log.presto.level }}

---
 # configmap-worker.yaml
{{- if gt (int .Values.server.workers) 0 }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "presto.worker" . }}
  labels:
    app: {{ template "presto.name" . }}
    chart: {{ template "presto.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: worker
data:
  node.properties: |
    node.environment={{ .Values.server.node.environment }}
    node.data-dir={{ .Values.server.node.dataDir }}
    plugin.dir={{ .Values.server.node.pluginDir }}

  jvm.config: |
    -server
    -Xmx{{ .Values.server.jvm.maxHeapSize }}
    -XX:+{{ .Values.server.jvm.gcMethod.type }}
    -XX:G1HeapRegionSize={{ .Values.server.jvm.gcMethod.g1.heapRegionSize }}
    -XX:+UseGCOverheadLimit
    -XX:+ExplicitGCInvokesConcurrent
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:+ExitOnOutOfMemoryError
    -Djdk.attach.allowAttachSelf=true

  config.properties: |
    coordinator=false
    http-server.http.port={{ .Values.server.config.http.port }}
    query.max-memory={{ .Values.server.config.query.maxMemory }}
    query.max-memory-per-node={{ .Values.server.config.query.maxMemoryPerNode }}
    discovery.uri=http://{{ template "presto.fullname" . }}:{{ .Values.server.config.http.port }}

  log.properties: |
    io.prestosql={{ .Values.server.log.presto.level }}

  health_check.sh: |
    #!/bin/bash 
    curl --silent {{ template "presto.fullname" . }}:{{ .Values.server.config.http.port }}/v1/node | tr "," "\n" | grep --silent $(hostname -i)

---
{{- end }} # deployment-coordinator.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "presto.coordinator" . }}
  labels:
    app: {{ template "presto.name" . }}
    chart: {{ template "presto.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: coordinator
spec:
  selector:
    matchLabels:
      app: {{ template "presto.name" . }}
      release: {{ .Release.Name }}
      component: coordinator
  template:
    metadata:
      labels:
        app: {{ template "presto.name" . }}
        release: {{ .Release.Name }}
        component: coordinator
    spec:
      {{- with .Values.image.securityContext }}
      securityContext:
        runAsUser: {{ .runAsUser }}
        runAsGroup: {{ .runAsGroup }}
      {{- end }}
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "presto.coordinator" . }}
      containers:
        - name: {{ .Chart.Name }}-coordinator
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
            - mountPath: {{ .Values.server.config.path }}
              name: config-volume
          ports:
            - name: http-coord
              containerPort: {{ .Values.server.config.http.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /v1/cluster
              port: http-coord
          readinessProbe:
            httpGet:
              path: /v1/cluster
              port: http-coord
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # deployment-worker.yaml
{{- if gt (int .Values.server.workers) 0 }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "presto.worker" . }}
  labels:
    app: {{ template "presto.name" . }}
    chart: {{ template "presto.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: worker
spec:
  replicas: {{ .Values.server.workers }}
  selector:
    matchLabels:
      app: {{ template "presto.name" . }}
      release: {{ .Release.Name }}
      component: worker
  template:
    metadata:
      labels:
        app: {{ template "presto.name" . }}
        release: {{ .Release.Name }}
        component: worker
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "presto.worker" . }}
      containers:
        - name: {{ .Chart.Name }}-worker
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
            - mountPath: {{ .Values.server.config.path }}
              name: config-volume
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - {{ .Values.server.config.path }}/health_check.sh
            initialDelaySeconds: 10
            periodSeconds: 25
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - {{ .Values.server.config.path }}/health_check.sh
            initialDelaySeconds: 5
            periodSeconds: 10
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}

{{- end }} # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "presto.fullname" . }}
  labels:
    app: {{ template "presto.name" . }}
    chart: {{ template "presto.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.server.config.http.port }}
      targetPort: http-coord
      protocol: TCP
      name: http-coord
  selector:
    app: {{ template "presto.name" . }}
    release: {{ .Release.Name }}
    component: coordinator

server:
  workers: 2
  node:
    environment: production
    dataDir: /data/presto
    pluginDir: /usr/lib/presto/plugin
  log:
    presto:
      level: INFO
  config:
    path: /usr/lib/presto/etc
    http:
      port: 8080
    query:
      maxMemory: "4GB"
      maxMemoryPerNode: "1GB"
  jvm:
    maxHeapSize: "8G"
    gcMethod:
      type: "UseG1GC"
      g1:
        heapRegionSize: "32M"

image:
  repository: prestosql/presto
  tag: 329
  pullPolicy: IfNotPresent
  securityContext:
    runAsUser: 1000
    runAsGroup: 1000

service:
  type: ClusterIP

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

nodeSelector: {}

tolerations: []

affinity: {}
