Skip to content

Commit

Permalink
feat: os filter in node template (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansyk13 authored Oct 18, 2023
1 parent 3ed7998 commit 763d960
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
FieldNodeTemplateMinMemory = "min_memory"
FieldNodeTemplateName = "name"
FieldNodeTemplateOnDemand = "on_demand"
FieldNodeTemplateOs = "os"
FieldNodeTemplateRebalancingConfigMinNodes = "rebalancing_config_min_nodes"
FieldNodeTemplateShouldTaint = "should_taint"
FieldNodeTemplateSpot = "spot"
Expand All @@ -62,10 +63,13 @@ const (
const (
ArchAMD64 = "amd64"
ArchARM64 = "arm64"
OsLinux = "linux"
OsWindows = "windows"
)

func resourceNodeTemplate() *schema.Resource {
supportedArchitectures := []string{ArchAMD64, ArchARM64}
supportedOs := []string{OsLinux, OsWindows}

return &schema.Resource{
CreateContext: resourceNodeTemplateCreate,
Expand Down Expand Up @@ -295,6 +299,21 @@ func resourceNodeTemplate() *schema.Resource {
},
Description: fmt.Sprintf("List of acceptable instance CPU architectures, the default is %s. Allowed values: %s.", ArchAMD64, strings.Join(supportedArchitectures, ", ")),
},
FieldNodeTemplateOs: {
Type: schema.TypeList,
MaxItems: 2,
MinItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(supportedOs, false)),
},
DefaultFunc: func() (interface{}, error) {
return []string{OsLinux}, nil
},
Description: fmt.Sprintf("List of acceptable instance Operating Systems, the default is %s. Allowed values: %s.", OsLinux, strings.Join(supportedOs, ", ")),
},
},
},
},
Expand Down Expand Up @@ -505,6 +524,9 @@ func flattenConstraints(c *sdk.NodetemplatesV1TemplateConstraints) ([]map[string
if c.Architectures != nil {
out[FieldNodeTemplateArchitectures] = lo.FromPtr(c.Architectures)
}
if c.Os != nil {
out[FieldNodeTemplateOs] = lo.FromPtr(c.Os)
}
return []map[string]any{out}, nil
}

Expand Down Expand Up @@ -966,6 +988,9 @@ func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConst
if v, ok := obj[FieldNodeTemplateArchitectures].([]any); ok {
out.Architectures = toPtr(toStringList(v))
}
if v, ok := obj[FieldNodeTemplateOs].([]any); ok {
out.Os = toPtr(toStringList(v))
}
if v, ok := obj[FieldNodeTemplateIsGpuOnly].(bool); ok {
out.IsGpuOnly = toPtr(v)
}
Expand Down
7 changes: 7 additions & 0 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
]
},
"architectures": ["amd64", "arm64"],
"os": ["linux"],
"gpu": {
"manufacturers": [
"NVIDIA"
Expand Down Expand Up @@ -154,6 +155,8 @@ constraints.0.max_memory = 0
constraints.0.min_cpu = 10
constraints.0.min_memory = 0
constraints.0.on_demand = true
constraints.0.os.# = 1
constraints.0.os.0 = linux
constraints.0.spot = false
constraints.0.spot_diversity_price_increase_limit_percent = 20
constraints.0.spot_interruption_predictions_enabled = true
Expand Down Expand Up @@ -394,6 +397,8 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.on_demand", "false"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architectures.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architectures.0", "amd64"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.0", "linux"),
resource.TestCheckResourceAttr(resourceName, "is_default", "false"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.enable_spot_diversity", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.spot_diversity_price_increase_limit_percent", "21"),
Expand Down Expand Up @@ -436,6 +441,8 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.use_spot_fallbacks", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architectures.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architectures.0", "arm64"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.0", "linux"),
resource.TestCheckResourceAttr(resourceName, "is_default", "false"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.enable_spot_diversity", "true"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.spot_diversity_price_increase_limit_percent", "22"),
Expand Down
3 changes: 2 additions & 1 deletion castai/sdk/api.gen.go

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

1 change: 1 addition & 0 deletions docs/resources/node_template.md

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

0 comments on commit 763d960

Please sign in to comment.