{{- if .Values.custom.configuration.enabled }}
apiVersion: {{ printf "%s/%s" .Values.crds.group .Values.crds.version }}
kind: Configuration
metadata:
  name: {{ .Values.custom.configuration.name }}
spec:
  discoveryHandler: 
    name: {{ required "A custom.configuration.discoveryHandlerName is required." .Values.custom.configuration.discoveryHandlerName }}
    discoveryDetails: {{ .Values.custom.configuration.discoveryDetails | quote }}
  {{- if or .Values.custom.configuration.brokerPod.image.repository .Values.custom.configuration.brokerJob.image.repository }}
  {{- /* Only add brokerSpec if a broker image is provided */}}
  brokerSpec:
    {{- if .Values.custom.configuration.brokerPod.image.repository }}
    brokerPodSpec:
      containers:
      - name: {{ .Values.custom.configuration.name }}-broker
        image: {{ printf "%s:%s" .Values.custom.configuration.brokerPod.image.repository .Values.custom.configuration.brokerPod.image.tag | quote }}
        {{- with .Values.custom.configuration.pullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        {{- if .Values.custom.configuration.brokerPod.env }}
        env:
          {{- range $key, $val := .Values.custom.configuration.brokerPod.env }}
          - name: {{ $key }}
            value: {{ $val | quote }}
          {{- end }}
        {{- end }}
        {{- if .Values.custom.configuration.brokerPod.envFrom }}
        envFrom:
        {{- range $val := .Values.custom.configuration.brokerPod.envFrom.secretRef }}
        - secretRef:
            name: {{ $val | quote }}
        {{- end }}
        {{- range $val := .Values.custom.configuration.brokerPod.envFrom.configMapRef }}
        - configMapRef:
            name: {{ $val | quote }}
        {{- end }}
        {{- end }}
        resources:
          requests:
            {{`"{{PLACEHOLDER}}"`}} : "1"
            memory: {{ .Values.custom.configuration.brokerPod.resources.memoryRequest }}
            cpu: {{ .Values.custom.configuration.brokerPod.resources.cpuRequest }}
          limits:
            {{`"{{PLACEHOLDER}}"`}} : "1"
            memory: {{ .Values.custom.configuration.brokerPod.resources.memoryLimit }}
            cpu: {{ .Values.custom.configuration.brokerPod.resources.cpuLimit }}
        {{- with .Values.custom.configuration.brokerPod.volumeMounts}}
        volumeMounts:
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.custom.configuration.brokerPod.volumes}}
      volumes:
        {{- toYaml . | nindent 6 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 6 }}
      {{- end }}
    {{- else }}
    brokerJobSpec:
      template:
        spec:
          containers:
          - name: {{ .Values.custom.configuration.name }}-broker
            image: {{ printf "%s:%s" .Values.custom.configuration.brokerJob.image.repository .Values.custom.configuration.brokerPod.image.tag | quote }}
            {{- if .Values.custom.configuration.brokerJob.command }}
            command: 
              {{- toYaml .Values.custom.configuration.brokerJob.command | nindent 14 }}
            {{- end }}
            {{- with .Values.custom.configuration.pullPolicy }}
            imagePullPolicy: {{ . }}
            {{- end }}
            {{- if .Values.custom.configuration.brokerJob.env }}
            env:
              {{- range $key, $val := .Values.custom.configuration.brokerJob.env }}
              - name: {{ $key }}
                value: {{ $val | quote }}
              {{- end }}
            {{- end }}
            {{- if .Values.custom.configuration.brokerJob.envFrom }}
            envFrom:
            {{- range $val := .Values.custom.configuration.brokerJob.envFrom.secretRef }}
            - secretRef:
                name: {{ $val | quote }}
            {{- end }}
            {{- range $val := .Values.custom.configuration.brokerJob.envFrom.configMapRef }}
            - configMapRef:
                name: {{ $val | quote }}
            {{- end }}
            {{- end }}
            resources:
              requests:
                {{`"{{PLACEHOLDER}}"`}} : "1"
                memory: {{ .Values.custom.configuration.brokerJob.resources.memoryRequest }}
                cpu: {{ .Values.custom.configuration.brokerJob.resources.cpuRequest }}
              limits:
                {{`"{{PLACEHOLDER}}"`}} : "1"
                memory: {{ .Values.custom.configuration.brokerJob.resources.memoryLimit }}
                cpu: {{ .Values.custom.configuration.brokerJob.resources.cpuLimit }}
            {{- with .Values.custom.configuration.brokerJob.volumeMounts}}
            volumeMounts:
              {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with .Values.custom.configuration.brokerJob.volumes}}
          volumes:
            {{- toYaml . | nindent 10 }}
          {{- end }}
          restartPolicy: {{ .Values.custom.configuration.brokerJob.restartPolicy }}
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 10 }}
          {{- end }}
      backoffLimit: {{ .Values.custom.configuration.brokerJob.backoffLimit }}
      parallelism: {{ .Values.custom.configuration.brokerJob.parallelism }}
      completions: {{ .Values.custom.configuration.brokerJob.completions }}
    {{- end }}
  {{- end }}
  {{- if .Values.custom.configuration.brokerPod.image.repository }}
  {{- if .Values.custom.configuration.createInstanceServices }}
  instanceServiceSpec:
    type: {{ .Values.custom.configuration.instanceService.type }}
    ports:
    - name: {{ .Values.custom.configuration.instanceService.name }}
      port: {{ .Values.custom.configuration.instanceService.port }}
      protocol: {{ .Values.custom.configuration.instanceService.protocol }}
      targetPort: {{ .Values.custom.configuration.instanceService.targetPort }}
  {{- end }}
  {{- if .Values.custom.configuration.createConfigurationService }}
  configurationServiceSpec:
    type: {{ .Values.custom.configuration.configurationService.type }}
    ports:
    - name: {{ .Values.custom.configuration.configurationService.name }}
      port: {{ .Values.custom.configuration.configurationService.port }}
      protocol: {{ .Values.custom.configuration.configurationService.protocol }}
      targetPort: {{ .Values.custom.configuration.configurationService.port }}
  {{- end }}
  {{- end }}
  {{- if .Values.custom.configuration.brokerProperties }}
  brokerProperties:
  {{- range $key, $val := .Values.custom.configuration.brokerProperties }}
  {{- $key | nindent 4 }}: {{ $val | quote }}
  {{- end }}
  {{- else }}
  brokerProperties: {}
  {{- end }}
  capacity: {{ .Values.custom.configuration.capacity }}
{{- end }}