Skip to content

Commit

Permalink
Storage and compute optimized constraints tri states (#310)
Browse files Browse the repository at this point in the history
* impl

* clean

* clean

* clean

* tf format

* add deprecation message

* update sdk

* update docs

* status->state

* build sdk

* generate sdk

* generate docs

* update message
  • Loading branch information
linkas45 authored May 14, 2024
1 parent e93aeae commit 3e25bed
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 33 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,41 @@ module "castai-aks-cluster" {
}
```
Migrating from 6.x.x to 7.x.x
---------------------------
Version 7.x.x changed:
* Removed `compute_optimized` and `storage_optimized` attributes in `castai_node_template` resource, `constraints` object. Use `compute_optimized_state` and `storage_optimized_state` instead.
Old configuration:
```terraform
module "castai-aks-cluster" {
node_templates = {
spot_tmpl = {
constraints = {
compute_optimized = false
storage_optimized = true
}
}
}
}
```
New configuration:
```terraform
module "castai-aks-cluster" {
node_templates = {
spot_tmpl = {
constraints = {
compute_optimized_state = "disabled"
storage_optimized_state = "enabled"
}
}
}
}
```
For more information for `castai-aks-cluster` module follow:
https://github.com/castai/terraform-castai-aks/blob/main/README.md#migrating-from-2xx-to-3xx
If you have used `castai-eks-cluster` or other modules follow:
Expand Down
3 changes: 3 additions & 0 deletions castai/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ const (
FieldKey = "key"
FieldValue = "value"
FieldEffect = "effect"

Enabled = "enabled"
Disabled = "disabled"
)
77 changes: 67 additions & 10 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -21,6 +22,7 @@ const (
FieldNodeTemplateArchitectures = "architectures"
FieldNodeTemplateAZs = "azs"
FieldNodeTemplateComputeOptimized = "compute_optimized"
FieldNodeTemplateComputeOptimizedState = "compute_optimized_state"
FieldNodeTemplateConfigurationId = "configuration_id"
FieldNodeTemplateConstraints = "constraints"
FieldNodeTemplateCustomInstancesEnabled = "custom_instances_enabled"
Expand Down Expand Up @@ -55,6 +57,7 @@ const (
FieldNodeTemplateSpotInterruptionPredictionsEnabled = "spot_interruption_predictions_enabled"
FieldNodeTemplateSpotInterruptionPredictionsType = "spot_interruption_predictions_type"
FieldNodeTemplateStorageOptimized = "storage_optimized"
FieldNodeTemplateStorageOptimizedState = "storage_optimized_state"
FieldNodeTemplateUseSpotFallbacks = "use_spot_fallbacks"
FieldNodeTemplateCustomPriority = "custom_priority"
FieldNodeTemplateDedicatedNodeAffinity = "dedicated_node_affinity"
Expand Down Expand Up @@ -231,8 +234,24 @@ func resourceNodeTemplate() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Storage optimized instance constraint - will only pick storage optimized nodes if true",
Deprecated: "Storage optimized constraint is deprecated and will be replaced by a new argument in the next major release.",
Description: "Storage optimized instance constraint (deprecated).",
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: "Deprecated field `storage_optimized`",
Detail: "Please use `storage_optimized_state` instead, supported values: `enabled`, `disabled` or empty string. See: https://github.com/castai/terraform-provider-castai#migrating-from-6xx-to-7xx",
AttributePath: path,
},
}
},
},
FieldNodeTemplateStorageOptimizedState: {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Storage optimized instance constraint - will only pick storage optimized nodes if enabled and won't pick if disabled. Empty value will have no effect. Supported values: `enabled`, `disabled` or empty string.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"", Enabled, Disabled}, false)),
},
FieldNodeTemplateIsGpuOnly: {
Type: schema.TypeBool,
Expand All @@ -244,8 +263,24 @@ func resourceNodeTemplate() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Compute optimized instance constraint - will only pick compute optimized nodes if true.",
Deprecated: "Compute optimized constraint is deprecated and will be replaced by a new argument in the next major release.",
Description: "Compute optimized instance constraint (deprecated).",
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: "Deprecated field `compute_optimized`",
Detail: "Please use `compute_optimized_state` instead, supported values: `enabled`, `disabled` or empty string. See: https://github.com/castai/terraform-provider-castai#migrating-from-6xx-to-7xx",
AttributePath: path,
},
}
},
},
FieldNodeTemplateComputeOptimizedState: {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Will only include compute optimized nodes when enabled and exclude compute optimized nodes when disabled. Empty value won't have effect on instances filter. Supported values: `enabled`, `disabled` or empty string.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"", Enabled, Disabled}, false)),
},
FieldNodeTemplateInstanceFamilies: {
Type: schema.TypeList,
Expand Down Expand Up @@ -578,10 +613,18 @@ func flattenConstraints(c *sdk.NodetemplatesV1TemplateConstraints) ([]map[string
out[FieldNodeTemplateInstanceFamilies] = flattenInstanceFamilies(c.InstanceFamilies)
}
if c.ComputeOptimized != nil {
out[FieldNodeTemplateComputeOptimized] = c.ComputeOptimized
if lo.FromPtr(c.ComputeOptimized) {
out[FieldNodeTemplateComputeOptimizedState] = Enabled
} else {
out[FieldNodeTemplateComputeOptimizedState] = Disabled
}
}
if c.StorageOptimized != nil {
out[FieldNodeTemplateStorageOptimized] = c.StorageOptimized
if lo.FromPtr(c.StorageOptimized) {
out[FieldNodeTemplateStorageOptimizedState] = Enabled
} else {
out[FieldNodeTemplateStorageOptimizedState] = Disabled
}
}
if c.Spot != nil {
out[FieldNodeTemplateSpot] = c.Spot
Expand Down Expand Up @@ -1049,8 +1092,15 @@ func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConst
}

out := &sdk.NodetemplatesV1TemplateConstraints{}
if v, ok := obj[FieldNodeTemplateComputeOptimized].(bool); ok {
out.ComputeOptimized = toPtr(v)
if v, ok := obj[FieldNodeTemplateComputeOptimizedState].(string); ok {
switch v {
case Enabled:
out.ComputeOptimized = toPtr(true)
case Disabled:
out.ComputeOptimized = toPtr(false)
default:
out.ComputeOptimized = nil
}
}
if v, ok := obj[FieldNodeTemplateFallbackRestoreRateSeconds].(int); ok {
out.FallbackRestoreRateSeconds = toPtr(int32(v))
Expand Down Expand Up @@ -1089,8 +1139,15 @@ func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConst
out.Spot = toPtr(!v)
}
}
if v, ok := obj[FieldNodeTemplateStorageOptimized].(bool); ok {
out.StorageOptimized = toPtr(v)
if v, ok := obj[FieldNodeTemplateStorageOptimizedState].(string); ok {
switch v {
case Enabled:
out.StorageOptimized = toPtr(true)
case Disabled:
out.StorageOptimized = toPtr(false)
default:
out.StorageOptimized = nil
}
}
if v, ok := obj[FieldNodeTemplateUseSpotFallbacks].(bool); ok {
out.UseSpotFallbacks = toPtr(v)
Expand Down
17 changes: 11 additions & 6 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
"spotDiversityPriceIncreaseLimitPercent": 20,
"spotInterruptionPredictionsEnabled": true,
"spotInterruptionPredictionsType": "aws-rebalance-recommendations",
"storageOptimized": false,
"storageOptimized": true,
"computeOptimized": false,
"minCpu": 10,
"maxCpu": 10000,
Expand Down Expand Up @@ -160,6 +160,7 @@ constraints.0.azs.0 = us-west-2a
constraints.0.azs.1 = us-west-2b
constraints.0.azs.2 = us-west-2c
constraints.0.compute_optimized = false
constraints.0.compute_optimized_state = disabled
constraints.0.custom_priority.# = 1
constraints.0.custom_priority.0.instance_families.# = 2
constraints.0.custom_priority.0.instance_families.0 = a
Expand Down Expand Up @@ -208,6 +209,7 @@ constraints.0.spot_diversity_price_increase_limit_percent = 20
constraints.0.spot_interruption_predictions_enabled = true
constraints.0.spot_interruption_predictions_type = aws-rebalance-recommendations
constraints.0.storage_optimized = false
constraints.0.storage_optimized_state = enabled
constraints.0.use_spot_fallbacks = false
custom_instances_enabled = true
custom_instances_with_extended_memory_enabled = true
Expand Down Expand Up @@ -467,6 +469,8 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.0.spot", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.0.on_demand", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.dedicated_node_affinity.#", "0"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.storage_optimized_state", "disabled"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.compute_optimized_state", ""),
),
},
{
Expand Down Expand Up @@ -529,6 +533,8 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.1.spot", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.1.on_demand", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.dedicated_node_affinity.#", "0"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.storage_optimized_state", "enabled"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.compute_optimized_state", "disabled"),
),
},
},
Expand Down Expand Up @@ -583,6 +589,7 @@ func testAccNodeTemplateConfig(rName, clusterName string) string {
spot_interruption_predictions_enabled = true
spot_interruption_predictions_type = "interruption-predictions"
use_spot_fallbacks = true
storage_optimized_state = "disabled"
min_cpu = 4
max_cpu = 100
instance_families {
Expand All @@ -593,9 +600,7 @@ func testAccNodeTemplateConfig(rName, clusterName string) string {
include_names = []
exclude_names = []
manufacturers = ["NVIDIA"]
}
compute_optimized = false
storage_optimized = false
}
custom_priority {
instance_families = ["c", "d"]
Expand Down Expand Up @@ -640,8 +645,8 @@ func testNodeTemplateUpdated(rName, clusterName string) string {
spot_interruption_predictions_enabled = true
spot_interruption_predictions_type = "interruption-predictions"
fallback_restore_rate_seconds = 1800
storage_optimized = false
compute_optimized = false
storage_optimized_state = "enabled"
compute_optimized_state = "disabled"
architectures = ["arm64"]
azs = ["eu-central-1a", "eu-central-1b", "eu-central-1c"]
Expand Down
6 changes: 4 additions & 2 deletions docs/resources/node_template.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/aks/aks_cluster_arm_template/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ module "castai-aks-cluster" {
instance_families = {
exclude = ["standard_DPLSv5"]
}
compute_optimized = false
storage_optimized = false
compute_optimized_state = "disabled"
storage_optimized_state = "disabled"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/aks/aks_cluster_autoscaler_policies/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ module "castai-aks-cluster" {
instance_families = {
exclude = ["standard_DPLSv5"]
}
compute_optimized = false
storage_optimized = false
compute_optimized_state = "disabled"
storage_optimized_state = "disabled"

# Optional: define custom priority for instances selection.
#
Expand Down
6 changes: 3 additions & 3 deletions examples/eks/eks_cluster_autoscaler_policies/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ module "castai-eks-cluster" {
instance_families = {
exclude = ["m5"]
}
compute_optimized = false
storage_optimized = false
is_gpu_only = false
compute_optimized_state = "disabled"
storage_optimized_state = "disabled"
is_gpu_only = false

# Optional: define custom priority for instances selection.
#
Expand Down
6 changes: 3 additions & 3 deletions examples/eks/eks_clusters/module/castai/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ locals {
instance_families = {
exclude = ["m5"]
}
compute_optimized = false
storage_optimized = false
is_gpu_only = false
compute_optimized_state = "disabled"
storage_optimized_state = "disabled"
is_gpu_only = false
}
}
})
Expand Down
5 changes: 2 additions & 3 deletions examples/gke/gke_cluster_autoscaler_policies/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ module "castai-gke-cluster" {
instance_families = {
exclude = ["e2"]
}
compute_optimized = false
storage_optimized = false

compute_optimized_state = "disabled"
storage_optimized_state = "disabled"
# Optional: define custom priority for instances selection.
#
# 1. Prioritize C2D and C2 spot instances above all else, regardless of price.
Expand Down
4 changes: 2 additions & 2 deletions examples/gke/gke_cluster_existing/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ module "castai-gke-cluster" {
instance_families = {
exclude = ["e2"]
}
compute_optimized = false
storage_optimized = false
compute_optimized_state = "disabled"
storage_optimized_state = "disabled"
}

custom_instances_enabled = true
Expand Down

0 comments on commit 3e25bed

Please sign in to comment.