Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tri-state values in node templates #298

Closed
wants to merge 3 commits into from
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
18 changes: 9 additions & 9 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@
FieldNodeTemplateStorageOptimized: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Storage optimized instance constraint - will only pick storage optimized nodes if true",
},
FieldNodeTemplateIsGpuOnly: {
Expand All @@ -233,7 +232,6 @@
FieldNodeTemplateComputeOptimized: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Compute optimized instance constraint - will only pick compute optimized nodes if true.",
},
FieldNodeTemplateInstanceFamilies: {
Expand Down Expand Up @@ -793,7 +791,7 @@
}

if v, ok := d.Get(FieldNodeTemplateConstraints).([]any); ok && len(v) > 0 {
req.Constraints = toTemplateConstraints(v[0].(map[string]any))
req.Constraints = toTemplateConstraints(d, v[0].(map[string]any))
}

if v, _ := d.GetOk(FieldNodeTemplateCustomInstancesEnabled); v != nil {
Expand Down Expand Up @@ -864,7 +862,7 @@
}

if v, ok := d.Get(FieldNodeTemplateConstraints).([]any); ok && len(v) > 0 {
req.Constraints = toTemplateConstraints(v[0].(map[string]any))
req.Constraints = toTemplateConstraints(d, v[0].(map[string]any))
}

if v, _ := d.GetOk(FieldNodeTemplateCustomInstancesEnabled); v != nil {
Expand Down Expand Up @@ -1029,14 +1027,15 @@
return ts
}

func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConstraints {
func toTemplateConstraints(d *schema.ResourceData, obj map[string]any) *sdk.NodetemplatesV1TemplateConstraints {
if obj == nil {
return nil
}

out := &sdk.NodetemplatesV1TemplateConstraints{}
if v, ok := obj[FieldNodeTemplateComputeOptimized].(bool); ok {
out.ComputeOptimized = toPtr(v)
// todo: this must be replaced with tri-state enum as GetOkExists is deprecated
if v, exists := d.GetOkExists(FieldNodeTemplateConstraints + "." + FieldNodeTemplateComputeOptimized); exists {

Check failure on line 1037 in castai/resource_node_template.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: d.GetOkExists is deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK (staticcheck)
out.ComputeOptimized = toPtr(v.(bool))
}
if v, ok := obj[FieldNodeTemplateFallbackRestoreRateSeconds].(int); ok {
out.FallbackRestoreRateSeconds = toPtr(int32(v))
Expand Down Expand Up @@ -1075,8 +1074,9 @@
out.Spot = toPtr(!v)
}
}
if v, ok := obj[FieldNodeTemplateStorageOptimized].(bool); ok {
out.StorageOptimized = toPtr(v)
// todo: this must be replaced with tri-state enum as GetOkExists is deprecated
if v, exists := d.GetOkExists(FieldNodeTemplateConstraints + "." + FieldNodeTemplateStorageOptimized); exists {

Check failure on line 1078 in castai/resource_node_template.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: d.GetOkExists is deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK (staticcheck)
out.StorageOptimized = toPtr(v.(bool))
}
if v, ok := obj[FieldNodeTemplateUseSpotFallbacks].(bool); ok {
out.UseSpotFallbacks = toPtr(v)
Expand Down
54 changes: 54 additions & 0 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,60 @@ func TestNodeTemplateResourceDelete_defaultNodeTemplate(t *testing.T) {
" false).", result[0].Detail)
}

func TestToTemplateConstraints_HandleTriStateValues(t *testing.T) {
tests := map[string]struct {
val cty.Value
assertions func(*require.Assertions, *sdk.NodetemplatesV1TemplateConstraints)
}{
"all values empty except one": {
val: cty.ObjectVal(map[string]cty.Value{
FieldNodeTemplateConstraints: cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
FieldNodeTemplateMaxCpu: cty.NumberIntVal(100),
}),
}),
}),
assertions: func(r *require.Assertions, constraints *sdk.NodetemplatesV1TemplateConstraints) {
r.NotNil(constraints)
r.Nil(constraints.ComputeOptimized)
r.Nil(constraints.StorageOptimized)
r.Nil(constraints.Gpu)
r.Nil(constraints.InstanceFamilies)
r.Nil(constraints.MaxMemory)
r.Nil(constraints.MinMemory)
r.Nil(constraints.MinCpu)
r.Nil(constraints.Spot)
r.Nil(constraints.OnDemand)
r.Nil(constraints.UseSpotFallbacks)
r.Nil(constraints.EnableSpotDiversity)
r.Nil(constraints.SpotDiversityPriceIncreaseLimitPercent)
r.Nil(constraints.SpotInterruptionPredictionsEnabled)
r.Nil(constraints.SpotInterruptionPredictionsType)
r.Nil(constraints.CustomPriority)
r.Nil(constraints.DedicatedNodeAffinity)
r.Nil(constraints.Architectures)
r.Nil(constraints.Os)
r.Nil(constraints.IsGpuOnly)
},
},
}

for name, td := range tests {
t.Run(name, func(t *testing.T) {
r := require.New(t)
resource := resourceNodeTemplate()
state := terraform.NewInstanceStateShimmedFromValue(td.val, 0)
state.ID = "testing-template"

data := resource.Data(state)
constraintsArray := data.Get(FieldNodeTemplateConstraints).([]any)
constraintsMap := constraintsArray[0].(map[string]any)
result := toTemplateConstraints(data, constraintsMap)
td.assertions(r, result)
})
}
}

func TestAccResourceNodeTemplate_basic(t *testing.T) {
rName := fmt.Sprintf("%v-node-template-%v", ResourcePrefix, acctest.RandString(8))
resourceName := "castai_node_template.test"
Expand Down
5 changes: 5 additions & 0 deletions castai/sdk/api.gen.go

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

110 changes: 110 additions & 0 deletions castai/sdk/client.gen.go

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

35 changes: 35 additions & 0 deletions castai/sdk/mock/client.go

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

3 changes: 0 additions & 3 deletions examples/gke/gke_cluster_autoscaler_policies/castai.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ module "castai-gke-cluster" {
spot = true
use_spot_fallbacks = true
min_cpu = 4
max_cpu = 100
instance_families = {
exclude = ["e2"]
}
compute_optimized = false
storage_optimized = false

# Optional: define custom priority for instances selection.
#
Expand Down
1 change: 1 addition & 0 deletions examples/gke/gke_cluster_autoscaler_policies/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ terraform {
required_providers {
castai = {
source = "castai/castai"
version = "0.0.0-local"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
Loading