Skip to content

Commit

Permalink
feat: add architecture priory node template constraint (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenorgate authored Oct 7, 2024
1 parent 97c1c74 commit 8dc5255
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
23 changes: 23 additions & 0 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
FieldNodeTemplateBurstableInstances = "burstable_instances"
FieldNodeTemplateCustomerSpecific = "customer_specific"
FieldNodeTemplateCPUManufacturers = "cpu_manufacturers"
FieldNodeTemplateArchitecturePriority = "architecture_priority"
)

const (
Expand Down Expand Up @@ -510,6 +511,21 @@ func resourceNodeTemplate() *schema.Resource {
},
Description: fmt.Sprintf("List of acceptable CPU manufacturers. Allowed values: %s.", strings.Join(supportedCPUManufacturers, ", ")),
},
FieldNodeTemplateArchitecturePriority: {
Type: schema.TypeList,
MaxItems: 2,
MinItems: 0,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(supportedArchitectures, false)),
},
DefaultFunc: func() (interface{}, error) {
return []string{}, nil
},
Description: fmt.Sprintf("Priority ordering of architectures, specifying no priority will pick cheapest. Allowed values: %s.", strings.Join(supportedArchitectures, ", ")),
},
},
},
},
Expand Down Expand Up @@ -729,6 +745,9 @@ func flattenConstraints(c *sdk.NodetemplatesV1TemplateConstraints) ([]map[string
if c.CpuManufacturers != nil {
out[FieldNodeTemplateCPUManufacturers] = lo.FromPtr(c.CpuManufacturers)
}
if c.ArchitecturePriority != nil {
out[FieldNodeTemplateArchitecturePriority] = lo.FromPtr(c.ArchitecturePriority)
}
setStateConstraintValue(c.Burstable, FieldNodeTemplateBurstableInstances, out)
setStateConstraintValue(c.CustomerSpecific, FieldNodeTemplateCustomerSpecific, out)
return []map[string]any{out}, nil
Expand Down Expand Up @@ -1310,6 +1329,10 @@ func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConst
}))
}

if v, ok := obj[FieldNodeTemplateArchitecturePriority].([]any); ok {
out.ArchitecturePriority = toPtr(toStringList(v))
}

return out
}

Expand Down
12 changes: 11 additions & 1 deletion castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
]
}
],
"cpuManufacturers": ["INTEL", "AMD"]
"cpuManufacturers": ["INTEL", "AMD"],
"architecturePriority": ["amd64", "arm64"]
},
"version": "3",
"shouldTaint": true,
Expand Down Expand Up @@ -153,6 +154,9 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
cluster_id = b6bfc074-a267-400f-b8f1-db0850c369b1
configuration_id = 7dc4f922-29c9-4377-889c-0c8c5fb8d497
constraints.# = 1
constraints.0.architecture_priority.# = 2
constraints.0.architecture_priority.0 = amd64
constraints.0.architecture_priority.1 = arm64
constraints.0.architectures.# = 2
constraints.0.architectures.0 = amd64
constraints.0.architectures.1 = arm64
Expand Down Expand Up @@ -541,6 +545,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.architecture_priority.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architecture_priority.0", "amd64"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.0", "linux"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.azs.#", "2"),
Expand Down Expand Up @@ -602,6 +608,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.architecture_priority.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.architecture_priority.0", "arm64"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.#", "1"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.os.0", "linux"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.azs.#", "3"),
Expand Down Expand Up @@ -708,6 +716,7 @@ func testAccNodeTemplateConfig(rName, clusterName string) string {
}
cpu_manufacturers = ["INTEL", "AMD"]
architecture_priority = ["amd64"]
}
}
`, rName))
Expand Down Expand Up @@ -764,6 +773,7 @@ func testNodeTemplateUpdated(rName, clusterName string) string {
}
cpu_manufacturers = ["INTEL", "AMD"]
architecture_priority = ["arm64"]
}
}
`, rName))
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 8dc5255

Please sign in to comment.