apiVersion: v1
kind: ConfigMap
metadata:
  name: presto-coordinator-config
  labels:
    app: presto
data:
  log.properties: |
    com.facebook.presto={{ upper .Values.presto.spec.coordinator.config.logLevel }}

  node.properties: |
    node.data-dir=/var/presto/data
    node.environment=production

  config.properties: |
    coordinator=true
    node-scheduler.include-coordinator={{ .Values.presto.spec.config.nodeSchedulerIncludeCoordinator }}
    discovery-server.enabled=true

{{- if .Values.presto.spec.config.tls.enabled }}
    http-server.http.enabled=false
    http-server.https.enabled=true
    http-server.https.port=8080
    http-server.https.keystore.key=changeit
    http-server.https.keystore.path=/opt/presto/tls/keystore.pem
    http-server.https.truststore.path=/opt/presto/tls/truststore.pem
    node.internal-address-source=FQDN
    discovery.uri=https://presto:8080
    internal-communication.https.required=true
    internal-communication.https.keystore.path=/opt/presto/tls/keystore.pem
    internal-communication.https.truststore.path=/opt/presto/tls/truststore.pem
{{- else }}
    http-server.http.port=8080
    discovery.uri=http://presto:8080
{{- end }}
{{- if .Values.presto.spec.config.auth.enabled }}
    http-server.authentication.type=CERTIFICATE
{{- end }}

    jmx.rmiserver.port=8081
    jmx.rmiregistry.port=8081

{{- if .Values.presto.spec.config.maxQueryLength }}
    query.max-length={{ .Values.presto.spec.config.maxQueryLength }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.taskConcurrency }}
    task.concurrency={{ .Values.presto.spec.coordinator.config.taskConcurrency }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.taskMaxWorkerThreads }}
    task.max-worker-threads: {{ .Values.presto.spec.coordinator.config.taskMaxWorkerThreads }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.taskMinDrivers }}
    task.min-drivers: {{ .Values.presto.spec.coordinator.config.taskMinDrivers }}
{{- end }}

{{- if .Values.presto.spec.config.connectors.prometheus.auth.useServiceAccountToken }}
  prometheus-connector-bearer-token-file: "/var/run/secrets/kubernetes.io/serviceaccount/token"
{{ else }}
  prometheus-connector-bearer-token-file: {{ .Values.presto.spec.config.connectors.prometheus.auth.bearerTokenFile }}
{{- end }}

  jvm.config: |
{{- if and .Values.networking.useGlobalProxyNetworking .Values.presto.spec.config.tls.enabled }}
    -Dhttps.proxyHost={{ .Values.networking.proxy.config.https_proxy.hostname }}
    -Dhttps.proxyPort={{ .Values.networking.proxy.config.https_proxy.port }}
    -Dhttps.proxyUser={{ .Values.networking.proxy.config.https_proxy.username }}
    -Dhttps.proxyPassword={{ .Values.networking.proxy.config.https_proxy.password }}
{{- else if .Values.networking.useGlobalProxyNetworking }}
    -Dhttp.proxyHost={{ .Values.networking.proxy.config.http_proxy.hostname }}
    -Dhttp.proxyPort={{ .Values.networking.proxy.config.http_proxy.port }}
    -Dhttp.proxyUser={{ .Values.networking.proxy.config.http_proxy.username }}
    -Dhttp.proxyPassword={{ .Values.networking.proxy.config.http_proxy.password }}
{{- end }}
    -server
    -XX:+UseContainerSupport
    -XX:+UseG1GC
    -XX:+UseGCOverheadLimit
{{- if .Values.presto.spec.coordinator.config.jvm.G1HeapRegionSize }}
    -XX:G1HeapRegionSize={{ .Values.presto.spec.coordinator.config.jvm.G1HeapRegionSize }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.parallelGCThreads }}
    -XX:ParallelGCThreads={{ .Values.presto.spec.coordinator.config.jvm.parallelGCThreads }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.concGCThreads }}
    -XX:ConcGCThreads={{ .Values.presto.spec.coordinator.config.jvm.concGCThreads }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.permSize }}
    -XX:PermSize={{ .Values.presto.spec.coordinator.config.jvm.permSize }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.maxGcPauseMillis }}
    -XX:MaxGCPauseMillis={{ .Values.presto.spec.coordinator.config.jvm.maxGcPauseMillis }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.initiatingHeapOccupancyPercent }}
    -XX:InitiatingHeapOccupancyPercent={{ .Values.presto.spec.coordinator.config.jvm.initiatingHeapOccupancyPercent }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.initialRAMPercentage }}
    -XX:InitialRAMPercentage={{ .Values.presto.spec.coordinator.config.jvm.initialRAMPercentage }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.maxRAMPercentage }}
    -XX:MaxRAMPercentage={{ .Values.presto.spec.coordinator.config.jvm.maxRAMPercentage }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.minRAMPercentage }}
    -XX:MinRAMPercentage={{ .Values.presto.spec.coordinator.config.jvm.minRAMPercentage }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.reservedCodeCacheSize }}
    -XX:ReservedCodeCacheSize={{ .Values.presto.spec.coordinator.config.jvm.reservedCodeCacheSize }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.maxCachedBufferSize }}
    -Djdk.nio.maxCachedBufferSize={{ .Values.presto.spec.coordinator.config.jvm.maxCachedBufferSize }}
{{- end }}
{{- if .Values.presto.spec.coordinator.config.jvm.maxDirectMemorySize }}
    -XX:MaxDirectMemorySize={{ .Values.presto.spec.coordinator.config.jvm.maxDirectMemorySize  }}
{{- end }}
{{- range .Values.presto.spec.coordinator.config.jvm.extraFlags }}
    {{ . }}
{{- end }}
    -XX:+ExplicitGCInvokesConcurrent
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:HeapDumpPath=/var/presto/logs/heap_dump.bin
    -XX:+ExitOnOutOfMemoryError
    -XX:ErrorFile=/var/presto/logs/java_error%p.log
    -verbose:gc
    -Xloggc:/var/presto/logs/gc.log
    -XX:+PrintGCDetails
    -XX:+PrintGCTimeStamps
    -XX:+PrintGCDateStamps
    -XX:+UseGCLogFileRotation
    -XX:NumberOfGCLogFiles=5
    -XX:GCLogFileSize=3M
    -javaagent:/opt/jmx_exporter/jmx_exporter.jar=8082:/opt/jmx_exporter/config/config.yml
    -Dcom.sun.management.jmxremote
    -Dcom.sun.management.jmxremote.local.only=false
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    -Dcom.sun.management.jmxremote.port=8081
    -Dcom.sun.management.jmxremote.rmi.port=8081
    -Djava.rmi.server.hostname=127.0.0.1
