# helm_charts_exporter-deployment.yaml
{{- if .Values.exporter.enabled }}
---

apiVersion: "v1"
kind: "Service"
metadata:
  name: "{{ include "solr.exporter-name" . }}"
  labels:
{{ include "solr.common.labels" . | indent 4 }}
    app.kubernetes.io/component: "exporter"
  annotations:
{{ toYaml .Values.exporter.service.annotations | indent 4}}
spec:
  type: "{{ .Values.exporter.service.type }}"
  ports:
    - port: {{ .Values.exporter.port }}
      name: "solr-client"
  selector:
    app.kubernetes.io/name: "{{ include "solr.name" . }}"
    app.kubernetes.io/instance: "{{ .Release.Name }}"
    app.kubernetes.io/component: "exporter"


---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "solr.exporter-name" . }}
  labels:
{{ include "solr.common.labels" . | indent 4 }}
    app.kubernetes.io/component: "exporter"
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: "{{ include "solr.name" . }}"
      app.kubernetes.io/instance: "{{ .Release.Name }}"
      app.kubernetes.io/component: "exporter"
  replicas: 1
  strategy:
    {{ toYaml .Values.exporter.updateStrategy | indent 4}}
  template:
    metadata:
      labels:
{{ include "solr.common.labels" . | indent 8 }}
        app.kubernetes.io/component: "exporter"
      annotations:
{{ toYaml .Values.exporter.podAnnotations | indent 8 }}
    spec:
{{- include "solr.imagePullSecrets" . | indent 6 }}    
      affinity:
{{ tpl (toYaml .Values.affinity) .  | indent 8 }}
      containers:
        - name: exporter
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          resources:
{{ toYaml .Values.exporter.resources | indent 12 }}
          ports:
            - containerPort: {{ .Values.port }}
              name: solr-client
          command:
            - "/opt/solr/contrib/prometheus-exporter/bin/solr-exporter"
            - "-p"
            - "{{ .Values.exporter.port }}"
            - "-z"
            - "{{ include "solr.zookeeper-service-name" . }}:2181"
            - "-n"
            - "{{ .Values.exporter.threads }}"
            - "-f"
            - "{{ .Values.exporter.configFile }}"
          livenessProbe:
            initialDelaySeconds: {{ .Values.exporter.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.exporter.livenessProbe.periodSeconds }}
            httpGet:
              path: "/metrics"
              port: {{ .Values.exporter.port }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.exporter.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.exporter.readinessProbe.periodSeconds }}
            httpGet:
              path: "/metrics"
              port: {{ .Values.exporter.port }}
      initContainers:
        - name: solr-init
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - 'sh'
            - '-c'
            - |
              {{- if .Values.tls.enabled }}
              PROTOCOL="https://"
              {{ else }}
              PROTOCOL="http://"
              {{- end }}
              COUNTER=0;
              while [  $COUNTER -lt 30 ]; do
                curl -k -s --connect-timeout 10 "${PROTOCOL}{{ include "solr.service-name" . }}:{{ .Values.port }}/solr/admin/info/system" && exit 0
                sleep 2
              done;
              echo "Did NOT see a Running Solr instance after 60 secs!";
              exit 1;
{{ end }}