apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "cp-schema-registry.fullname" . }}
  labels:
    app: {{ template "cp-schema-registry.name" . }}
    chart: {{ template "cp-schema-registry.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "cp-schema-registry.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "cp-schema-registry.name" . }}
        release: {{ .Release.Name }}
      {{- if or .Values.podAnnotations .Values.prometheus.jmx.enabled }}
      annotations:
      {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
      {{- if .Values.prometheus.jmx.enabled }}
        prometheus.io/scrape: "true"
        prometheus.io/port: {{ .Values.prometheus.jmx.port | quote }}
      {{- end }}
      {{- end }}
    spec:
      containers:
        {{- if .Values.prometheus.jmx.enabled }}
        - name: prometheus-jmx-exporter
          image: "{{ .Values.prometheus.jmx.image }}:{{ .Values.prometheus.jmx.imageTag }}"
          imagePullPolicy: "{{ .Values.prometheus.jmx.imagePullPolicy }}"
          command:
          - java
          - -XX:+UnlockExperimentalVMOptions
          - -XX:+UseCGroupMemoryLimitForHeap
          - -XX:MaxRAMFraction=1
          - -XshowSettings:vm
          - -jar
          - jmx_prometheus_httpserver.jar
          - {{ .Values.prometheus.jmx.port | quote }}
          - /etc/jmx-schema-registry/jmx-schema-registry-prometheus.yml
          ports:
          - containerPort: {{ .Values.prometheus.jmx.port }}
          resources:
{{ toYaml .Values.prometheus.jmx.resources | indent 12 }}
          volumeMounts:
          - name: jmx-config
            mountPath: /etc/jmx-schema-registry
        {{- end }}
        - name: {{ template "cp-schema-registry.name" . }}-server
          image: "{{ .Values.image }}:{{ .Values.imageTag }}"
          imagePullPolicy: "{{ .Values.imagePullPolicy }}"
          ports:
            - name: schema-registry
              containerPort: {{ .Values.servicePort }}
              protocol: TCP
            {{- if .Values.prometheus.jmx.enabled }}
            - containerPort: {{ .Values.jmx.port }}
              name: jmx
            {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          env:
          - name: SCHEMA_REGISTRY_HOST_NAME
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: SCHEMA_REGISTRY_LISTENERS
            value: http://0.0.0.0:{{ .Values.servicePort }}
          - name: SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS
            value: {{ template "cp-schema-registry.kafka.bootstrapServers" . }}
          - name: SCHEMA_REGISTRY_KAFKASTORE_GROUP_ID
            value: {{ template "cp-schema-registry.groupId" . }}
          - name: SCHEMA_REGISTRY_MASTER_ELIGIBILITY
            value: "true"
          - name: SCHEMA_REGISTRY_HEAP_OPTS
            value: "{{ .Values.heapOptions }}"
          {{ range $configName, $configValue := .Values.configurationOverrides }}
          - name: SCHEMA_REGISTRY_{{ $configName | replace "." "_" | upper }}
            value: {{ $configValue | quote }}
          {{ end }}
          {{- range $key, $value := .Values.customEnv }}
          - name: {{ $key | quote }}
            value: {{ $value | quote }}
          {{- end }}
          {{- if .Values.schemaRegistryOpts }}
          - name: SCHEMA_REGISTRY_OPTS
            value: "{{ .Values.schemaRegistryOpts }}"
          {{- end }}
          {{- if .Values.jmx.port }}
          - name: JMX_PORT
            value: "{{ .Values.jmx.port }}"
          {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      volumes:
      {{- if .Values.prometheus.jmx.enabled }}
      - name: jmx-config
        configMap:
          name: {{ template "cp-schema-registry.fullname" . }}-jmx-configmap
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end }}
