Skip to content

Commit

Permalink
feat: Rename node affinity to dedicated node affinity in Node Template
Browse files Browse the repository at this point in the history
  • Loading branch information
jansyk13 committed Mar 19, 2024
1 parent 381b3a5 commit 7cc8b58
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 47 deletions.
31 changes: 16 additions & 15 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
FieldNodeTemplateStorageOptimized = "storage_optimized"
FieldNodeTemplateUseSpotFallbacks = "use_spot_fallbacks"
FieldNodeTemplateCustomPriority = "custom_priority"
FieldNodeTemplateNodeAffinity = "node_affinity"
FieldNodeTemplateDedicatedNodeAffinity = "dedicated_node_affinity"
FieldNodeTemplateAzName = "az_name"
FieldNodeTemplateInstanceTypes = "instance_types"
)
Expand Down Expand Up @@ -348,9 +348,10 @@ func resourceNodeTemplate() *schema.Resource {
},
},
},
FieldNodeTemplateNodeAffinity: {
Type: schema.TypeList,
Optional: true,
FieldNodeTemplateDedicatedNodeAffinity: {
Type: schema.TypeList,
Optional: true,
Description: "Dedicated node affinity - creates preference for instances to be created on sole tenancy or dedicated nodes. This\n feature is only available for GCP clusters, requires feature flag to be enabled and sole tenancy nodes with local\n SSDs or GPUs are not supported. If the sole tenancy or dedicated nodes don't have capacity for selected instance\n type, the Autoscaler will fall back to multi-tenant instance types available for this Node Template.\n Other instance constraints are applied when the Autoscaler picks available instance types that can be created on\n the sole tenancy or dedicated node (example: setting min CPU to 16).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
FieldNodeTemplateInstanceTypes: {
Expand All @@ -359,7 +360,7 @@ func resourceNodeTemplate() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Instance types in this node group.",
Description: "Instance/node types in this node group.",
},
FieldNodeTemplateAzName: {
Required: true,
Expand Down Expand Up @@ -518,8 +519,8 @@ func flattenConstraints(c *sdk.NodetemplatesV1TemplateConstraints) ([]map[string
if c.CustomPriority != nil && len(*c.CustomPriority) > 0 {
out[FieldNodeTemplateCustomPriority] = flattenCustomPriority(*c.CustomPriority)
}
if c.NodeAffinity != nil && len(*c.NodeAffinity) > 0 {
out[FieldNodeTemplateNodeAffinity] = flattenNodeAffinity(*c.NodeAffinity)
if c.DedicatedNodeAffinity != nil && len(*c.DedicatedNodeAffinity) > 0 {
out[FieldNodeTemplateDedicatedNodeAffinity] = flattenNodeAffinity(*c.DedicatedNodeAffinity)
}
if c.InstanceFamilies != nil {
out[FieldNodeTemplateInstanceFamilies] = flattenInstanceFamilies(c.InstanceFamilies)
Expand Down Expand Up @@ -628,8 +629,8 @@ func flattenCustomPriority(priorities []sdk.NodetemplatesV1TemplateConstraintsCu
})
}

func flattenNodeAffinity(affinities []sdk.NodetemplatesV1TemplateConstraintsNodeAffinity) any {
return lo.Map(affinities, func(item sdk.NodetemplatesV1TemplateConstraintsNodeAffinity, index int) map[string]any {
func flattenNodeAffinity(affinities []sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity) any {
return lo.Map(affinities, func(item sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity, index int) map[string]any {
result := map[string]any{}
if item.InstanceTypes != nil {
result[FieldNodeTemplateInstanceTypes] = *item.InstanceTypes
Expand Down Expand Up @@ -1064,16 +1065,16 @@ func toTemplateConstraints(obj map[string]any) *sdk.NodetemplatesV1TemplateConst
}))
}
}
if v, ok := obj[FieldNodeTemplateNodeAffinity].([]any); ok && len(v) > 0 {
if v, ok := obj[FieldNodeTemplateDedicatedNodeAffinity].([]any); ok && len(v) > 0 {
if ok {
out.NodeAffinity = lo.ToPtr(lo.FilterMap(v, func(item any, _ int) (sdk.NodetemplatesV1TemplateConstraintsNodeAffinity, bool) {
out.DedicatedNodeAffinity = lo.ToPtr(lo.FilterMap(v, func(item any, _ int) (sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity, bool) {
val, ok := item.(map[string]any)
if !ok {
return sdk.NodetemplatesV1TemplateConstraintsNodeAffinity{}, false
return sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity{}, false
}
res := toTemplateConstraintsNodeAffinity(val)
if res == nil {
return sdk.NodetemplatesV1TemplateConstraintsNodeAffinity{}, false
return sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity{}, false
}
return *res, true
}))
Expand Down Expand Up @@ -1144,12 +1145,12 @@ func toTemplateConstraintsCustomPriority(o map[string]any) *sdk.NodetemplatesV1T
return &out
}

func toTemplateConstraintsNodeAffinity(o map[string]any) *sdk.NodetemplatesV1TemplateConstraintsNodeAffinity {
func toTemplateConstraintsNodeAffinity(o map[string]any) *sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity {
if o == nil {
return nil
}

out := sdk.NodetemplatesV1TemplateConstraintsNodeAffinity{}
out := sdk.NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity{}
if v, ok := o[FieldNodeTemplateName].(string); ok {
out.Name = toPtr(v)
}
Expand Down
16 changes: 8 additions & 8 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
"onDemand": true
}
],
"nodeAffinity": [
"dedicatedNodeAffinity": [
{
"name": "foo",
"azName": "eu-central-1a",
Expand Down Expand Up @@ -178,11 +178,11 @@ constraints.0.max_cpu = 10000
constraints.0.max_memory = 0
constraints.0.min_cpu = 10
constraints.0.min_memory = 0
constraints.0.node_affinity.# = 1
constraints.0.node_affinity.0.az_name = eu-central-1a
constraints.0.node_affinity.0.instance_types.# = 1
constraints.0.node_affinity.0.instance_types.0 = m5.24xlarge
constraints.0.node_affinity.0.name = foo
constraints.0.dedicated_node_affinity.# = 1
constraints.0.dedicated_node_affinity.0.az_name = eu-central-1a
constraints.0.dedicated_node_affinity.0.instance_types.# = 1
constraints.0.dedicated_node_affinity.0.instance_types.0 = m5.24xlarge
constraints.0.dedicated_node_affinity.0.name = foo
constraints.0.on_demand = true
constraints.0.os.# = 1
constraints.0.os.0 = linux
Expand Down Expand Up @@ -446,7 +446,7 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.0.instance_families.1", "d"),
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.node_affinity.#", "0"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.dedicated_node_affinity.#", "0"),
),
},
{
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "constraints.0.custom_priority.1.instance_families.1", "d"),
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.node_affinity.#", "0"),
resource.TestCheckResourceAttr(resourceName, "constraints.0.dedicated_node_affinity.#", "0"),
),
},
},
Expand Down
40 changes: 27 additions & 13 deletions castai/sdk/api.gen.go

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

117 changes: 117 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.

Loading

0 comments on commit 7cc8b58

Please sign in to comment.