Skip to content

Commit

Permalink
feat(ocean/gcp): added is_aggressive_scale_down_enabled and `filter…
Browse files Browse the repository at this point in the history
…s` fields (#598)
  • Loading branch information
chandra1-n authored Nov 19, 2024
1 parent 2cbb6df commit 2c1c5d0
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 1.198.0 (November, 19 2024)
ENHANCEMENTS:
* resource/spotinst_ocean_gke_import: Added support for `is_aggressive_scale_down_enabled` and `filters` fields.

## 1.197.1 (Nov, 14 2024)
NOTES:
* resource/spotinst_ocean_right_sizing_rule: Fixed document format for readability.
Expand Down
20 changes: 19 additions & 1 deletion docs/resources/ocean_gke_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ resource "spotinst_ocean_gke_import" "example" {
whitelist = ["n1-standard-1", "n1-standard-2"]
filters {
exclude_families = ["n2"]
include_families = ["c2", "c3"]
min_memory_gib = 8
max_memory_gib = 16
min_vcpu = 2
max_vcpu = 16
}
backend_services {
service_name = "example-backend-service"
location_type = "regional"
Expand Down Expand Up @@ -81,6 +90,13 @@ The following arguments are supported:
* `desired_capacity` - (Optional) The number of instances to launch and maintain in the cluster.
* `whitelist` - (Optional) Instance types allowed in the Ocean cluster. Cannot be configured if blacklist list is configured.
* `blacklist` - (Optional) Instance types to avoid launching in the Ocean cluster. Cannot be configured if whitelist list is configured.
* `filters` - (Optional) List of filters. The Instance types that match with all filters compose the Ocean's whitelist parameter. Cannot be configured together with whitelist/blacklist.
* `exclude_families` - (Optional) Types belonging to a family from the ExcludeFamilies will not be available for scaling (asterisk wildcard is also supported). For example, C* will exclude instance types from these families: c5, c4, c4a, etc.
* `include_families` - (Optional) Types belonging to a family from the IncludeFamilies will be available for scaling (asterisk wildcard is also supported). For example, C* will include instance types from these families: c5, c4, c4a, etc.
* `max_memory_gib` - (Optional) Maximum amount of Memory (GiB).
* `max_vcpu` - (Optional) Maximum number of vcpus available.
* `min_memory_gib` - (Optional) Minimum amount of Memory (GiB).
* `min_vcpu` - (Optional) Minimum number of vcpus available.
* `draining_timeout` - (Optional) The draining timeout (in seconds) before terminating the instance.
* `backend_services` - (Optional) Describes the backend service configurations.
* `service_name` - (Required) The name of the backend service.
Expand Down Expand Up @@ -158,6 +174,7 @@ The following arguments are supported:
* `down` - (Optional) Auto Scaling scale down operations.
* `evaluation_periods` - (Optional, Default: `null`) The number of evaluation periods that should accumulate before a scale down action takes place.
* `max_scale_down_percentage` - (Optional) Would represent the maximum % to scale-down. Number between 1-100.
* `is_aggressive_scale_down_enabled` - (Optional, Default: `false`) When set to 'true', the Aggressive Scale Down feature is enabled.
* `resource_limits` - (Optional) Optionally set upper and lower bounds on the resource usage of the cluster.
* `max_vcpu` - (Optional) The maximum cpu in vCpu units that can be allocated to the cluster.
* `max_memory_gib` - (Optional) The maximum memory in GiB units that can be allocated to the cluster.
Expand All @@ -166,7 +183,7 @@ The following arguments are supported:
autoscaler {
is_enabled = true
is_auto_config = false
cooldown = 30
cooldown = 180
auto_headroom_percentage = 10
enable_automatic_and_manual_headroom = false
Expand All @@ -180,6 +197,7 @@ The following arguments are supported:
down {
evaluation_periods = 3
max_scale_down_percentage = 30
is_aggressive_scale_down_enabled = true
}
resource_limits {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.5.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
github.com/sethvargo/go-password v0.3.1
github.com/spotinst/spotinst-sdk-go v1.373.0
github.com/spotinst/spotinst-sdk-go v1.374.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spotinst/spotinst-sdk-go v1.373.0 h1:7swkwpX3JiLCQx6ZbXbgA36zcmUVgo2ZWUogTLp7aYw=
github.com/spotinst/spotinst-sdk-go v1.373.0/go.mod h1:Tn4/eb0SFY6IXmxz71CClujvbD/PuT+EO6Ta8v6AML4=
github.com/spotinst/spotinst-sdk-go v1.374.0 h1:SEpyp6jOP2jqk8XWVqz9ChQchodQHgs2An66ebFw6Xc=
github.com/spotinst/spotinst-sdk-go v1.374.0/go.mod h1:Tn4/eb0SFY6IXmxz71CClujvbD/PuT+EO6Ta8v6AML4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
29 changes: 15 additions & 14 deletions spotinst/ocean_gke_auto_scaling/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package ocean_gke_auto_scaling
import "github.com/spotinst/terraform-provider-spotinst/spotinst/commons"

const (
Autoscaler commons.FieldName = "autoscaler"
AutoscaleCooldown commons.FieldName = "autoscale_cooldown"
AutoscaleDown commons.FieldName = "autoscale_down"
AutoscaleHeadroom commons.FieldName = "autoscale_headroom"
AutoscaleIsAutoConfig commons.FieldName = "autoscale_is_auto_config"
AutoscaleIsEnabled commons.FieldName = "autoscale_is_enabled"
EvaluationPeriods commons.FieldName = "evaluation_periods"
CPUPerUnit commons.FieldName = "cpu_per_unit"
GPUPerUnit commons.FieldName = "gpu_per_unit"
MaxVCPU commons.FieldName = "max_vcpu"
MaxMemoryGIB commons.FieldName = "max_memory_gib"
MemoryPerUnit commons.FieldName = "memory_per_unit"
NumOfUnits commons.FieldName = "num_of_units"
ResourceLimits commons.FieldName = "resource_limits"
Autoscaler commons.FieldName = "autoscaler"
AutoscaleCooldown commons.FieldName = "autoscale_cooldown"
AutoscaleDown commons.FieldName = "autoscale_down"
AutoscaleHeadroom commons.FieldName = "autoscale_headroom"
AutoscaleIsAutoConfig commons.FieldName = "autoscale_is_auto_config"
AutoscaleIsEnabled commons.FieldName = "autoscale_is_enabled"
EvaluationPeriods commons.FieldName = "evaluation_periods"
CPUPerUnit commons.FieldName = "cpu_per_unit"
GPUPerUnit commons.FieldName = "gpu_per_unit"
MaxVCPU commons.FieldName = "max_vcpu"
MaxMemoryGIB commons.FieldName = "max_memory_gib"
MemoryPerUnit commons.FieldName = "memory_per_unit"
NumOfUnits commons.FieldName = "num_of_units"
ResourceLimits commons.FieldName = "resource_limits"
IsAggressiveScaleDownEnabled commons.FieldName = "is_aggressive_scale_down_enabled"
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
Type: schema.TypeInt,
Optional: true,
},
string(IsAggressiveScaleDownEnabled): {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -249,6 +253,12 @@ func expandOceanGKEAutoScalerDown(data interface{}) (*gcp.AutoScalerDown, error)
if v, ok := m[string(EvaluationPeriods)].(int); ok && v > 0 {
autoScaleDown.SetEvaluationPeriods(spotinst.Int(v))
}

if v, ok := m[string(IsAggressiveScaleDownEnabled)].(bool); ok {
aggressiveScaleDown := &gcp.AggressiveScaleDown{}
autoScaleDown.SetAggressiveScaleDown(aggressiveScaleDown)
autoScaleDown.AggressiveScaleDown.SetIsEnabled(spotinst.Bool(v))
}
}
return autoScaleDown, nil
}
Expand Down
10 changes: 10 additions & 0 deletions spotinst/ocean_gke_import/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ const (
BatchMinHealthyPercentage commons.FieldName = "batch_min_healthy_percentage"
RespectPdb commons.FieldName = "respect_pdb"
)

const (
Filters commons.FieldName = "filters"
ExcludeFamilies commons.FieldName = "exclude_families"
IncludeFamilies commons.FieldName = "include_families"
MaxMemoryGiB commons.FieldName = "max_memory_gib"
MaxVcpu commons.FieldName = "max_vcpu"
MinMemoryGiB commons.FieldName = "min_memory_gib"
MinVcpu commons.FieldName = "min_vcpu"
)
221 changes: 221 additions & 0 deletions spotinst/ocean_gke_import/fields_spotinst_ocean_gke_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,106 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
},
nil, nil, nil, nil,
)

fieldsMap[Filters] = commons.NewGenericField(
commons.OceanGKEInstanceTypes,
Filters,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{string(Blacklist), string(Whitelist)},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

string(ExcludeFamilies): {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

string(IncludeFamilies): {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

string(MaxMemoryGiB): {
Type: schema.TypeFloat,
Optional: true,
Default: -1,
},

string(MaxVcpu): {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},

string(MinMemoryGiB): {
Type: schema.TypeFloat,
Optional: true,
Default: -1,
},

string(MinVcpu): {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
var result []interface{} = nil

if cluster != nil && cluster.Compute != nil && cluster.Compute.InstanceTypes != nil &&
cluster.Compute.InstanceTypes.Filters != nil {
result = flattenFilters(cluster.Compute.InstanceTypes.Filters)
}
if len(result) > 0 {
if err := resourceData.Set(string(Filters), result); err != nil {
return fmt.Errorf(commons.FailureFieldReadPattern, string(Filters), err)
}
}
return nil
},

func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
if v, ok := resourceData.GetOk(string(Filters)); ok {
if filters, err := expandFilters(v, false); err != nil {
return err
} else {
cluster.Compute.InstanceTypes.SetFilters(filters)
}
}
return nil
},

func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
var value *gcp.Filters = nil

if v, ok := resourceData.GetOk(string(Filters)); ok {
if filters, err := expandFilters(v, true); err != nil {
return err
} else {
value = filters
}
}
if cluster.Compute.InstanceTypes == nil {
cluster.Compute.InstanceTypes = &gcp.InstanceTypes{}
}
cluster.Compute.InstanceTypes.SetFilters(value)
return nil
},
nil,
)
}

func expandServices(data interface{}) ([]*gcp.BackendService, error) {
Expand Down Expand Up @@ -521,3 +621,124 @@ func expandNamedPorts(data interface{}) (*gcp.NamedPorts, error) {
}
return namedPorts, nil
}

func expandFilters(data interface{}, nullify bool) (*gcp.Filters, error) {
filters := &gcp.Filters{}
list := data.([]interface{})
if list == nil || list[0] == nil {
return filters, nil
}
m := list[0].(map[string]interface{})

if v, ok := m[string(ExcludeFamilies)]; ok {
excludeFamilies, err := expandInstanceTypeFiltersList(v)
if err != nil {
return nil, err
}
if excludeFamilies != nil && len(excludeFamilies) > 0 {
filters.SetExcludeFamilies(excludeFamilies)
} else {
if nullify {
filters.SetExcludeFamilies(nil)
}
}
}

if v, ok := m[string(IncludeFamilies)]; ok {
includeFamilies, err := expandInstanceTypeFiltersList(v)
if err != nil {
return nil, err
}
if includeFamilies != nil && len(includeFamilies) > 0 {
filters.SetIncludeFamilies(includeFamilies)
} else {
if nullify {
filters.SetIncludeFamilies(nil)
}
}
}

if v, ok := m[string(MaxMemoryGiB)].(float64); ok {
if v == -1 {
filters.SetMaxMemoryGiB(nil)
} else {
filters.SetMaxMemoryGiB(spotinst.Float64(v))
}
}

if v, ok := m[string(MaxVcpu)].(int); ok {
if v == -1 {
filters.SetMaxVcpu(nil)
} else {
filters.SetMaxVcpu(spotinst.Int(v))
}
}

if v, ok := m[string(MinMemoryGiB)].(float64); ok {
if v == -1 {
filters.SetMinMemoryGiB(nil)
} else {
filters.SetMinMemoryGiB(spotinst.Float64(v))
}
}

if v, ok := m[string(MinVcpu)].(int); ok {
if v == -1 {
filters.SetMinVcpu(nil)
} else {
filters.SetMinVcpu(spotinst.Int(v))
}
}

return filters, nil
}

func expandInstanceTypeFiltersList(data interface{}) ([]string, error) {
list := data.(*schema.Set).List()
result := make([]string, 0, len(list))

for _, v := range list {
if instanceTypeList, ok := v.(string); ok && instanceTypeList != "" {
result = append(result, instanceTypeList)
}
}
return result, nil
}

func flattenFilters(filters *gcp.Filters) []interface{} {
var out []interface{}

if filters != nil {
result := make(map[string]interface{})
value := spotinst.Int(-1)
result[string(MaxMemoryGiB)] = value
result[string(MinMemoryGiB)] = value
result[string(MaxVcpu)] = value
result[string(MinVcpu)] = value

if filters.MaxMemoryGiB != nil {
result[string(MaxMemoryGiB)] = spotinst.Float64Value(filters.MaxMemoryGiB)
}
if filters.MinMemoryGiB != nil {
result[string(MinMemoryGiB)] = spotinst.Float64Value(filters.MinMemoryGiB)
}
if filters.MaxVcpu != nil {
result[string(MaxVcpu)] = spotinst.IntValue(filters.MaxVcpu)
}
if filters.MinVcpu != nil {
result[string(MinVcpu)] = spotinst.IntValue(filters.MinVcpu)
}
if filters.ExcludeFamilies != nil {
result[string(ExcludeFamilies)] = filters.ExcludeFamilies
}
if filters.IncludeFamilies != nil {
result[string(IncludeFamilies)] = filters.IncludeFamilies
}

if len(result) > 0 {
out = append(out, result)
}
}

return out
}
1 change: 1 addition & 0 deletions spotinst/ocean_gke_import_autoscaler/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ const (
GPUPerUnit commons.FieldName = "gpu_per_unit"
AutoHeadroomPercentage commons.FieldName = "auto_headroom_percentage"
EnableAutomaticAndManualHeadroom commons.FieldName = "enable_automatic_and_manual_headroom"
IsAggressiveScaleDownEnabled commons.FieldName = "is_aggressive_scale_down_enabled"
)
Loading

0 comments on commit 2c1c5d0

Please sign in to comment.