{{- if .Values.startupapicheck.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "startupapicheck.fullname" . }}
  namespace: {{ include "cert-manager.namespace" . }}
  labels:
    app: {{ include "startupapicheck.name" . }}
    app.kubernetes.io/name: {{ include "startupapicheck.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: "startupapicheck"
    {{- include "labels" . | nindent 4 }}
  {{- with .Values.startupapicheck.jobAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  backoffLimit: {{ .Values.startupapicheck.backoffLimit }}
  {{- if hasKey .Values.startupapicheck "ttlSecondsAfterFinished" }}
  ttlSecondsAfterFinished: {{ .Values.startupapicheck.ttlSecondsAfterFinished }}
  {{- end }}
  template:
    metadata:
      labels:
        app: {{ include "startupapicheck.name" . }}
        app.kubernetes.io/name: {{ include "startupapicheck.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: "startupapicheck"
        {{- include "labels" . | nindent 8 }}
        {{- with .Values.startupapicheck.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.startupapicheck.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      restartPolicy: OnFailure
      serviceAccountName: {{ template "startupapicheck.serviceAccountName" . }}
      {{- if hasKey .Values.startupapicheck "automountServiceAccountToken" }}
      automountServiceAccountToken: {{ .Values.startupapicheck.automountServiceAccountToken }}
      {{- end }}
      enableServiceLinks: {{ .Values.startupapicheck.enableServiceLinks }}
      {{- with .Values.global.priorityClassName }}
      priorityClassName: {{ . | quote }}
      {{- end }}
      {{- if (hasKey .Values.global "hostUsers") }}
      hostUsers: {{ .Values.global.hostUsers }}       
      {{- end }}
      {{- with .Values.startupapicheck.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}-startupapicheck
          image: "{{ template "cert-manager.image" (tuple .Values.startupapicheck.image .Values.imageRegistry .Values.imageNamespace (printf ":%s" .Chart.AppVersion)) }}"
          imagePullPolicy: {{ .Values.startupapicheck.image.pullPolicy }}
          args:
          - check
          - api
          - --wait={{ .Values.startupapicheck.timeout }}
          {{- with .Values.startupapicheck.extraArgs }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
          {{- with .Values.startupapicheck.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          env:
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          {{- with .Values.startupapicheck.extraEnv }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
          {{- with .Values.startupapicheck.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.startupapicheck.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- if .Values.startupapicheck.runtimeClassName }}
      runtimeClassName: {{ .Values.startupapicheck.runtimeClassName | quote }}
      {{- else if .Values.global.runtimeClassName }}
      runtimeClassName: {{ .Values.global.runtimeClassName | quote }}
      {{- end }}
      {{- $nodeSelector := .Values.global.nodeSelector | default dict }}
      {{- $nodeSelector = merge $nodeSelector (.Values.startupapicheck.nodeSelector | default dict) }}
      {{- with $nodeSelector }}
      nodeSelector:
        {{- range $key, $value := . }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
      {{- end }}
      {{- with .Values.startupapicheck.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.startupapicheck.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.startupapicheck.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
