apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Values.clusterName }}-importer
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "chart.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Values.clusterName }}
    app.kubernetes.io/component: importer
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+"  "_" }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "chart.name" . }}
      app.kubernetes.io/instance: {{ .Values.clusterName }}
      app.kubernetes.io/component: importer
  serviceName: {{ .Values.clusterName }}-importer
  replicas: 1
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/path: "/metrics"
        prometheus.io/port: "8286"
      labels:
        app.kubernetes.io/name: {{ template "chart.name" . }}
        app.kubernetes.io/instance: {{ .Values.clusterName }}
        app.kubernetes.io/component: importer
    spec:
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 6 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 6 }}
    {{- end }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 6 }}
    {{- end }}
      containers:
      - name: importer
        image: {{ .Values.image }}
        imagePullPolicy: {{ .Values.imagePullPolicy | default "IfNotPresent"}}
        command:
        - /tikv-importer
        # tikv-importer does not support domain name: https://github.com/tikv/importer/issues/16
        # - --addr=${MY_POD_NAME}.tikv-importer:8287
        - --addr=$(MY_POD_IP):8287
        - --config=/etc/tikv-importer/tikv-importer.toml
        - --import-dir=/var/lib/tikv-importer
        env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        - name: TZ
          value: {{ .Values.timezone | default "UTC" }}
        volumeMounts:
        - name: data
          mountPath: /var/lib/tikv-importer
        - name: config
          mountPath: /etc/tikv-importer
        {{- if and .Values.tlsCluster .Values.tlsCluster.enabled }}
        - name: importer-tls
          mountPath: /var/lib/importer-tls
          readOnly: true
        {{- end }}
        {{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        {{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ .Values.clusterName }}-importer-{{ template "importer-configmap.data-digest" . }}
          items:
          - key: config-file
            path: tikv-importer.toml
      {{- if and .Values.tlsCluster .Values.tlsCluster.enabled }}
      - name: importer-tls
        secret:
          secretName: {{ .Values.clusterName }}-importer-cluster-secret
      {{- end }}
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: {{ .Values.storageClassName }}
      resources:
        requests:
          storage: {{ .Values.storage }}
