@@ -14,6 +14,7 @@ import (
14
14
"github.com/go-logr/logr"
15
15
v1 "k8s.io/api/core/v1"
16
16
k8serrors "k8s.io/apimachinery/pkg/api/errors"
17
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
18
19
"k8s.io/apimachinery/pkg/runtime/schema"
19
20
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
@@ -581,20 +582,46 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateNodePool(ctx context.Cont
581
582
labels := map [string ]string {
582
583
"giantswarm.io/machine-pool" : fmt .Sprintf ("%s-%s" , cluster .Name , karpenterMachinePool .Name ),
583
584
}
584
- if len (karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels ) > 0 {
585
+ if karpenterMachinePool . Spec . NodePool != nil && len (karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels ) > 0 {
585
586
for labelKey , labelValue := range karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels {
586
587
labels [labelKey ] = labelValue
587
588
}
588
589
}
589
590
591
+ requirements := []v1alpha1.NodeSelectorRequirementWithMinValues {}
592
+ taints := []v1.Taint {}
593
+ expireAfter := v1alpha1 .MustParseNillableDuration ("720h" )
594
+ budgets := []v1alpha1.Budget {}
595
+ consolidateAfter := v1alpha1 .MustParseNillableDuration ("0s" )
596
+ consolidationPolicy := "WhenEmptyOrUnderutilized"
597
+
598
+ if karpenterMachinePool .Spec .NodePool != nil {
599
+ requirements = karpenterMachinePool .Spec .NodePool .Template .Spec .Requirements
600
+ taints = karpenterMachinePool .Spec .NodePool .Template .Spec .Taints
601
+ expireAfter = karpenterMachinePool .Spec .NodePool .Template .Spec .ExpireAfter
602
+ budgets = karpenterMachinePool .Spec .NodePool .Disruption .Budgets
603
+ consolidateAfter = karpenterMachinePool .Spec .NodePool .Disruption .ConsolidateAfter
604
+ consolidationPolicy = string (karpenterMachinePool .Spec .NodePool .Disruption .ConsolidationPolicy )
605
+ }
606
+
607
+ terminationGracePeriod := metav1.Duration {}
608
+ if karpenterMachinePool .Spec .NodePool != nil && karpenterMachinePool .Spec .NodePool .Template .Spec .TerminationGracePeriod != nil {
609
+ terminationGracePeriod = * karpenterMachinePool .Spec .NodePool .Template .Spec .TerminationGracePeriod
610
+ }
611
+
612
+ weight := int32 (1 )
613
+ if karpenterMachinePool .Spec .NodePool != nil && karpenterMachinePool .Spec .NodePool .Weight != nil {
614
+ weight = * karpenterMachinePool .Spec .NodePool .Weight
615
+ }
616
+
590
617
operation , err := controllerutil .CreateOrUpdate (ctx , workloadClusterClient , nodePool , func () error {
591
- nodePool . Object [ " spec" ] = map [string ]interface {}{
618
+ spec : = map [string ]interface {}{
592
619
"template" : map [string ]interface {}{
593
620
"metadata" : map [string ]interface {}{
594
621
"labels" : labels ,
595
622
},
596
623
"spec" : map [string ]interface {}{
597
- "taints" : karpenterMachinePool . Spec . NodePool . Template . Spec . Taints ,
624
+ "taints" : taints ,
598
625
"startupTaints" : []interface {}{
599
626
map [string ]interface {}{
600
627
"effect" : "NoExecute" ,
@@ -607,25 +634,27 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateNodePool(ctx context.Cont
607
634
"value" : "true" ,
608
635
},
609
636
},
610
- "requirements" : karpenterMachinePool . Spec . NodePool . Template . Spec . Requirements ,
637
+ "requirements" : requirements ,
611
638
"nodeClassRef" : map [string ]interface {}{
612
639
"group" : "karpenter.k8s.aws" ,
613
640
"kind" : "EC2NodeClass" ,
614
641
"name" : karpenterMachinePool .Name ,
615
642
},
616
- "terminationGracePeriodSeconds" : karpenterMachinePool . Spec . NodePool . Template . Spec . TerminationGracePeriod ,
617
- "expireAfter" : karpenterMachinePool . Spec . NodePool . Template . Spec . ExpireAfter ,
643
+ "terminationGracePeriodSeconds" : terminationGracePeriod ,
644
+ "expireAfter" : expireAfter ,
618
645
},
619
646
},
620
647
"disruption" : map [string ]interface {}{
621
- "budgets" : karpenterMachinePool . Spec . NodePool . Disruption . Budgets ,
622
- "consolidateAfter" : karpenterMachinePool . Spec . NodePool . Disruption . ConsolidateAfter ,
623
- "consolidationPolicy" : karpenterMachinePool . Spec . NodePool . Disruption . ConsolidationPolicy ,
648
+ "budgets" : budgets ,
649
+ "consolidateAfter" : consolidateAfter ,
650
+ "consolidationPolicy" : consolidationPolicy ,
624
651
},
625
652
"limits" : karpenterMachinePool .Spec .NodePool .Limits ,
626
- "weight" : karpenterMachinePool . Spec . NodePool . Weight ,
653
+ "weight" : weight ,
627
654
}
628
655
656
+ nodePool .Object ["spec" ] = spec
657
+
629
658
return nil
630
659
})
631
660
0 commit comments