From 68cdbe8610a1b3108609a7e9ff645e3997d32886 Mon Sep 17 00:00:00 2001 From: Michal Jura Date: Mon, 23 Oct 2023 14:43:35 +0200 Subject: [PATCH] Check for duplicate names for NodePools --- pkg/gke/create.go | 6 ++++++ pkg/gke/create_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/pkg/gke/create.go b/pkg/gke/create.go index a25be2c6..fe3a945b 100644 --- a/pkg/gke/create.go +++ b/pkg/gke/create.go @@ -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") diff --git a/pkg/gke/create_test.go b/pkg/gke/create_test.go index 36256ad7..512c8acf 100644 --- a/pkg/gke/create_test.go +++ b/pkg/gke/create_test.go @@ -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", @@ -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() {