@@ -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"
@@ -534,20 +535,46 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateNodePool(ctx context.Cont
534
535
labels := map [string ]string {
535
536
"giantswarm.io/machine-pool" : fmt .Sprintf ("%s-%s" , cluster .Name , karpenterMachinePool .Name ),
536
537
}
537
- if len (karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels ) > 0 {
538
+ if karpenterMachinePool . Spec . NodePool != nil && len (karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels ) > 0 {
538
539
for labelKey , labelValue := range karpenterMachinePool .Spec .NodePool .Template .ObjectMeta .Labels {
539
540
labels [labelKey ] = labelValue
540
541
}
541
542
}
542
543
544
+ requirements := []v1alpha1.NodeSelectorRequirementWithMinValues {}
545
+ taints := []v1.Taint {}
546
+ expireAfter := v1alpha1 .MustParseNillableDuration ("720h" )
547
+ budgets := []v1alpha1.Budget {}
548
+ consolidateAfter := v1alpha1 .MustParseNillableDuration ("0s" )
549
+ consolidationPolicy := "WhenEmptyOrUnderutilized"
550
+
551
+ if karpenterMachinePool .Spec .NodePool != nil {
552
+ requirements = karpenterMachinePool .Spec .NodePool .Template .Spec .Requirements
553
+ taints = karpenterMachinePool .Spec .NodePool .Template .Spec .Taints
554
+ expireAfter = karpenterMachinePool .Spec .NodePool .Template .Spec .ExpireAfter
555
+ budgets = karpenterMachinePool .Spec .NodePool .Disruption .Budgets
556
+ consolidateAfter = karpenterMachinePool .Spec .NodePool .Disruption .ConsolidateAfter
557
+ consolidationPolicy = string (karpenterMachinePool .Spec .NodePool .Disruption .ConsolidationPolicy )
558
+ }
559
+
560
+ terminationGracePeriod := metav1.Duration {}
561
+ if karpenterMachinePool .Spec .NodePool != nil && karpenterMachinePool .Spec .NodePool .Template .Spec .TerminationGracePeriod != nil {
562
+ terminationGracePeriod = * karpenterMachinePool .Spec .NodePool .Template .Spec .TerminationGracePeriod
563
+ }
564
+
565
+ weight := int32 (1 )
566
+ if karpenterMachinePool .Spec .NodePool != nil && karpenterMachinePool .Spec .NodePool .Weight != nil {
567
+ weight = * karpenterMachinePool .Spec .NodePool .Weight
568
+ }
569
+
543
570
operation , err := controllerutil .CreateOrUpdate (ctx , workloadClusterClient , nodePool , func () error {
544
- nodePool . Object [ " spec" ] = map [string ]interface {}{
571
+ spec : = map [string ]interface {}{
545
572
"template" : map [string ]interface {}{
546
573
"metadata" : map [string ]interface {}{
547
574
"labels" : labels ,
548
575
},
549
576
"spec" : map [string ]interface {}{
550
- "taints" : karpenterMachinePool . Spec . NodePool . Template . Spec . Taints ,
577
+ "taints" : taints ,
551
578
"startupTaints" : []interface {}{
552
579
map [string ]interface {}{
553
580
"effect" : "NoExecute" ,
@@ -560,25 +587,27 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateNodePool(ctx context.Cont
560
587
"value" : "true" ,
561
588
},
562
589
},
563
- "requirements" : karpenterMachinePool . Spec . NodePool . Template . Spec . Requirements ,
590
+ "requirements" : requirements ,
564
591
"nodeClassRef" : map [string ]interface {}{
565
592
"group" : "karpenter.k8s.aws" ,
566
593
"kind" : "EC2NodeClass" ,
567
594
"name" : karpenterMachinePool .Name ,
568
595
},
569
- "terminationGracePeriodSeconds" : karpenterMachinePool . Spec . NodePool . Template . Spec . TerminationGracePeriod ,
570
- "expireAfter" : karpenterMachinePool . Spec . NodePool . Template . Spec . ExpireAfter ,
596
+ "terminationGracePeriodSeconds" : terminationGracePeriod ,
597
+ "expireAfter" : expireAfter ,
571
598
},
572
599
},
573
600
"disruption" : map [string ]interface {}{
574
- "budgets" : karpenterMachinePool . Spec . NodePool . Disruption . Budgets ,
575
- "consolidateAfter" : karpenterMachinePool . Spec . NodePool . Disruption . ConsolidateAfter ,
576
- "consolidationPolicy" : karpenterMachinePool . Spec . NodePool . Disruption . ConsolidationPolicy ,
601
+ "budgets" : budgets ,
602
+ "consolidateAfter" : consolidateAfter ,
603
+ "consolidationPolicy" : consolidationPolicy ,
577
604
},
578
605
"limits" : karpenterMachinePool .Spec .NodePool .Limits ,
579
- "weight" : karpenterMachinePool . Spec . NodePool . Weight ,
606
+ "weight" : weight ,
580
607
}
581
608
609
+ nodePool .Object ["spec" ] = spec
610
+
582
611
return nil
583
612
})
584
613
0 commit comments