Skip to content

Commit

Permalink
KUBE-328: Support setting image family on node configuration (#344)
Browse files Browse the repository at this point in the history
* Add ImageFamily field to terraform

* Move image family to eks config

* Generate docs

* Add test

* Fix test reference and add update test
  • Loading branch information
Tsonov authored Jun 28, 2024
1 parent 0193bab commit 3c9371a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
56 changes: 55 additions & 1 deletion castai/resource_node_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const (
FieldNodeConfigurationKOPS = "kops"
FieldNodeConfigurationGKE = "gke"
FieldNodeConfigurationEKSTargetGroup = "target_group"
FieldNodeConfigurationEKSImageFamily = "eks_image_family"
)

const (
eksImageFamilyAL2 = "al2"
eksImageFamilyAL2023 = "al2023"
eksImageFamilyBottlerocket = "bottlerocket"
)

func resourceNodeConfiguration() *schema.Resource {
Expand Down Expand Up @@ -111,7 +118,7 @@ func resourceNodeConfiguration() *schema.Resource {
FieldNodeConfigurationImage: {
Type: schema.TypeString,
Optional: true,
Description: "Image to be used while provisioning the node. If nothing is provided will be resolved to latest available image based on Kubernetes version if possible ",
Description: "Image to be used while provisioning the node. If nothing is provided will be resolved to latest available image based on Image family, Kubernetes version and node architecture if possible. See Cast.ai documentation for details.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace),
},
FieldNodeConfigurationTags: {
Expand Down Expand Up @@ -233,6 +240,15 @@ func resourceNodeConfiguration() *schema.Resource {
Description: "Number of IPs per prefix to be used for calculating max pods.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 256)),
},
FieldNodeConfigurationEKSImageFamily: {
Type: schema.TypeString,
Optional: true,
Description: "Image OS Family to use when provisioning node. If both image and family are provided, the system will use provided image and provisioning logic for given family. If only image family is provided, the system will attempt to resolve the latest image from that family based on kubernetes version and node architecture. If image family is omitted, a default family (based on cloud provider) will be used. See Cast.ai documentation for details.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{eksImageFamilyAL2, eksImageFamilyAL2023, eksImageFamilyBottlerocket}, true)),
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
return strings.EqualFold(oldValue, newValue)
},
},
FieldNodeConfigurationEKSTargetGroup: {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -678,9 +694,30 @@ func toEKSConfig(obj map[string]interface{}) *sdk.NodeconfigV1EKSConfig {
}
}

if v, ok := obj[FieldNodeConfigurationEKSImageFamily].(string); ok {
out.ImageFamily = toEKSImageFamily(v)
}

return out
}

func toEKSImageFamily(v string) *sdk.NodeconfigV1EKSConfigImageFamily {
if v == "" {
return nil
}

switch strings.ToLower(v) {
case eksImageFamilyAL2:
return lo.ToPtr(sdk.FAMILYAL2)
case eksImageFamilyAL2023:
return lo.ToPtr(sdk.FAMILYAL2023)
case eksImageFamilyBottlerocket:
return lo.ToPtr(sdk.FAMILYBOTTLEROCKET)
default:
return nil
}
}

func flattenEKSConfig(config *sdk.NodeconfigV1EKSConfig) []map[string]interface{} {
if config == nil {
return nil
Expand Down Expand Up @@ -745,9 +782,26 @@ func flattenEKSConfig(config *sdk.NodeconfigV1EKSConfig) []map[string]interface{
}
}

if v := config.ImageFamily; v != nil {
m[FieldNodeConfigurationEKSImageFamily] = fromEKSImageFamily(*v)
}

return []map[string]interface{}{m}
}

func fromEKSImageFamily(family sdk.NodeconfigV1EKSConfigImageFamily) string {
switch family {
case sdk.FAMILYBOTTLEROCKET, sdk.FamilyBottlerocket:
return eksImageFamilyBottlerocket
case sdk.FAMILYAL2, sdk.FamilyAl2:
return eksImageFamilyAL2
case sdk.FAMILYAL2023, sdk.FamilyAl2023:
return eksImageFamilyAL2023
default:
return ""
}
}

func toKOPSConfig(obj map[string]interface{}) *sdk.NodeconfigV1KOPSConfig {
if obj == nil {
return nil
Expand Down
4 changes: 4 additions & 0 deletions castai/resource_node_configuration_eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "eks.0.target_group.#", "1"),
resource.TestCheckResourceAttr(resourceName, "eks.0.max_pods_per_node_formula", "NUM_IP_PER_PREFIX-NUM_MAX_NET_INTERFACES"),
resource.TestCheckResourceAttr(resourceName, "eks.0.ips_per_prefix", "4"),
resource.TestCheckResourceAttr(resourceName, "eks.0.eks_image_family", "al2023"),
resource.TestCheckResourceAttr(resourceName, "eks.0.target_group.0.arn", "arn:aws:test"),
resource.TestCheckResourceAttr(resourceName, "aks.#", "0"),
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "eks.0.volume_throughput", "130"),
resource.TestCheckResourceAttr(resourceName, "eks.0.max_pods_per_node_formula", "NUM_IP_PER_PREFIX+NUM_MAX_NET_INTERFACES"),
resource.TestCheckResourceAttr(resourceName, "eks.0.ips_per_prefix", "3"),
resource.TestCheckResourceAttr(resourceName, "eks.0.eks_image_family", "al2"),
resource.TestCheckResourceAttr(resourceName, "eks.0.target_group.#", "1"),
resource.TestCheckResourceAttr(resourceName, "eks.0.target_group.0.arn", "arn:aws:test2"),
resource.TestCheckResourceAttr(resourceName, "eks.0.target_group.0.port", "80"),
Expand Down Expand Up @@ -138,6 +140,7 @@ resource "castai_node_configuration" "test" {
imds_hop_limit = 3
max_pods_per_node_formula = "NUM_IP_PER_PREFIX-NUM_MAX_NET_INTERFACES"
ips_per_prefix = 4
eks_image_family = "al2023"
target_group {
arn = "arn:aws:test"
}
Expand Down Expand Up @@ -169,6 +172,7 @@ resource "castai_node_configuration" "test" {
volume_throughput = 130
max_pods_per_node_formula = "NUM_IP_PER_PREFIX+NUM_MAX_NET_INTERFACES"
ips_per_prefix = 3
eks_image_family = "al2"
target_group {
arn = "arn:aws:test2"
port = 80
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/node_configuration.md

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

0 comments on commit 3c9371a

Please sign in to comment.