{{- if .Values.proxy.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ printf "%s-proxy" (include "common.names.fullname" .) }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: proxy
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.proxy.updateStrategy }}
  strategy: {{- toYaml .Values.proxy.updateStrategy | nindent 4 }}
  {{- end }}
  replicas: {{ coalesce .Values.proxy.replicaCount .Values.proxy.replicas }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: proxy
  template:
    metadata:
      {{- if .Values.proxy.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: proxy
        {{- if .Values.proxy.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.proxy.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "wavefront.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.proxy.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.proxy.podAffinityPreset "component" "proxy" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.proxy.podAntiAffinityPreset "component" "proxy" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.proxy.nodeAffinityPreset.type "key" .Values.proxy.nodeAffinityPreset.key "values" .Values.proxy.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.proxy.schedulerName }}
      schedulerName: {{ .Values.proxy.schedulerName }}
      {{- end }}
      {{- if .Values.proxy.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.priorityClassName }}
      priorityClassName: {{ .Values.proxy.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.proxy.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.proxy.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.proxy.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.proxy.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: wavefront-proxy
          image: {{ template "wavefront.proxy.image" . }}
          imagePullPolicy: {{ .Values.proxy.image.pullPolicy }}
          {{- if .Values.proxy.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.proxy.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.proxy.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.proxy.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: WAVEFRONT_URL
              value: {{ printf "%s/api" .Values.wavefront.url }}
            - name: WAVEFRONT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ include "wavefront.token.secretName" . }}
                  key: api-token
            - name: WAVEFRONT_PROXY_ARGS
              value: {{ .Values.proxy.args }}
              {{- if .Values.proxy.tracePort }} --traceListenerPorts {{ .Values.proxy.tracePort }}{{- end -}}
              {{- if .Values.proxy.jaegerPort }} --traceJaegerListenerPorts {{ .Values.proxy.jaegerPort }}{{- end -}}
              {{- if .Values.proxy.traceJaegerHttpListenerPort }} --traceJaegerHttpListenerPorts {{ .Values.proxy.traceJaegerHttpListenerPort }}{{- end -}}
              {{- if .Values.proxy.traceJaegerGrpcListenerPort }} --traceJaegerGrpcListenerPorts {{ .Values.proxy.traceJaegerGrpcListenerPort }}{{- end -}}
              {{- if .Values.proxy.zipkinPort }} --traceZipkinListenerPorts {{ .Values.proxy.zipkinPort }}{{- end -}}
              {{- if .Values.proxy.traceSamplingRate }} --traceSamplingRate {{ .Values.proxy.traceSamplingRate }}{{- end -}}
              {{- if .Values.proxy.traceSamplingDuration }} --traceSamplingDuration {{ .Values.proxy.traceSamplingDuration }}{{- end -}}
              {{- if .Values.proxy.traceJaegerApplicationName }} --traceJaegerApplicationName {{ .Values.proxy.traceJaegerApplicationName }}{{- end -}}
              {{- if .Values.proxy.traceZipkinApplicationName }} --traceZipkinApplicationName {{ .Values.proxy.traceZipkinApplicationName }}{{- end -}}
              {{- if .Values.proxy.histogramPort }} --histogramDistListenerPorts {{ .Values.proxy.histogramPort }}{{- end -}}
              {{- if .Values.proxy.histogramMinutePort }} --histogramMinuteListenerPorts {{ .Values.proxy.histogramMinutePort }}{{- end -}}
              {{- if .Values.proxy.histogramHourPort }} --histogramHourListenerPorts {{ .Values.proxy.histogramHourPort }}{{- end -}}
              {{- if .Values.proxy.histogramDayPort }} --histogramDayListenerPorts {{ .Values.proxy.histogramDayPort }}{{- end -}}
              {{- if .Values.proxy.deltaCounterPort }} --deltaCounterPorts {{ .Values.proxy.deltaCounterPort }}{{- end -}}
              {{- if .Values.proxy.preprocessor }} --preprocessorConfigFile /etc/wavefront/wavefront-proxy/preprocessor/rules.yaml{{- end -}}
            {{- if .Values.proxy.heap }}
            - name: JAVA_HEAP_USAGE
              value: {{ .Values.proxy.heap | quote }}
            {{- end }}
            {{- if .Values.proxy.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.proxy.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.proxy.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.proxy.port }}
              protocol: TCP
            {{- if .Values.proxy.tracePort }}
            - containerPort: {{ .Values.proxy.tracePort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.jaegerPort }}
            - containerPort: {{ .Values.proxy.jaegerPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.traceJaegerHttpListenerPort }}
            - containerPort: {{ .Values.proxy.traceJaegerHttpListenerPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.traceJaegerGrpcListenerPort }}
            - containerPort: {{ .Values.proxy.traceJaegerGrpcListenerPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.zipkinPort }}
            - containerPort: {{ .Values.proxy.zipkinPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.histogramPort }}
            - containerPort: {{ .Values.proxy.histogramPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.histogramMinutePort }}
            - containerPort: {{ .Values.proxy.histogramMinutePort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.histogramHourPort }}
            - containerPort: {{ .Values.proxy.histogramHourPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.histogramDayPort }}
            - containerPort: {{ .Values.proxy.histogramDayPort }}
              protocol: TCP
            {{- end }}
            {{- if .Values.proxy.deltaCounterPort }}
            - containerPort: {{ .Values.proxy.deltaCounterPort }}
              protocol: TCP
            {{- end }}
          {{- if .Values.proxy.resources }}
          resources: {{- toYaml .Values.proxy.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.proxy.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: {{ .Values.proxy.port }}
            initialDelaySeconds: {{ .Values.proxy.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.proxy.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.proxy.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.proxy.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.proxy.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.proxy.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: {{ .Values.proxy.port }}
            initialDelaySeconds: {{ .Values.proxy.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.proxy.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.proxy.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.proxy.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.proxy.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.proxy.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.startupProbe.enabled }}
          startupProbe:
            tcpSocket:
              port: {{ .Values.proxy.port }}
            initialDelaySeconds: {{ .Values.proxy.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.proxy.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.proxy.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.proxy.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.proxy.startupProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            {{- if .Values.proxy.preprocessor }}
            - name: preprocessor
              mountPath: /etc/wavefront/wavefront-proxy/preprocessor
            {{- end }}
            {{- if .Values.proxy.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.proxy.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.proxy.preprocessor }}
        - name: preprocessor
          configMap:
            name: {{ include "wavefront.proxy.configmapName" . }}
        {{- end }}
        {{- if .Values.proxy.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
