Skip to content

Commit

Permalink
feat: add custom instance with extended memory field to node template
Browse files Browse the repository at this point in the history
  • Loading branch information
jansyk13 committed Dec 1, 2023
1 parent 89fa800 commit 6428e2e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 112 deletions.
88 changes: 52 additions & 36 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,43 @@ import (
)

const (
FieldNodeTemplateArchitectures = "architectures"
FieldNodeTemplateComputeOptimized = "compute_optimized"
FieldNodeTemplateConfigurationId = "configuration_id"
FieldNodeTemplateConstraints = "constraints"
FieldNodeTemplateCustomInstancesEnabled = "custom_instances_enabled"
FieldNodeTemplateCustomLabels = "custom_labels"
FieldNodeTemplateCustomTaints = "custom_taints"
FieldNodeTemplateEnableSpotDiversity = "enable_spot_diversity"
FieldNodeTemplateExclude = "exclude"
FieldNodeTemplateExcludeNames = "exclude_names"
FieldNodeTemplateFallbackRestoreRateSeconds = "fallback_restore_rate_seconds"
FieldNodeTemplateGpu = "gpu"
FieldNodeTemplateInclude = "include"
FieldNodeTemplateIncludeNames = "include_names"
FieldNodeTemplateInstanceFamilies = "instance_families"
FieldNodeTemplateIsDefault = "is_default"
FieldNodeTemplateIsEnabled = "is_enabled"
FieldNodeTemplateIsGpuOnly = "is_gpu_only"
FieldNodeTemplateManufacturers = "manufacturers"
FieldNodeTemplateMaxCount = "max_count"
FieldNodeTemplateMaxCpu = "max_cpu"
FieldNodeTemplateMaxMemory = "max_memory"
FieldNodeTemplateMinCount = "min_count"
FieldNodeTemplateMinCpu = "min_cpu"
FieldNodeTemplateMinMemory = "min_memory"
FieldNodeTemplateName = "name"
FieldNodeTemplateOnDemand = "on_demand"
FieldNodeTemplateOs = "os"
FieldNodeTemplateRebalancingConfigMinNodes = "rebalancing_config_min_nodes"
FieldNodeTemplateShouldTaint = "should_taint"
FieldNodeTemplateSpot = "spot"
FieldNodeTemplateSpotDiversityPriceIncreaseLimitPercent = "spot_diversity_price_increase_limit_percent"
FieldNodeTemplateSpotInterruptionPredictionsEnabled = "spot_interruption_predictions_enabled"
FieldNodeTemplateSpotInterruptionPredictionsType = "spot_interruption_predictions_type"
FieldNodeTemplateStorageOptimized = "storage_optimized"
FieldNodeTemplateUseSpotFallbacks = "use_spot_fallbacks"
FieldNodeTemplateArchitectures = "architectures"
FieldNodeTemplateComputeOptimized = "compute_optimized"
FieldNodeTemplateConfigurationId = "configuration_id"
FieldNodeTemplateConstraints = "constraints"
FieldNodeTemplateCustomInstancesEnabled = "custom_instances_enabled"
FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled = "custom_instances_with_extended_memory_enabled"
FieldNodeTemplateCustomLabels = "custom_labels"
FieldNodeTemplateCustomTaints = "custom_taints"
FieldNodeTemplateEnableSpotDiversity = "enable_spot_diversity"
FieldNodeTemplateExclude = "exclude"
FieldNodeTemplateExcludeNames = "exclude_names"
FieldNodeTemplateFallbackRestoreRateSeconds = "fallback_restore_rate_seconds"
FieldNodeTemplateGpu = "gpu"
FieldNodeTemplateInclude = "include"
FieldNodeTemplateIncludeNames = "include_names"
FieldNodeTemplateInstanceFamilies = "instance_families"
FieldNodeTemplateIsDefault = "is_default"
FieldNodeTemplateIsEnabled = "is_enabled"
FieldNodeTemplateIsGpuOnly = "is_gpu_only"
FieldNodeTemplateManufacturers = "manufacturers"
FieldNodeTemplateMaxCount = "max_count"
FieldNodeTemplateMaxCpu = "max_cpu"
FieldNodeTemplateMaxMemory = "max_memory"
FieldNodeTemplateMinCount = "min_count"
FieldNodeTemplateMinCpu = "min_cpu"
FieldNodeTemplateMinMemory = "min_memory"
FieldNodeTemplateName = "name"
FieldNodeTemplateOnDemand = "on_demand"
FieldNodeTemplateOs = "os"
FieldNodeTemplateRebalancingConfigMinNodes = "rebalancing_config_min_nodes"
FieldNodeTemplateShouldTaint = "should_taint"
FieldNodeTemplateSpot = "spot"
FieldNodeTemplateSpotDiversityPriceIncreaseLimitPercent = "spot_diversity_price_increase_limit_percent"
FieldNodeTemplateSpotInterruptionPredictionsEnabled = "spot_interruption_predictions_enabled"
FieldNodeTemplateSpotInterruptionPredictionsType = "spot_interruption_predictions_type"
FieldNodeTemplateStorageOptimized = "storage_optimized"
FieldNodeTemplateUseSpotFallbacks = "use_spot_fallbacks"
)

const (
Expand Down Expand Up @@ -369,6 +370,13 @@ func resourceNodeTemplate() *schema.Resource {
Description: "Marks whether custom instances should be used when deciding which parts of inventory are available. " +
"Custom instances are only supported in GCP.",
},
FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Marks whether custom instances with extended memory should be used when deciding which parts of inventory are available. " +
"Custom instances are only supported in GCP.",
},
},
}
}
Expand Down Expand Up @@ -431,6 +439,9 @@ func resourceNodeTemplateRead(ctx context.Context, d *schema.ResourceData, meta
if err := d.Set(FieldNodeTemplateCustomInstancesEnabled, lo.FromPtrOr(nodeTemplate.CustomInstancesEnabled, false)); err != nil {
return diag.FromErr(fmt.Errorf("setting custom instances enabled: %w", err))
}
if err := d.Set(FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled, lo.FromPtrOr(nodeTemplate.CustomInstancesWithExtendedMemoryEnabled, false)); err != nil {
return diag.FromErr(fmt.Errorf("setting custom instances with extended memory enabled: %w", err))
}

return nil
}
Expand Down Expand Up @@ -576,6 +587,7 @@ func updateNodeTemplate(ctx context.Context, d *schema.ResourceData, meta any, s
FieldNodeTemplateCustomLabels,
FieldNodeTemplateCustomTaints,
FieldNodeTemplateCustomInstancesEnabled,
FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled,
FieldNodeTemplateConstraints,
FieldNodeTemplateIsEnabled,
) {
Expand Down Expand Up @@ -643,6 +655,10 @@ func updateNodeTemplate(ctx context.Context, d *schema.ResourceData, meta any, s
req.CustomInstancesEnabled = lo.ToPtr(v.(bool))
}

if v, _ := d.GetOk(FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled); v != nil {
req.CustomInstancesWithExtendedMemoryEnabled = lo.ToPtr(v.(bool))
}

resp, err := client.NodeTemplatesAPIUpdateNodeTemplateWithResponse(ctx, clusterID, name, req)
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
return diag.FromErr(checkErr)
Expand Down
18 changes: 12 additions & 6 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
"rebalancingConfig": {
"minNodes": 0
},
"customInstancesEnabled": true
"customInstancesEnabled": true,
"customInstancesWithExtendedMemoryEnabled": true
}
}
]
Expand Down Expand Up @@ -164,6 +165,7 @@ constraints.0.spot_interruption_predictions_type = aws-rebalance-recommendations
constraints.0.storage_optimized = false
constraints.0.use_spot_fallbacks = false
custom_instances_enabled = true
custom_instances_with_extended_memory_enabled = true
custom_labels.% = 2
custom_labels.key-1 = value-1
custom_labels.key-2 = value-2
Expand Down Expand Up @@ -253,7 +255,8 @@ func TestNodeTemplateResourceCreate_defaultNodeTemplate(t *testing.T) {
"rebalancingConfig": {
"minNodes": 0
},
"customInstancesEnabled": true
"customInstancesEnabled": true,
"customInstancesWithExtendedMemoryEnabled": true
}
}
]
Expand All @@ -269,10 +272,11 @@ func TestNodeTemplateResourceCreate_defaultNodeTemplate(t *testing.T) {

resource := resourceNodeTemplate()
val := cty.ObjectVal(map[string]cty.Value{
FieldClusterId: cty.StringVal(clusterId),
FieldNodeTemplateName: cty.StringVal("default-by-castai"),
FieldNodeTemplateIsDefault: cty.BoolVal(true),
FieldNodeTemplateCustomInstancesEnabled: cty.BoolVal(true),
FieldClusterId: cty.StringVal(clusterId),
FieldNodeTemplateName: cty.StringVal("default-by-castai"),
FieldNodeTemplateIsDefault: cty.BoolVal(true),
FieldNodeTemplateCustomInstancesEnabled: cty.BoolVal(true),
FieldNodeTemplateCustomInstancesWithExtendedMemoryEnabled: cty.BoolVal(true),
})
state := terraform.NewInstanceStateShimmedFromValue(val, 0)
state.ID = "default-by-castai"
Expand Down Expand Up @@ -371,6 +375,7 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "should_taint", "true"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_with_extended_memory_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_labels.%", "2"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-1", rName+"-label-value-1"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-2", rName+"-label-value-2"),
Expand Down Expand Up @@ -423,6 +428,7 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "should_taint", "true"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_with_extended_memory_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_labels.%", "2"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-1", rName+"-label-value-1"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-2", rName+"-label-value-2"),
Expand Down
57 changes: 22 additions & 35 deletions castai/sdk/api.gen.go

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

Loading

0 comments on commit 6428e2e

Please sign in to comment.