Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/resources/cluster_eks.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Refer to the [Import section](/docs#import) to learn more.

- `cloud_account_id` (String) The AWS cloud account id to use for this cluster.
- `cloud_config` (Block List, Min: 1, Max: 1) The AWS environment configuration settings such as network parameters and encryption parameters that apply to this cluster. (see [below for nested schema](#nestedblock--cloud_config))
- `machine_pool` (Block List, Min: 1) The machine pool configuration for the cluster. (see [below for nested schema](#nestedblock--machine_pool))
- `machine_pool` (Block Set, Min: 1) The machine pool configuration for the cluster. (see [below for nested schema](#nestedblock--machine_pool))
- `name` (String) The name of the cluster.

### Optional
Expand Down
41 changes: 27 additions & 14 deletions spectrocloud/cluster_common_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,39 @@ func resourceMachinePoolEksHash(v interface{}) int {
m := v.(map[string]interface{})
buf := CommonHash(m)

buf.WriteString(fmt.Sprintf("%d-", m["disk_size_gb"].(int)))
if m["min"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["min"].(int)))
// Include EKS-specific fields following MAAS pattern
if val, ok := m["disk_size_gb"]; ok {
buf.WriteString(fmt.Sprintf("%d-", val.(int)))
}
if m["max"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["max"].(int)))
if val, ok := m["instance_type"]; ok {
buf.WriteString(fmt.Sprintf("%s-", val.(string)))
}
if val, ok := m["capacity_type"]; ok {
buf.WriteString(fmt.Sprintf("%s-", val.(string)))
}
if val, ok := m["max_price"]; ok && val != nil {
buf.WriteString(fmt.Sprintf("%s-", val.(string)))
}
if val, ok := m["ami_type"]; ok && val != nil {
buf.WriteString(fmt.Sprintf("%s-", val.(string)))
}
buf.WriteString(fmt.Sprintf("%s-", m["instance_type"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["capacity_type"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["max_price"].(string)))

keys := make([]string, 0, len(m["az_subnets"].(map[string]interface{})))
for k := range m["az_subnets"].(map[string]interface{}) {
keys = append(keys, k)
// Include AZ configuration
if azSubnets, ok := m["az_subnets"].(map[string]interface{}); ok && len(azSubnets) > 0 {
buf.WriteString(HashStringMap(azSubnets))
}
sort.Strings(keys)
for _, k := range keys {
buf.WriteString(fmt.Sprintf("%s-%s", k, m["az_subnets"].(map[string]interface{})[k].(string)))
if azs, ok := m["azs"]; ok && azs != nil {
azsList := azs.([]interface{})
azsListStr := make([]string, len(azsList))
for i, v := range azsList {
azsListStr[i] = v.(string)
}
sort.Strings(azsListStr)
azsStr := strings.Join(azsListStr, "-")
buf.WriteString(fmt.Sprintf("%s-", azsStr))
}

// Include EKS launch template configuration
if m["eks_launch_template"] != nil {
buf.WriteString(eksLaunchTemplate(m["eks_launch_template"]))
}
Expand Down
2 changes: 2 additions & 0 deletions spectrocloud/cluster_common_taints.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ func FlattenAdditionalLabelsAndTaints(labels map[string]string, intaints []*mode
taints := flattenClusterTaints(intaints)
if len(taints) > 0 {
oi["taints"] = taints
} else {
oi["taints"] = make([]interface{}, 0)
}
}
81 changes: 70 additions & 11 deletions spectrocloud/resource_cluster_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func resourceClusterEks() *schema.Resource {
return &schema.Resource{
r := &schema.Resource{
CreateContext: resourceClusterEksCreate,
ReadContext: resourceClusterEksRead,
UpdateContext: resourceClusterEksUpdate,
Expand All @@ -34,7 +34,7 @@ func resourceClusterEks() *schema.Resource {
Delete: schema.DefaultTimeout(60 * time.Minute),
},

SchemaVersion: 2,
SchemaVersion: 3,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -224,15 +224,15 @@ func resourceClusterEks() *schema.Resource {
},
},
"machine_pool": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Set: resourceMachinePoolEksHash,
Description: "The machine pool configuration for the cluster.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
//ForceNew: true,
},
"additional_labels": {
Type: schema.TypeMap,
Expand Down Expand Up @@ -261,11 +261,13 @@ func resourceClusterEks() *schema.Resource {
"min": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "Minimum number of nodes in the machine pool. This is used for autoscaling the machine pool.",
},
"max": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "Maximum number of nodes in the machine pool. This is used for autoscaling the machine pool.",
},
"instance_type": {
Expand Down Expand Up @@ -382,6 +384,19 @@ func resourceClusterEks() *schema.Resource {
},
},
}

// No-op state upgrader to move from version 2 -> 3 (TypeList -> TypeSet for machine_pool)
// r.StateUpgraders = []schema.StateUpgrader{
// {
// Version: 2,
// Type: r.CoreConfigSchema().ImpliedType(),
// Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
// return rawState, nil
// },
// },
// }

return r
}

func resourceClusterEksCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -558,22 +573,42 @@ func flattenMachinePoolConfigsEks(machinePools []*models.V1EksMachinePoolConfig)
oi["max"] = int(machinePool.MaxSize)
oi["instance_type"] = machinePool.InstanceType
oi["ami_type"] = machinePool.AmiType

// Always set capacity_type (with schema default if nil)
if machinePool.CapacityType != nil {
oi["capacity_type"] = machinePool.CapacityType
oi["capacity_type"] = *machinePool.CapacityType
} else {
oi["capacity_type"] = "on-demand" // matches schema default
}

// Always set max_price (empty string if not spot)
if machinePool.SpotMarketOptions != nil {
oi["max_price"] = machinePool.SpotMarketOptions.MaxPrice
} else {
oi["max_price"] = ""
}

oi["disk_size_gb"] = int(machinePool.RootDeviceSize)

// Always set both azs and az_subnets (one populated, other empty)
if len(machinePool.SubnetIds) > 0 {
oi["az_subnets"] = machinePool.SubnetIds
oi["azs"] = make([]interface{}, 0) // empty array
} else {
oi["azs"] = machinePool.Azs
oi["az_subnets"] = make(map[string]interface{}) // empty map
if machinePool.Azs != nil {
oi["azs"] = machinePool.Azs
} else {
oi["azs"] = make([]interface{}, 0) // empty array
}
}
eksLaunchTemplates := flattenEksLaunchTemplate(machinePool.AwsLaunchTemplate)

// Always set eks_launch_template (empty array if not configured)
eksLaunchTemplates := flattenEksLaunchTemplate(machinePool.AwsLaunchTemplate)
if eksLaunchTemplates != nil {
oi["eks_launch_template"] = flattenEksLaunchTemplate(machinePool.AwsLaunchTemplate)
oi["eks_launch_template"] = eksLaunchTemplates
} else {
oi["eks_launch_template"] = make([]interface{}, 0)
}

ois = append(ois, oi)
Expand Down Expand Up @@ -699,8 +734,24 @@ func resourceClusterEksUpdate(ctx context.Context, d *schema.ResourceData, m int
nraw = new(schema.Set)
}

os := oraw.([]interface{})
ns := nraw.([]interface{})
var os []interface{}
var ns []interface{}
switch v := oraw.(type) {
case *schema.Set:
os = v.List()
case []interface{}:
os = v
default:
os = []interface{}{}
}
switch v := nraw.(type) {
case *schema.Set:
ns = v.List()
case []interface{}:
ns = v
default:
ns = []interface{}{}
}

osMap := make(map[string]interface{})
for _, mp := range os {
Expand Down Expand Up @@ -851,7 +902,15 @@ func toEksCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro
machinePoolConfigs = append(machinePoolConfigs, toMachinePoolEks(cpPool))
}

for _, machinePool := range d.Get("machine_pool").([]interface{}) {
var machinePools []interface{}
if v := d.Get("machine_pool"); v != nil {
if set, ok := v.(*schema.Set); ok {
machinePools = set.List()
} else if list, ok := v.([]interface{}); ok {
machinePools = list
}
}
for _, machinePool := range machinePools {
mp := toMachinePoolEks(machinePool)
machinePoolConfigs = append(machinePoolConfigs, mp)
}
Expand Down
Loading