Skip to content

Commit 021d0d7

Browse files
committed
PLT-1887: Fixed eks dynamic cluster provisioning, removed cp entry in api call
1 parent e4d4455 commit 021d0d7

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

spectrocloud/resource_cluster_eks.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,18 @@ func toEksCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro
830830
cluster.Spec.CloudConfig.EndpointAccess = access
831831

832832
machinePoolConfigs := make([]*models.V1EksMachinePoolConfigEntity, 0)
833-
// Following same logic as UI for setting up control plane for managed cluster
834-
cpPool := map[string]interface{}{
835-
"control_plane": true,
836-
"name": "cp-pool",
837-
"az_subnets": cloudConfig["az_subnets"],
838-
"capacity_type": "spot",
839-
"count": 0,
833+
// Following same logic as UI for setting up control plane for static cluster
834+
// Only add cp-pool for dynamic cluster provisioning when az_subnets is not empty and has more than one element
835+
if cloudConfig["az_subnets"] != nil && len(cloudConfig["az_subnets"].(map[string]interface{})) > 0 {
836+
cpPool := map[string]interface{}{
837+
"control_plane": true,
838+
"name": "cp-pool",
839+
"az_subnets": cloudConfig["az_subnets"],
840+
"capacity_type": "spot",
841+
"count": 0,
842+
}
843+
machinePoolConfigs = append(machinePoolConfigs, toMachinePoolEks(cpPool))
840844
}
841-
machinePoolConfigs = append(machinePoolConfigs, toMachinePoolEks(cpPool))
842845
for _, machinePool := range d.Get("machine_pool").([]interface{}) {
843846
mp := toMachinePoolEks(machinePool)
844847
machinePoolConfigs = append(machinePoolConfigs, mp)

spectrocloud/resource_cluster_eks_expand_test.go

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/google/go-cmp/cmp"
88
"github.com/spectrocloud/palette-sdk-go/api/models"
9+
"github.com/spectrocloud/palette-sdk-go/client"
910
"github.com/stretchr/testify/assert"
1011

1112
"github.com/spectrocloud/terraform-provider-spectrocloud/types"
@@ -94,18 +95,73 @@ func TestToEksCluster(t *testing.T) {
9495
},
9596
})
9697

97-
//client := &client.V1Client{}
98-
//
99-
//cluster, err := toEksCluster(client, d)
100-
//
101-
//assert.NoError(t, err, "Expected no error from toEksCluster")
102-
//assert.Equal(t, "test-cluster", cluster.Metadata.Name, "Unexpected cluster name")
103-
//
104-
//assert.NotNil(t, cluster.Spec.Machinepoolconfig, "Expected MachinePools to be non-nil")
105-
//assert.Equal(t, 2, len(cluster.Spec.Machinepoolconfig), "Expected one machine pool in the cluster")
106-
//
107-
//assert.Equal(t, "test-pool", *cluster.Spec.Machinepoolconfig[1].PoolConfig.Name, "Unexpected machine pool name")
108-
//assert.Equal(t, int64(10), cluster.Spec.Machinepoolconfig[1].CloudConfig.RootDeviceSize, "Unexpected disk size")
98+
client := &client.V1Client{}
99+
100+
cluster, err := toEksCluster(client, d)
101+
102+
assert.NoError(t, err, "Expected no error from toEksCluster")
103+
assert.Equal(t, "test-cluster", cluster.Metadata.Name, "Unexpected cluster name")
104+
105+
assert.NotNil(t, cluster.Spec.Machinepoolconfig, "Expected MachinePools to be non-nil")
106+
// Without az_subnets, no cp-pool should be added
107+
assert.Equal(t, 1, len(cluster.Spec.Machinepoolconfig), "Expected one machine pool in the cluster (no cp-pool)")
108+
109+
assert.Equal(t, "test-pool", *cluster.Spec.Machinepoolconfig[0].PoolConfig.Name, "Unexpected machine pool name")
110+
assert.Equal(t, int64(10), cluster.Spec.Machinepoolconfig[0].CloudConfig.RootDeviceSize, "Unexpected disk size")
111+
}
112+
113+
func TestToEksClusterWithAzSubnets(t *testing.T) {
114+
// Setup a dummy ResourceData for testing with az_subnets
115+
d := resourceClusterEks().TestResourceData()
116+
117+
/* set the values of the ResourceData */
118+
d.Set("name", "test-cluster-az")
119+
d.Set("context", "project")
120+
d.Set("tags", []interface{}{"tag1:value1", "tag2:value2"})
121+
d.Set("cloud_account_id", "test-cloud-id")
122+
d.Set("cloud_config", []interface{}{
123+
map[string]interface{}{
124+
"ssh_key_name": "test-ssh-key",
125+
"region": "us-west-1",
126+
"vpc_id": "test-vpc-id",
127+
"endpoint_access": "public",
128+
"public_access_cidrs": []interface{}{"0.0.0.0/0"},
129+
"encryption_config_arn": "arn:test:encryption",
130+
"az_subnets": map[string]interface{}{
131+
"us-west-1a": "subnet-12345",
132+
"us-west-1b": "subnet-67890",
133+
},
134+
},
135+
})
136+
d.Set("machine_pool", []interface{}{
137+
map[string]interface{}{
138+
"name": "test-pool",
139+
"disk_size_gb": 10,
140+
"count": 2,
141+
"instance_type": "t2.micro",
142+
"capacity_type": "on-demand",
143+
"update_strategy": "RollingUpdateScaleOut",
144+
},
145+
})
146+
147+
client := &client.V1Client{}
148+
149+
cluster, err := toEksCluster(client, d)
150+
151+
assert.NoError(t, err, "Expected no error from toEksCluster")
152+
assert.Equal(t, "test-cluster-az", cluster.Metadata.Name, "Unexpected cluster name")
153+
154+
assert.NotNil(t, cluster.Spec.Machinepoolconfig, "Expected MachinePools to be non-nil")
155+
// With az_subnets having more than one element, cp-pool should be added
156+
assert.Equal(t, 2, len(cluster.Spec.Machinepoolconfig), "Expected two machine pools in the cluster (cp-pool + test-pool)")
157+
158+
// Check that cp-pool is the first one
159+
assert.Equal(t, "cp-pool", *cluster.Spec.Machinepoolconfig[0].PoolConfig.Name, "Expected first machine pool to be cp-pool")
160+
assert.True(t, cluster.Spec.Machinepoolconfig[0].PoolConfig.IsControlPlane, "Expected first machine pool to be control plane")
161+
162+
// Check that test-pool is the second one
163+
assert.Equal(t, "test-pool", *cluster.Spec.Machinepoolconfig[1].PoolConfig.Name, "Expected second machine pool to be test-pool")
164+
assert.Equal(t, int64(10), cluster.Spec.Machinepoolconfig[1].CloudConfig.RootDeviceSize, "Unexpected disk size")
109165
}
110166

111167
func TestToMachinePoolEks(t *testing.T) {

0 commit comments

Comments
 (0)