Skip to content

Commit

Permalink
PLT-552: aws and vmware cluster context support. (#311)
Browse files Browse the repository at this point in the history
* PLT-497: Draft for additional security groups.

* PLT-532:Fixed placement change detection issue (#300)

* PLT-532:Fixed placement change detection issue

* fix diff

* Docs-0.14.2 (#297)

* Generate docs.

* Generate docs.

* PLT-527: validate placements irrespective of the order. (#302)

* PLT-527:Fixed Vsphere control plane placement validation

* added comments

* placement validation

* PLT-527: Added sorting and correct error message.

* Update spectrocloud/resource_cluster_vsphere.go

* PLT-536:fix for additional_label delete

---------

Co-authored-by: Sivaanand Murugesan <[email protected]>

* PLT-537: Adding autoscaling options for MAAS. (#304)

* PLT-542:Added Validation for resource_pool attribute for control_place placements (#307)

* Docs refresh for 0.14.2. (#305)

* Docs refresh for 0.14.2.

* Min max description add for 0.14.2.

* PLT-546: Workaround empty element in Set framework issue. (#309)

* PLT-525: AWS US GOV cloud account support. (#299)

* PLT-525: AWS US GOV cloud account support.

* PLT-525: Correcting unit test.

* PLT-525: Unit test stubs for resources.

* PLT-543:Added support for OCI registry for profile creation (#308)

* Fixing unit-tests and refresh docs. (#310)

* PLT-536:Added fix for machine pool type set empty item issue (#306)

* PLT-536:Added fix for machine pool type set empty item issue

* Update spectrocloud/resource_cluster_aks.go

Co-authored-by: nikchern <[email protected]>

* Update spectrocloud/resource_cluster_aws.go

* Update spectrocloud/resource_cluster_azure.go

* Update spectrocloud/resource_cluster_coxedge.go

* Update spectrocloud/resource_cluster_edge_native.go

* Update spectrocloud/resource_cluster_edge_vsphere.go

* Update spectrocloud/resource_cluster_eks.go

* Update spectrocloud/resource_cluster_gcp.go

* Update spectrocloud/resource_cluster_libvirt.go

* Update spectrocloud/resource_cluster_openstack.go

* Update spectrocloud/resource_cluster_tke.go

* Update spectrocloud/resource_cluster_virtual.go

---------

Co-authored-by: nikchern <[email protected]>

* PLT-497: Additional security groups support.

* PLT-497: Split schema for AWS support.

* PLT-552: context fix for VMware and AWS.

* PLT-552: docs refresh.

---------

Co-authored-by: Sivaanand Murugesan <[email protected]>
  • Loading branch information
nikchern and SivaanandM authored Jul 7, 2023
1 parent 278ce01 commit 018cfa2
Show file tree
Hide file tree
Showing 27 changed files with 398 additions and 52 deletions.
3 changes: 3 additions & 0 deletions docs/resources/cluster_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,16 @@ Required:
Optional:

- `additional_labels` (Map of String)
- `additional_security_groups` (Set of String) Additional security groups to attach to the instance.
- `az_subnets` (Map of String) Mutually exclusive with `azs`. Use `az_subnets` for Static provisioning.
- `azs` (Set of String) Mutually exclusive with `az_subnets`. Use `azs` for Dynamic provisioning.
- `capacity_type` (String) Capacity type is an instance type, can be 'on-demand' or 'spot'. Defaults to 'on-demand'.
- `control_plane` (Boolean) Whether this machine pool is a control plane. Defaults to `false`.
- `control_plane_as_worker` (Boolean) Whether this machine pool is a control plane and a worker. Defaults to `false`.
- `disk_size_gb` (Number)
- `max` (Number) Maximum number of nodes in the machine pool. This is used for autoscaling the machine pool.
- `max_price` (String)
- `min` (Number) Minimum number of nodes in the machine pool. This is used for autoscaling the machine pool.
- `taints` (Block List) (see [below for nested schema](#nestedblock--machine_pool--taints))
- `update_strategy` (String) Update strategy for the machine pool. Valid values are `RollingUpdateScaleOut` and `RollingUpdateScaleIn`.

Expand Down
1 change: 1 addition & 0 deletions docs/resources/cluster_eks.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Optional:

Optional:

- `additional_security_groups` (Set of String) Additional security groups to attach to the instance.
- `ami_id` (String) The ID of the custom Amazon Machine Image (AMI).
- `root_volume_iops` (Number) The number of input/output operations per second (IOPS) for the root volume.
- `root_volume_throughput` (Number) The throughput of the root volume in MiB/s.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/robfig/cron v1.2.0
github.com/spectrocloud/hapi v1.14.1-0.20230619173315-64e3e574c13d
github.com/spectrocloud/palette-sdk-go v0.0.0-20230629160306-b732211f7547
github.com/spectrocloud/hapi v1.14.1-0.20230703141843-3da1ad40c0a6
github.com/spectrocloud/palette-sdk-go v0.0.0-20230704181323-336d6bd76b91
github.com/stretchr/testify v1.8.0
gotest.tools v2.2.0+incompatible
k8s.io/api v0.23.5
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,10 @@ github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spectrocloud/gomi v1.14.1-0.20230412095143-b0595c6c6f08 h1:AnOC0U+ExlKBeT5yF2Pg8PPfVOfxwOUBS/5deOl1Q4Y=
github.com/spectrocloud/gomi v1.14.1-0.20230412095143-b0595c6c6f08/go.mod h1:UnhUDpFEvtYh6m384r3xzj8/+Z6/hMp2O8whEMYVHec=
github.com/spectrocloud/hapi v1.14.1-0.20230619173315-64e3e574c13d h1:4R0CarVmnTRT0b4SlkFWZcwiNRtavjWAcwnSMYmGhGg=
github.com/spectrocloud/hapi v1.14.1-0.20230619173315-64e3e574c13d/go.mod h1:9lX5c6bShSkAg24223A7XBCyJj4/Kr9w0YFv6Mf5ZlE=
github.com/spectrocloud/palette-sdk-go v0.0.0-20230629160306-b732211f7547 h1:WuZD24RZC3mcYPWuBfzVZI35DE+eieJwChf/jOhH3jw=
github.com/spectrocloud/palette-sdk-go v0.0.0-20230629160306-b732211f7547/go.mod h1:KiG053DAd5vV/nWv1KgVOI6YkHWuMGb5wpD6j2zzttk=
github.com/spectrocloud/hapi v1.14.1-0.20230703141843-3da1ad40c0a6 h1:gnTkGdGus+IcEsqJRDvZ2gpGwmx6kbei2wB9LRLwxZc=
github.com/spectrocloud/hapi v1.14.1-0.20230703141843-3da1ad40c0a6/go.mod h1:O/Bkbw92QPSGPNQPqKt7Qlkn+9BKK/a22KTUlk76KHI=
github.com/spectrocloud/palette-sdk-go v0.0.0-20230704181323-336d6bd76b91 h1:BGRy3b/xP4JaZqhAy+Ik2CcqX39jbkkYtYeyCkbnlWs=
github.com/spectrocloud/palette-sdk-go v0.0.0-20230704181323-336d6bd76b91/go.mod h1:KiG053DAd5vV/nWv1KgVOI6YkHWuMGb5wpD6j2zzttk=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down
8 changes: 6 additions & 2 deletions spectrocloud/cluster_common_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (

// read common fields like kubeconfig, tags, backup policy, scan policy, cluster_rbac_binding, namespaces
func readCommonFields(c *client.V1Client, d *schema.ResourceData, cluster *models.V1SpectroCluster) (diag.Diagnostics, bool) {
kubecfg, err := c.GetClusterKubeConfig(d.Id())
ClusterContext := "project"
if cluster.Metadata.Annotations["scope"] != "" {
ClusterContext = cluster.Metadata.Annotations["scope"]
}
kubecfg, err := c.GetClusterKubeConfig(d.Id(), ClusterContext)
if err != nil {
return diag.FromErr(err), true
}
Expand All @@ -21,7 +25,7 @@ func readCommonFields(c *client.V1Client, d *schema.ResourceData, cluster *model
return diag.FromErr(err), true
}

if policy, err := c.GetClusterBackupConfig(d.Id()); err != nil {
if policy, err := c.GetClusterBackupConfig(d.Id(), ClusterContext); err != nil {
return diag.FromErr(err), true
} else if policy != nil && policy.Spec.Config != nil {
if err := d.Set("backup_policy", flattenBackupPolicy(policy.Spec.Config)); err != nil {
Expand Down
50 changes: 50 additions & 0 deletions spectrocloud/cluster_common_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"hash/fnv"
"sort"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -96,11 +97,29 @@ func resourceMachinePoolAwsHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%t-", m["control_plane_as_worker"].(bool)))
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%d-", m["count"].(int)))

if m["min"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["min"].(int)))
}
if m["max"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["max"].(int)))
}
buf.WriteString(fmt.Sprintf("%s-", m["update_strategy"].(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)))
if m["azs"] != nil {
azsSet := m["azs"].(*schema.Set)
azsList := azsSet.List()
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))
}
buf.WriteString(fmt.Sprintf("%s-", m["azs"].(*schema.Set).GoString()))
buf.WriteString(HashStringMap(m["az_subnets"]))

Expand Down Expand Up @@ -133,9 +152,40 @@ func resourceMachinePoolEksHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%s-%s", i, j.(string)))
}

if m["eks_launch_template"] != nil {
buf.WriteString(eksLaunchTemplate(m["eks_launch_template"]))
}

return int(hash(buf.String()))
}

func eksLaunchTemplate(v interface{}) string {
var buf bytes.Buffer
if len(v.([]interface{})) > 0 {
m := v.([]interface{})[0].(map[string]interface{})

if m["ami_id"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["ami_id"].(string)))
}
if m["root_volume_type"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["root_volume_type"].(string)))
}
if m["root_volume_iops"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["root_volume_iops"].(int)))
}
if m["root_volume_throughput"] != nil {
buf.WriteString(fmt.Sprintf("%d-", m["root_volume_throughput"].(int)))
}
if m["additional_security_groups"] != nil {
for _, sg := range m["additional_security_groups"].(*schema.Set).List() {
buf.WriteString(fmt.Sprintf("%s-", sg.(string)))
}
}
}

return buf.String()
}

func resourceMachinePoolCoxEdgeHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion spectrocloud/data_source_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func dataSourceClusterRead(_ context.Context, d *schema.ResourceData, m interfac
}
if cluster != nil {
d.SetId(cluster.Metadata.UID)
kubeConfig, _ := c.GetClusterKubeConfig(cluster.Metadata.UID)
kubeConfig, _ := c.GetClusterKubeConfig(cluster.Metadata.UID, ClusterContext)
if err := d.Set("kube_config", kubeConfig); err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 0 additions & 2 deletions spectrocloud/data_volume_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func TestCreateDataVolumePositive(t *testing.T) {
ClusterRbac: nil,
ClusterResources: nil,
ControlPlaneHealthCheckTimeout: "",
Fips: nil,
HostClusterConfig: &models.V1HostClusterConfig{
ClusterEndpoint: &models.V1HostClusterEndpoint{
Config: nil,
Expand Down Expand Up @@ -223,7 +222,6 @@ func TestCreateDataVolume(t *testing.T) {
ClusterRbac: nil,
ClusterResources: nil,
ControlPlaneHealthCheckTimeout: "",
Fips: nil,
HostClusterConfig: &models.V1HostClusterConfig{
ClusterEndpoint: &models.V1HostClusterEndpoint{
Config: nil,
Expand Down
2 changes: 1 addition & 1 deletion spectrocloud/resource_cluster_aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func resourceClusterAksUpdate(ctx context.Context, d *schema.ResourceData, m int

for _, mp := range ns {
machinePoolResource := mp.(map[string]interface{})
// since known issue in TF SDK: https://github.com/hashicorp/terraform-plugin-sdk/issues/588
// since known issue in TF SDK: https://github.com/hashicorp/terraform-plugin-sdk/issues/588
if machinePoolResource["name"].(string) != "" {
name := machinePoolResource["name"].(string)
hash := resourceMachinePoolAksHash(machinePoolResource)
Expand Down
91 changes: 86 additions & 5 deletions spectrocloud/resource_cluster_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spectrocloud
import (
"context"
"log"
"sort"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -156,6 +157,16 @@ func resourceClusterAws() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"min": {
Type: schema.TypeInt,
Optional: true,
Description: "Minimum number of nodes in the machine pool. This is used for autoscaling the machine pool.",
},
"max": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum number of nodes in the machine pool. This is used for autoscaling the machine pool.",
},
"capacity_type": {
Type: schema.TypeString,
Default: "on-demand",
Expand Down Expand Up @@ -198,6 +209,15 @@ func resourceClusterAws() *schema.Resource {
Required: true,
},
},
"additional_security_groups": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "Additional security groups to attach to the instance.",
},
},
},
},
Expand Down Expand Up @@ -225,12 +245,12 @@ func resourceClusterAwsCreate(ctx context.Context, d *schema.ResourceData, m int

cluster := toAwsCluster(c, d)

uid, err := c.CreateClusterAws(cluster)
ClusterContext := d.Get("context").(string)
uid, err := c.CreateClusterAws(cluster, ClusterContext)
if err != nil {
return diag.FromErr(err)
}

ClusterContext := d.Get("context").(string)
diagnostics, isError := waitForClusterCreation(ctx, d, ClusterContext, uid, diags, c, true)
if isError {
return diagnostics
Expand Down Expand Up @@ -293,12 +313,16 @@ func flattenMachinePoolConfigsAws(machinePools []*models.V1AwsMachinePoolConfig)

SetAdditionalLabelsAndTaints(machinePool.AdditionalLabels, machinePool.Taints, oi)

oi["control_plane"] = machinePool.IsControlPlane
if machinePool.IsControlPlane != nil {
oi["control_plane"] = *machinePool.IsControlPlane
}
oi["control_plane_as_worker"] = machinePool.UseControlPlaneAsWorker
oi["name"] = machinePool.Name
oi["count"] = int(machinePool.Size)
flattenUpdateStrategy(machinePool.UpdateStrategy, oi)

oi["min"] = int(machinePool.MinSize)
oi["max"] = int(machinePool.MaxSize)
oi["instance_type"] = machinePool.InstanceType
if machinePool.CapacityType != nil {
oi["capacity_type"] = machinePool.CapacityType
Expand All @@ -312,9 +336,36 @@ func flattenMachinePoolConfigsAws(machinePools []*models.V1AwsMachinePoolConfig)
} else {
oi["azs"] = machinePool.Azs
}

if machinePool.AdditionalSecurityGroups != nil && len(machinePool.AdditionalSecurityGroups) > 0 {
additionalSecuritygroup := make([]string, 0)
for _, sg := range machinePool.AdditionalSecurityGroups {
additionalSecuritygroup = append(additionalSecuritygroup, sg.ID)
}
oi["additional_security_groups"] = additionalSecuritygroup
}

ois[i] = oi
}

sort.SliceStable(ois, func(i, j int) bool {
var controlPlaneI, controlPlaneJ bool
if ois[i].(map[string]interface{})["control_plane"] != nil {
controlPlaneI = ois[i].(map[string]interface{})["control_plane"].(bool)
}
if ois[j].(map[string]interface{})["control_plane"] != nil {
controlPlaneJ = ois[j].(map[string]interface{})["control_plane"].(bool)
}

// If both are control planes or both are not, sort by name
if controlPlaneI == controlPlaneJ {
return ois[i].(map[string]interface{})["name"].(string) < ois[j].(map[string]interface{})["name"].(string)
}

// Otherwise, control planes come first
return controlPlaneI && !controlPlaneJ
})

return ois
}

Expand Down Expand Up @@ -346,7 +397,7 @@ func resourceClusterAwsUpdate(ctx context.Context, d *schema.ResourceData, m int

for _, mp := range ns.List() {
machinePoolResource := mp.(map[string]interface{})
// since known issue in TF SDK: https://github.com/hashicorp/terraform-plugin-sdk/issues/588
// since known issue in TF SDK: https://github.com/hashicorp/terraform-plugin-sdk/issues/588
if machinePoolResource["name"].(string) != "" {
name := machinePoolResource["name"].(string)
if name != "" {
Expand Down Expand Up @@ -420,13 +471,25 @@ func toAwsCluster(c *client.V1Client, d *schema.ResourceData) *models.V1SpectroA
},
}

//for _, machinePool := range d.Get("machine_pool").([]interface{}) {
machinePoolConfigs := make([]*models.V1AwsMachinePoolConfigEntity, 0)
for _, machinePool := range d.Get("machine_pool").(*schema.Set).List() {
mp := toMachinePoolAws(machinePool, cluster.Spec.CloudConfig.VpcID)
machinePoolConfigs = append(machinePoolConfigs, mp)
}

sort.SliceStable(machinePoolConfigs, func(i, j int) bool {
controlPlaneI := machinePoolConfigs[i].PoolConfig.IsControlPlane
controlPlaneJ := machinePoolConfigs[j].PoolConfig.IsControlPlane

// If both are control planes or both are not, sort by name
if controlPlaneI == controlPlaneJ {
return *machinePoolConfigs[i].PoolConfig.Name < *machinePoolConfigs[j].PoolConfig.Name
}

// Otherwise, control planes come first
return controlPlaneI && !controlPlaneJ
})

cluster.Spec.Machinepoolconfig = machinePoolConfigs
cluster.Spec.ClusterConfig = toClusterConfig(d)

Expand Down Expand Up @@ -463,6 +526,17 @@ func toMachinePoolAws(machinePool interface{}, vpcId string) *models.V1AwsMachin
azs = append(azs, az.(string))
}
}
min := int32(m["count"].(int))
max := int32(m["count"].(int))

if m["min"] != nil {
min = int32(m["min"].(int))
}

if m["max"] != nil {
max = int32(m["max"].(int))
}

mp := &models.V1AwsMachinePoolConfigEntity{
CloudConfig: &models.V1AwsMachinePoolCloudConfigEntity{
Azs: azs,
Expand All @@ -481,6 +555,8 @@ func toMachinePoolAws(machinePool interface{}, vpcId string) *models.V1AwsMachin
UpdateStrategy: &models.V1UpdateStrategy{
Type: getUpdateStrategy(m),
},
MinSize: min,
MaxSize: max,
UseControlPlaneAsWorker: controlPlaneAsWorker,
},
}
Expand All @@ -495,5 +571,10 @@ func toMachinePoolAws(machinePool interface{}, vpcId string) *models.V1AwsMachin
MaxPrice: maxPrice,
}
}

if m["additional_security_groups"] != nil {
mp.CloudConfig.AdditionalSecurityGroups = setAdditionalSecurityGroups(m)
}

return mp
}
Loading

0 comments on commit 018cfa2

Please sign in to comment.