Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for setting node affinity selectors for dedicated n… #291

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion 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 @@ -368,10 +382,37 @@ func resourceNodeTemplate() *schema.Resource {
Description: "Availability zone name.",
},
FieldNodeTemplateName: {
Required: true,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you don't need to provide the dedicated node group name if you are using the selectors

Type: schema.TypeString,
Description: "Name of node group.",
},
FieldNodeTemplateAffinityName: {
mikenorgate marked this conversation as resolved.
Show resolved Hide resolved
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
}
1 change: 1 addition & 0 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ 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.# = 0
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.

Loading