{{ if .Values.jenkins.enabled }}
apiVersion: {{ .Values.jenkins.apiVersion }}
kind: Jenkins
metadata:
  name: {{ .Values.jenkins.name }}
  namespace: {{ .Values.jenkins.namespace }}
  {{- with .Values.jenkins.labels }}
  labels: {{ toYaml . | nindent 4 }}
  {{- end }}
spec:
  configurationAsCode:
    {{- if .Values.jenkins.configuration.configurationAsCode }}
    configurations:
      {{ range .Values.jenkins.configuration.configurationAsCode }}
      - name: {{ .configMapName }}
      {{- end }}
    secret:
      {{- if .Values.jenkins.configuration.secretRefName }}
      name: {{ .Values.jenkins.configuration.secretRefName }}
      {{- else if .Values.jenkins.configuration.secretData }}
      name: jenkins-{{ .Values.jenkins.name }}
      {{- else }}
      name: ""
      {{- end }}
    {{- else }}
    configurations: []
    secret:
      name: ""
    {{- end }}
  groovyScripts:
    {{- if .Values.jenkins.configuration.groovyScripts }}
    configurations:
      {{- range .Values.jenkins.configuration.groovyScripts }}
      - name: {{ .configMapName }}
      {{- end }}
    secret:
      {{- if .Values.jenkins.configuration.secretRefName }}
      name: {{ .Values.jenkins.configuration.secretRefName }}
      {{- else if .Values.jenkins.configuration.secretData }}
      name: jenkins-{{ .Values.jenkins.name }}
      {{- else }}
      name: ""
      {{- end }}
    {{- else }}
    configurations: []
    secret:
      name: ""
    {{- end }}
  jenkinsAPISettings:
    authorizationStrategy: {{ .Values.jenkins.authorizationStrategy }}
  {{- if .Values.jenkins.backup.enabled }}
  backup:
    containerName: {{ .Values.jenkins.backup.containerName }}
    action:
      exec:
        {{- with .Values.jenkins.backup.backupCommand }}
        command: {{ toYaml . | nindent 8 }}
        {{- end }}
    interval: {{ .Values.jenkins.backup.interval }}
    makeBackupBeforePodDeletion: {{ .Values.jenkins.backup.makeBackupBeforePodDeletion }}
  restore:
    containerName: {{ .Values.jenkins.backup.containerName }}
    action:
      exec:
        {{- with .Values.jenkins.backup.restoreCommand }}
        command: {{ toYaml . | nindent 8 }}
        {{- end }}
    {{- if .Values.jenkins.backup.getLatestAction }}
    getLatestAction:
      exec:
        {{- with .Values.jenkins.backup.getLatestAction }}
        command: {{ toYaml . | nindent 8 }}
        {{- end }}
    {{- end }}
    {{- if .Values.jenkins.backup.recoveryOnce }}
    recoveryOnce: {{ .Values.jenkins.backup.recoveryOnce }}
    {{- end }}
  {{- end }}
  {{- with .Values.jenkins.service }}
  service: {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.jenkins.slaveService }}
  slaveService: {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.jenkins.notifications }}
  notifications: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.jenkins.serviceAccount }}
  serviceAccount: {{ toYaml . | nindent 4 }}
  {{- end }}
  master:
    {{- with .Values.jenkins.labels }}
    labels: {{ toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.jenkins.nodeSelector }}
    nodeSelector: {{ toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.jenkins.tolerations }}
    tolerations: {{ toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.jenkins.annotations }}
    annotations: {{ toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.jenkins.basePlugins }}
    basePlugins: {{ toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.jenkins.plugins }}
    plugins: {{ toYaml . | nindent 4 }}
    {{- end }}
    latestPlugins: {{ .Values.jenkins.latestPlugins }}
    {{- if .Values.jenkins.priorityClassName }}
    priorityClassName: {{ .Values.jenkins.priorityClassName }}
    {{- end }}
    disableCSRFProtection: {{ .Values.jenkins.disableCSRFProtection }}
    {{- with .Values.jenkins.hostAliases }}
    hostAliases: {{ toYaml . | nindent 4 }}
    {{- end }}
    skipPlugins: {{ .Values.jenkins.skipPlugins }}
    {{- if .Values.jenkins.terminationGracePeriodSeconds }}
    terminationGracePeriodSeconds: {{ .Values.jenkins.terminationGracePeriodSeconds }}
    {{- end }}
    containers:
      - name:  jenkins-master
        image: {{ .Values.jenkins.image }}
        imagePullPolicy: {{ .Values.jenkins.imagePullPolicy }}
        {{- with .Values.jenkins.livenessProbe }}
        livenessProbe: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.jenkins.readinessProbe }}
        readinessProbe: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.jenkins.resources }}
        resources: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.jenkins.env }}
        env: {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.jenkins.lifecycle}}
        lifecycle: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.jenkins.volumeMounts }}
        volumeMounts: {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- if .Values.jenkins.backup.enabled }}
      - name: {{ .Values.jenkins.backup.containerName }}
        image: {{ .Values.jenkins.backup.image }}
        imagePullPolicy: {{ .Values.jenkins.imagePullPolicy }}
        {{- with .Values.jenkins.backup.resources }}
        resources: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.jenkins.backup.env }}
        env: {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.jenkins.backup.volumeMounts }}
        volumeMounts: {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
    {{- with .Values.jenkins.volumes }}
    volumes: {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.jenkins.imagePullSecrets }}
    imagePullSecrets: {{ toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.jenkins.securityContext}}
    securityContext:
    {{- toYaml . | nindent 6 }}
    {{- end }}
  validateSecurityWarnings: {{ .Values.jenkins.validateSecurityWarnings }}
  {{- with .Values.jenkins.seedJobs }}
  seedJobs: {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if .Values.jenkins.seedJobAgentImage }}
  seedJobAgentImage: {{ .Values.jenkins.seedJobAgentImage }}
  {{- end }}
{{- end }}
