From 29b28efffa60cbcb6828c65b3be0d473e3c666a8 Mon Sep 17 00:00:00 2001 From: Francisco Augusto Date: Tue, 12 Dec 2023 16:05:41 +0100 Subject: [PATCH] Fix gke node pools nodes per region --- cloud/scope/managedmachinepool.go | 6 +++++- cloud/services/container/nodepools/reconcile.go | 3 +-- cloud/services/shared/machinepool.go | 10 ++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cloud/scope/managedmachinepool.go b/cloud/scope/managedmachinepool.go index c66681423..4dbe929ee 100644 --- a/cloud/scope/managedmachinepool.go +++ b/cloud/scope/managedmachinepool.go @@ -159,7 +159,11 @@ func (s *ManagedMachinePoolScope) NodePoolVersion() *string { func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool, regional bool) *containerpb.NodePool { replicas := *machinePool.Spec.Replicas if regional { - replicas /= cloud.DefaultNumRegionsPerZone + if len(nodePool.Spec.NodeLocations) != 0 { + replicas /= int32(len(nodePool.Spec.NodeLocations)) + } else { + replicas /= cloud.DefaultNumRegionsPerZone + } } nodePoolName := nodePool.Spec.NodePoolName if len(nodePoolName) == 0 { diff --git a/cloud/services/container/nodepools/reconcile.go b/cloud/services/container/nodepools/reconcile.go index 60c835ca4..8a4ac2b68 100644 --- a/cloud/services/container/nodepools/reconcile.go +++ b/cloud/services/container/nodepools/reconcile.go @@ -21,7 +21,6 @@ import ( "fmt" "reflect" - "sigs.k8s.io/cluster-api-provider-gcp/cloud" "sigs.k8s.io/cluster-api-provider-gcp/util/resourceurl" "google.golang.org/api/iterator" @@ -411,7 +410,7 @@ func (s *Service) checkDiffAndPrepareUpdateSize(existingNodePool *containerpb.No replicas := *s.scope.MachinePool.Spec.Replicas if shared.IsRegional(s.scope.Region()) { - replicas /= cloud.DefaultNumRegionsPerZone + replicas /= int32(len(existingNodePool.Locations)) } if replicas != existingNodePool.InitialNodeCount { diff --git a/cloud/services/shared/machinepool.go b/cloud/services/shared/machinepool.go index e1f7c44b7..fb83b83b4 100644 --- a/cloud/services/shared/machinepool.go +++ b/cloud/services/shared/machinepool.go @@ -34,8 +34,14 @@ func ManagedMachinePoolPreflightCheck(managedPool *infrav1exp.GCPManagedMachineP } if IsRegional(location) { - if *machinePool.Spec.Replicas%cloud.DefaultNumRegionsPerZone != 0 { - return fmt.Errorf("a machine pool (%s) in a regional cluster must have replicas with a multiple of %d", machinePool.Name, cloud.DefaultNumRegionsPerZone) + var numRegionsPerZone int32 + if len(managedPool.Spec.NodeLocations) != 0 { + numRegionsPerZone = int32(len(managedPool.Spec.NodeLocations)) + } else { + numRegionsPerZone = cloud.DefaultNumRegionsPerZone + } + if *machinePool.Spec.Replicas%numRegionsPerZone != 0 { + return fmt.Errorf("a machine pool (%s) in a regional cluster with %d regions, must have replicas with a multiple of %d", machinePool.Name, numRegionsPerZone, numRegionsPerZone) } }