Skip to content

Commit

Permalink
feat: add support for setting node affinity selectors for dedicated n… (
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenorgate authored Apr 11, 2024
1 parent 269ff59 commit 02c93f0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
71 changes: 71 additions & 0 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const (
FieldNodeTemplateDedicatedNodeAffinity = "dedicated_node_affinity"
FieldNodeTemplateAzName = "az_name"
FieldNodeTemplateInstanceTypes = "instance_types"
FieldNodeTemplateAffinityName = "affinity"
FieldNodeTemplateAffinityKeyName = "key"
FieldNodeTemplateAffinityOperatorName = "operator"
FieldNodeTemplateAffinityValuesName = "values"
)

const (
Expand All @@ -73,9 +77,19 @@ const (
OsWindows = "windows"
)

const (
NodeSelectorOperationIn = "In"
NodeSelectorOperationNotIn = "NotIn"
NodeSelectorOperationExists = "Exists"
NodeSelectorOperationDoesNot = "DoesNotExist"
NodeSelectorOperationGt = "Gt"
NodeSelectorOperationLt = "Lt"
)

func resourceNodeTemplate() *schema.Resource {
supportedArchitectures := []string{ArchAMD64, ArchARM64}
supportedOs := []string{OsLinux, OsWindows}
supportedSelectorOperations := []string{NodeSelectorOperationIn, NodeSelectorOperationNotIn, NodeSelectorOperationExists, NodeSelectorOperationDoesNot, NodeSelectorOperationGt, NodeSelectorOperationLt}

return &schema.Resource{
CreateContext: resourceNodeTemplateCreate,
Expand Down Expand Up @@ -372,6 +386,33 @@ func resourceNodeTemplate() *schema.Resource {
Type: schema.TypeString,
Description: "Name of node group.",
},
FieldNodeTemplateAffinityName: {
Optional: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
FieldNodeTemplateAffinityKeyName: {
Required: true,
Type: schema.TypeString,
Description: "Key of the node affinity selector.",
},
FieldNodeTemplateAffinityOperatorName: {
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(supportedSelectorOperations, false)),
Description: fmt.Sprintf("Operator of the node affinity selector. Allowed values: %s.", strings.Join(supportedSelectorOperations, ", ")),
},
FieldNodeTemplateAffinityValuesName: {
Required: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Values of the node affinity selector.",
},
},
},
},
},
},
},
Expand Down Expand Up @@ -638,6 +679,17 @@ func flattenNodeAffinity(affinities []sdk.NodetemplatesV1TemplateConstraintsDedi

result[FieldNodeTemplateName] = lo.FromPtr(item.Name)
result[FieldNodeTemplateAzName] = lo.FromPtr(item.AzName)

if item.Affinity != nil && len(*item.Affinity) > 0 {
result[FieldNodeTemplateAffinityName] = lo.Map(*item.Affinity, func(affinity sdk.K8sSelectorV1KubernetesNodeAffinity, index int) map[string]any {
return map[string]any{
FieldNodeTemplateAffinityKeyName: affinity.Key,
FieldNodeTemplateAffinityOperatorName: affinity.Operator,
FieldNodeTemplateAffinityValuesName: affinity.Values,
}
})
}

return result
})
}
Expand Down Expand Up @@ -1160,6 +1212,25 @@ func toTemplateConstraintsNodeAffinity(o map[string]any) *sdk.NodetemplatesV1Tem
if v, ok := o[FieldNodeTemplateInstanceTypes].([]any); ok {
out.InstanceTypes = toPtr(toStringList(v))
}
if v, ok := o[FieldNodeTemplateAffinityName].([]any); ok {
out.Affinity = toPtr(lo.Map(v, func(item any, _ int) sdk.K8sSelectorV1KubernetesNodeAffinity {
val, ok := item.(map[string]any)
if !ok {
return sdk.K8sSelectorV1KubernetesNodeAffinity{}
}
out := sdk.K8sSelectorV1KubernetesNodeAffinity{}
if v, ok := val[FieldNodeTemplateAffinityKeyName].(string); ok {
out.Key = v
}
if v, ok := val[FieldNodeTemplateAffinityOperatorName].(string); ok {
out.Operator = sdk.K8sSelectorV1Operator(v)
}
if v, ok := val[FieldNodeTemplateAffinityValuesName].([]any); ok {
out.Values = toStringList(v)
}
return out
}))
}

return &out
}
14 changes: 13 additions & 1 deletion castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ func TestNodeTemplateResourceReadContext(t *testing.T) {
{
"name": "foo",
"azName": "eu-central-1a",
"instanceTypes": ["m5.24xlarge"]
"instanceTypes": ["m5.24xlarge"],
"affinity": [
{
"key": "gke.io/gcp-nodepool",
"operator": "In",
"values": ["foo"]
}
]
}
]
},
Expand Down Expand Up @@ -179,6 +186,11 @@ constraints.0.max_memory = 0
constraints.0.min_cpu = 10
constraints.0.min_memory = 0
constraints.0.dedicated_node_affinity.# = 1
constraints.0.dedicated_node_affinity.0.affinity.# = 1
constraints.0.dedicated_node_affinity.0.affinity.0.key = gke.io/gcp-nodepool
constraints.0.dedicated_node_affinity.0.affinity.0.operator = In
constraints.0.dedicated_node_affinity.0.affinity.0.values.# = 1
constraints.0.dedicated_node_affinity.0.affinity.0.values.0 = foo
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
Expand Down
37 changes: 24 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.

14 changes: 14 additions & 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 02c93f0

Please sign in to comment.