apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: presto-worker
  labels:
    app: presto
    presto: worker
spec:
  replicas: {{ .Values.presto.spec.worker.replicas }}
  serviceName: presto-nodes
  selector:
    matchLabels:
      app: presto
      presto: worker
{{- if .Values.presto.spec.labels }}
{{ toYaml .Values.presto.spec.labels | indent 6 }}
{{- end }}
  template:
    metadata:
      labels:
        app: presto
        presto: worker
{{- if .Values.presto.spec.labels }}
{{ toYaml .Values.presto.spec.labels | indent 8 }}
{{- end }}
      annotations:
        presto-worker-config-hash: {{ include (print $.Template.BasePath "/presto/presto-worker-config.yaml") . | sha256sum }}
        presto-common-config-hash: {{ include (print $.Template.BasePath "/presto/presto-common-config.yaml") . | sha256sum }}
        presto-catalog-config-hash: {{ include (print $.Template.BasePath "/presto/presto-catalog-config-secret.yaml") . | sha256sum }}
        presto-jmx-config-hash: {{ include (print $.Template.BasePath "/presto/presto-jmx-config.yaml") . | sha256sum }}
{{- if .Values.presto.spec.config.aws.createSecret }}
        presto-aws-credentials-hash: {{ include (print $.Template.BasePath "/presto/presto-aws-credentials-secret.yaml") . | sha256sum }}
{{- end }}
{{- if .Values.presto.spec.config.azure.createSecret }}
        presto-azure-credentials-secret-hash: {{ include (print $.Template.BasePath "/presto/presto-azure-credentials-secret.yaml") . | sha256sum }}
{{- end }}
{{- if .Values.presto.spec.config.gcs.createSecret }}
        presto-gcs-credentials-secret-hash: {{ include (print $.Template.BasePath "/presto/presto-gcs-credentials-secret.yaml") . | sha256sum }}
{{- end }}
{{- if .Values.presto.spec.config.s3Compatible.createSecret }}
        presto-s3-compatible-credentials-secret-hash: {{ include (print $.Template.BasePath "/presto/presto-s3-compatible-credentials-secret.yaml") . | sha256sum }}
{{- end }}
{{- if .Values.presto.spec.config.s3Compatible.ca.createSecret }}
        presto-s3-compatible-ca-secret-hash: {{ include (print $.Template.BasePath "/presto/presto-s3-compatible-ca-secret.yaml") . | sha256sum }}
{{- end }}
{{- if and .Values.presto.spec.config.tls.enabled .Values.presto.spec.config.tls.createSecret }}
        presto-server-tls-hash: {{ include (print $.Template.BasePath "/presto/presto-tls-secrets.yaml") . | sha256sum }}
{{- if and .Values.presto.spec.config.auth.enabled .Values.presto.spec.config.auth.createSecret }}
        presto-client-tls-hash: {{ include (print $.Template.BasePath "/presto/presto-auth-secrets.yaml") . | sha256sum }}
{{- end }}
{{- end }}
{{- if and .Values.presto.spec.config.connectors.hive.tls.enabled .Values.presto.spec.config.connectors.hive.tls.createSecret }}
        presto-hive-metastore-tls-hash: {{ include (print $.Template.BasePath "/presto/presto-hive-metastore-tls-secrets.yaml") . | sha256sum }}
{{- end }}
{{- if .Values.presto.spec.annotations }}
{{ toYaml .Values.presto.spec.annotations | indent 8 }}
{{- end }}
    spec:
{{- if .Values.presto.spec.securityContext }}
      securityContext:
{{ toYaml .Values.presto.spec.securityContext | indent 8 }}
{{- end }}
{{- if .Values.presto.spec.worker.affinity }}
      affinity:
{{ toYaml .Values.presto.spec.worker.affinity | indent 8 }}
{{- end }}
{{- if .Values.presto.spec.worker.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.presto.spec.worker.nodeSelector | indent 8 }}
{{- end }}
{{- if .Values.presto.spec.worker.tolerations }}
      tolerations:
{{ toYaml .Values.presto.spec.worker.tolerations | indent 8 }}
{{- end }}
      initContainers:
      - name: copy-presto-config
        image: "{{ include "presto-image" . }}"
        imagePullPolicy: {{ .Values.presto.spec.image.pullPolicy }}
        command: ['/presto-common/initialize_presto.sh']
        # Copy the mounted configuration data into the presto-etc emptyDir volume so we can write to the config files
        env:
{{- if .Values.presto.spec.config.tls.enabled }}
        - name: PRESTO_SERVER_KEYFILE
          value: /presto-server/tls.key
        - name: PRESTO_SERVER_CRTFILE
          value: /presto-server/tls.crt
        - name: PRESTO_CA_CRTFILE
          value: /presto-server/ca.crt
        - name: PRESTO_KEYSTORE_PEM
          value: /opt/presto/tls/keystore.pem
        - name: PRESTO_TRUSTSTORE_PEM
          value: /opt/presto/tls/truststore.pem
{{- if .Values.presto.spec.config.auth.enabled }}
        - name: PRESTO_CLIENT_KEYFILE
          value: /presto-client/tls.key
        - name: PRESTO_CLIENT_CRTFILE
          value: /presto-client/tls.crt
        - name: PRESTO_CLIENT_CA_CRTFILE
          value: /presto-client/ca.crt
{{- end }}
{{- end }}
{{- if .Values.presto.spec.config.connectors.hive.tls.enabled }}
        - name: GHOSTUNNEL_CLIENT_CA_CRTFILE
          value: /opt/ghostunnel/tls/ca.crt
        - name: GHOSTUNNEL_CLIENT_TLS_CERT
          value: /opt/ghostunnel/tls/tls.crt
        - name: GHOSTUNNEL_CLIENT_TLS_KEY
          value: /opt/ghostunnel/tls/tls.key
        - name: GHOSTUNNEL_CLIENT_KEYSTORE
          value: /opt/ghostunnel/tls/keystore-combined.pem
{{- end }}
{{- if or .Values.presto.spec.config.azure.secretName .Values.presto.spec.config.azure.createSecret }}
        - name: AZURE_STORAGE_ACCOUNT_NAME
          valueFrom:
            secretKeyRef:
              name: "{{ .Values.presto.spec.config.azure.secretName | default "presto-azure-credentials" }}"
              key: azure-storage-account-name
        - name: AZURE_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: "{{ .Values.presto.spec.config.azure.secretName | default "presto-azure-credentials" }}"
              key: azure-secret-access-key
{{- end }}
{{- include "presto-common-env" . | indent 8 }}
        volumeMounts:
        - name: presto-etc
          mountPath: /opt/presto/presto-server/etc
        - name: presto-worker-config
          mountPath: /presto-etc
        - name: presto-catalog-config
          mountPath: /presto-etc/catalog
        - name: presto-data
          mountPath: /var/presto/data
        - name: presto-common-config
          mountPath: /presto-common
        - name: hadoop-config
          mountPath: /hadoop-config
        - name: hadoop-starting-config
          mountPath: /hadoop-starting-config
{{- if or .Values.presto.spec.config.gcs.secretName .Values.presto.spec.config.gcs.createSecret }}
        - name: gcs-json
          mountPath: /gcs-json
{{- end }}
{{- if .Values.presto.spec.config.tls.enabled }}
        - name: presto-tls
          mountPath: /opt/presto/tls
        - name: presto-server-pem
          mountPath: /presto-server
{{- if .Values.presto.spec.config.auth.enabled }}
        - name: presto-client-pem
          mountPath: /presto-client
{{- end }}
{{- end }}
{{- if .Values.presto.spec.config.connectors.hive.tls.enabled }}
        - name: hive-metastore-auth-tls
          mountPath: /opt/ghostunnel/tls
        - name: hive-metastore-auth-tls-secrets
          mountPath: /hive-metastore-auth-tls-secrets
{{- end }}
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
      containers:
      - name: presto
        image: "{{ include "presto-image" . }}"
        imagePullPolicy: {{ .Values.presto.spec.image.pullPolicy }}
        command: ['/presto-common/entrypoint.sh']
        args: ['/opt/presto/presto-server/bin/launcher', 'run']
        env:
{{- include "presto-common-env" . | indent 8 }}
        ports:
        - name: api
          containerPort: 8080
          protocol: TCP
        - name: metrics
          containerPort: 8082
          protocol: TCP
        volumeMounts:
        - name: presto-etc
          mountPath: /opt/presto/presto-server/etc
        - name: presto-common-config
          mountPath: /presto-common
        - name: presto-jmx-config
          mountPath: /opt/jmx_exporter/config
        - name: presto-data
          mountPath: /var/presto/data
        - name: presto-logs
          mountPath: /var/presto/logs
        - name: presto-service-ca-bundle
          mountPath: /var/run/configmaps/service-ca-bundle
          readOnly: true
{{- if .Values.presto.spec.config.connectors.hive.useHadoopConfig }}
        - name: hadoop-config
          mountPath: /hadoop-config
        - name: hadoop-starting-config
          mountPath: /hadoop-starting-config
{{- end }}
{{- if or .Values.presto.spec.config.gcs.secretName .Values.presto.spec.config.gcs.createSecret }}
        - name: gcs-json
          mountPath: /gcs-json
{{- end }}
{{- if or .Values.presto.spec.config.s3Compatible.ca.secretName .Values.presto.spec.config.s3Compatible.ca.createSecret }}
        - name: s3-compatible-ca
          mountPath: /s3-compatible-ca
{{- end }}
{{- if .Values.presto.spec.config.tls.enabled }}
        - name: presto-tls
          mountPath: /opt/presto/tls
{{- end }}
{{- if .Values.hive.spec.config.sharedVolume.enabled }}
        - name: hive-warehouse-data
          mountPath: {{ .Values.hive.spec.config.sharedVolume.mountPath }}
{{- end }}
        resources:
{{ toYaml .Values.presto.spec.worker.resources | indent 10 }}
{{- if .Values.presto.spec.config.connectors.hive.tls.enabled }}
      - name: ghostunnel-client
        image: "{{ include "ghostunnel-image" . }}"
        imagePullPolicy: {{ .Values.__ghostunnel.image.pullPolicy }}
        env:
        - name: GHOSTUNNEL_CLIENT_CA_CRTFILE
          value: /opt/ghostunnel/tls/ca.crt
        - name: GHOSTUNNEL_CLIENT_KEYSTORE
          value: /opt/ghostunnel/tls/keystore-combined.pem
        command: ["ghostunnel"]
        args:
        - client
        - --listen
        - localhost:9083
        - --target
        - hive-metastore:9083
        - --keystore
        -  $(GHOSTUNNEL_CLIENT_KEYSTORE)
        - --cacert
        -  $(GHOSTUNNEL_CLIENT_CA_CRTFILE)
        volumeMounts:
        - name: hive-metastore-auth-tls
          mountPath: /opt/ghostunnel/tls
{{- end }}
      volumes:
      - name: presto-worker-config
        configMap:
          name: presto-worker-config
      - name: presto-common-config
        configMap:
          name: presto-common-config
          defaultMode: 0775
      - name: presto-service-ca-bundle
        configMap:
          name: service-ca-bundle
          optional: true
      - name: presto-catalog-config
        secret:
          secretName: presto-catalog-config
      - name: presto-jmx-config
        configMap:
          name: presto-jmx-config
      - name: presto-etc
        emptyDir: {}
      - name: presto-data
        emptyDir: {}
      - name: presto-logs
        emptyDir: {}
{{- if .Values.presto.spec.config.connectors.hive.useHadoopConfig }}
      - name: hadoop-config
        emptyDir: {}
      - name: hadoop-starting-config
        secret:
          secretName: {{ .Values.presto.spec.config.connectors.hive.hadoopConfigSecretName }}
{{- end }}
{{- if or .Values.presto.spec.config.gcs.secretName .Values.presto.spec.config.gcs.createSecret }}
      - name: gcs-json
        secret:
          secretName: {{ .Values.presto.spec.config.gcs.secretName | default "presto-gcs-credentials" }}
{{- end }}
{{- if or .Values.presto.spec.config.s3Compatible.ca.secretName .Values.presto.spec.config.s3Compatible.ca.createSecret }}
      - name: s3-compatible-ca
        secret:
          secretName: {{ .Values.presto.spec.config.s3Compatible.ca.secretName | default "presto-s3-compatible-ca" }}
{{- end }}
{{- if .Values.presto.spec.config.tls.enabled }}
      - name: presto-tls
        emptyDir: {}
      - name: presto-server-pem
        secret:
          secretName: {{ .Values.presto.spec.config.tls.secretName }}
{{- if .Values.presto.spec.config.auth.enabled }}
      - name: presto-client-pem
        secret:
          secretName: {{ .Values.presto.spec.config.auth.secretName }}
{{- end }}
{{- end }}
{{- if .Values.presto.spec.config.connectors.hive.tls.enabled }}
      - name: hive-metastore-auth-tls
        emptyDir: {}
      - name: hive-metastore-auth-tls-secrets
        secret:
          secretName: {{ .Values.presto.spec.config.connectors.hive.tls.secretName }}
{{- end }}
{{- if .Values.hive.spec.config.sharedVolume.enabled }}
      - name: hive-warehouse-data
        persistentVolumeClaim:
          claimName: {{ .Values.hive.spec.config.sharedVolume.claimName }}
{{- end }}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      serviceAccount: presto
{{- if .Values.presto.spec.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.presto.spec.imagePullSecrets | indent 8 }}
{{- end }}
      terminationGracePeriodSeconds: {{ .Values.presto.spec.worker.terminationGracePeriodSeconds }}
