# grafana_charts_tokengen-job.yaml
{{ if .Values.tokengenJob.enable }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "enterprise-metrics.fullname" . }}-tokengen
  labels:
    app: {{ template "enterprise-metrics.name" . }}-tokengen
    chart: {{ template "enterprise-metrics.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    {{- if .Values.tokengenJob.annotations }}
    {{- toYaml .Values.tokengenJob.annotations | nindent 4 }}
    {{- end }}
    "helm.sh/hook": post-install
spec:
  backoffLimit: 6
  completions: 1
  parallelism: 1
  selector:
  template:
    metadata:
      labels:
        app: {{ template "enterprise-metrics.name" . }}-tokengen
        # The name label is important for cortex-mixin compatibility which expects certain names for services.
        name: tokengen
        target: tokengen
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "enterprise-metrics.serviceAccountName" . }}
      {{- if .Values.tokengenJob.priorityClassName }}
      priorityClassName: {{ .Values.tokengenJob.priorityClassName }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.tokengenJob.securityContext | nindent 8 }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      {{- with .Values.tokengenJob.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
        {{- toYaml .Values.tokengenJob.initContainers | nindent 8 }}
      containers:
        - name: tokengen
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - "-target=tokengen"
            - "-config.file=/etc/enterprise-metrics/enterprise-metrics.yaml"
            {{- if .Values.minio.enabled }}
            - -admin.client.backend-type=s3
            - -admin.client.s3.endpoint={{ .Release.Name }}-minio.{{ .Release.Namespace }}.svc:9000
            - -admin.client.s3.bucket-name=enterprise-metrics-admin
            - -admin.client.s3.access-key-id=enterprise-metrics
            - -admin.client.s3.secret-access-key=supersecret
            - -admin.client.s3.insecure=true
            {{- end }}
            {{- range $key, $value := .Values.tokengenJob.extraArgs }}
            - "-{{ $key }}={{ $value }}"
            {{- end }}
          volumeMounts:
            {{- if .Values.tokengenJob.extraVolumeMounts }}
              {{ toYaml .Values.tokengenJob.extraVolumeMounts | nindent 12 }}
            {{- end }}
            - name: config
              mountPath: /etc/enterprise-metrics
            - name: license
              mountPath: /license
          env:
            {{- if .Values.tokengenJob.env }}
              {{ toYaml .Values.tokengenJob.env | nindent 12 }}
            {{- end }}
      restartPolicy: OnFailure
      volumes:
        - name: config
          secret:
          {{- if .Values.useExternalConfig }}
            secretName: {{ .Values.externalConfigSecretName }}
          {{- else }}
            secretName: {{ template "enterprise-metrics.fullname" . }}
          {{- end }}
        {{- if .Values.tokengenJob.extraVolumes }}
        {{ toYaml .Values.tokengenJob.extraVolumes | nindent 8 }}
        {{- end }}
        - name: license
          secret:
            secretName: {{ .Values.license.secretName }}
        - name: storage
          emptyDir: {}
{{- end }}