Skip to content

Commit

Permalink
Check for duplicate names for NodePools
Browse files Browse the repository at this point in the history
  • Loading branch information
mjura committed Oct 24, 2023
1 parent 25ff84d commit 68cdbe8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/gke/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ func validateCreateRequest(ctx context.Context, gkeClient services.GKEClusterSer
return fmt.Errorf("cluster name is required")
}

nodeP := map[string]bool{}
for _, np := range config.Spec.NodePools {
if nodeP[*np.Name] {
return fmt.Errorf("NodePool names must be unique within the [%s] cluster to avoid duplication", config.Spec.ClusterName)
}
nodeP[*np.Name] = true

if np.Autoscaling != nil && np.Autoscaling.Enabled {
if np.Autoscaling.MinNodeCount < 1 || np.Autoscaling.MaxNodeCount < np.Autoscaling.MinNodeCount {
return fmt.Errorf("minNodeCount in the NodePool must be >= 1 and <= maxNodeCount")
Expand Down
41 changes: 41 additions & 0 deletions pkg/gke/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var _ = Describe("CreateCluster", func() {
subnetworkName = "test-subnetwork"
emptyString = ""
boolTrue = true
nodePoolName = "test-node-pool"
initialNodeCount = int64(3)
maxPodsConstraint = int64(110)
config = &gkev1.GKEClusterConfig{
Spec: gkev1.GKEClusterConfigSpec{
Region: "test-region",
Expand Down Expand Up @@ -115,6 +118,44 @@ var _ = Describe("CreateCluster", func() {
Expect(err).To(HaveOccurred())
})

It("should fail to create cluster with duplicated nodepool names", func() {
config.Spec.NodePools = []gkev1.GKENodePoolConfig{
{
Name: &nodePoolName,
InitialNodeCount: &initialNodeCount,
Version: &k8sVersion,
MaxPodsConstraint: &maxPodsConstraint,
Config: &gkev1.GKENodeConfig{},
Autoscaling: &gkev1.GKENodePoolAutoscaling{
Enabled: true,
MinNodeCount: 3,
MaxNodeCount: 5,
},
Management: &gkev1.GKENodePoolManagement{
AutoRepair: true,
AutoUpgrade: true,
},
},
{
Name: &nodePoolName,
InitialNodeCount: &initialNodeCount,
Version: &k8sVersion,
MaxPodsConstraint: &maxPodsConstraint,
Config: &gkev1.GKENodeConfig{},
Autoscaling: &gkev1.GKENodePoolAutoscaling{
Enabled: true,
MinNodeCount: 3,
MaxNodeCount: 5,
},
Management: &gkev1.GKENodePoolManagement{
AutoRepair: true,
AutoUpgrade: true,
},
},
}
err := Create(ctx, clusterServiceMock, config)
Expect(err).To(HaveOccurred())
})
})

var _ = Describe("CreateNodePool", func() {
Expand Down

0 comments on commit 68cdbe8

Please sign in to comment.