{{- if .Values.customResources.enabled }}
{{- range $pool := .Values.customResources.nodePools }}
{{- if $pool.enabled }}
---
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
  name: {{ $pool.name }}
  labels:
    {{- include "karpenter.labels" $ | nindent 4 }}
    app.kubernetes.io/component: nodepool
    karpenter.sh/nodepool: {{ $pool.name | quote }}
    {{- if $.Values.customResources.mode }}
    karpenter-ibm.sh/mode: {{ $.Values.customResources.mode }}
    {{- end }}
    {{- if $pool.labels }}
    {{- range $key, $value := $pool.labels }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
    {{- end }}
spec:
  # Template defines the node configuration
  template:
    metadata:
      labels:
        # Standard Karpenter labels
        karpenter.sh/nodepool: {{ $pool.name | quote }}
        {{- if $pool.labels }}
        {{- range $key, $value := $pool.labels }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
        {{- end }}
        # Global labels from values
        {{- if $.Values.customResources.global.labels }}
        {{- range $key, $value := $.Values.customResources.global.labels }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
        {{- end }}

      {{- if or $pool.taints $.Values.customResources.global.taints }}
      # Taints for this nodepool
      taints:
        {{- if $pool.taints }}
        {{- range $taint := $pool.taints }}
        - key: {{ $taint.key | quote }}
          value: {{ $taint.value | quote }}
          effect: {{ $taint.effect | quote }}
        {{- end }}
        {{- end }}
        {{- if $.Values.customResources.global.taints }}
        {{- range $taint := $.Values.customResources.global.taints }}
        - key: {{ $taint.key | quote }}
          value: {{ $taint.value | quote }}
          effect: {{ $taint.effect | quote }}
        {{- end }}
        {{- end }}
      {{- end }}

    spec:
      # Node requirements (constraints and preferences)
      requirements:
        # Instance types
        - key: "node.kubernetes.io/instance-type"
          operator: In
          values:
            {{- if $pool.requirements.instanceTypes }}
            {{- range $instanceType := $pool.requirements.instanceTypes }}
            - {{ $instanceType | quote }}
            {{- end }}
            {{- else }}
            {{- range $instanceType := $.Values.customResources.global.instanceTypes }}
            - {{ $instanceType | quote }}
            {{- end }}
            {{- end }}

        # Capacity type (on-demand vs spot)
        - key: "karpenter.sh/capacity-type"
          operator: In
          values:
            - {{ $pool.requirements.capacityType | default $.Values.customResources.global.capacityType | quote }}

        # Architecture
        - key: "kubernetes.io/arch"
          operator: In
          values:
            - {{ $pool.requirements.architecture | default $.Values.customResources.global.architecture | quote }}

        {{- if $pool.requirements.zones }}
        # Availability zones
        - key: "topology.kubernetes.io/zone"
          operator: In
          values:
            {{- range $zone := $pool.requirements.zones }}
            - {{ $zone | quote }}
            {{- end }}
        {{- end }}

      # Node class reference
      nodeClassRef:
        group: karpenter-ibm.sh
        kind: IBMNodeClass
        name: {{ $pool.nodeClassRef | default $.Values.customResources.nodeClass.name | default "default" }}

  # Disruption settings
  disruption:
    {{- if $pool.disruption }}
    consolidationPolicy: {{ $pool.disruption.consolidationPolicy | default $.Values.customResources.global.disruption.consolidationPolicy }}
    consolidateAfter: {{ $pool.disruption.consolidateAfter | default $.Values.customResources.global.disruption.consolidateAfter }}
    expireAfter: {{ $pool.disruption.expireAfter | default $.Values.customResources.global.disruption.expireAfter }}
    {{- else }}
    consolidationPolicy: {{ $.Values.customResources.global.disruption.consolidationPolicy }}
    consolidateAfter: {{ $.Values.customResources.global.disruption.consolidateAfter }}
    expireAfter: {{ $.Values.customResources.global.disruption.expireAfter }}
    {{- end }}

  # Resource limits
  limits:
    {{- if $pool.limits }}
    {{- if $pool.limits.cpu }}
    cpu: {{ $pool.limits.cpu }}
    {{- else }}
    cpu: {{ $.Values.customResources.global.limits.cpu }}
    {{- end }}
    {{- if $pool.limits.memory }}
    memory: {{ $pool.limits.memory }}
    {{- else }}
    memory: {{ $.Values.customResources.global.limits.memory }}
    {{- end }}
    {{- else }}
    cpu: {{ $.Values.customResources.global.limits.cpu }}
    memory: {{ $.Values.customResources.global.limits.memory }}
    {{- end }}
{{- end }}
{{- end }}
{{- end }}
