From 6521822b47470e843c939a4b93bc919142fef597 Mon Sep 17 00:00:00 2001 From: Marcin Kaciuba Date: Mon, 10 Jun 2024 03:59:01 -0700 Subject: [PATCH] KUBE-304: node configuration eks max pods formula (#324) * KUBE-304: node configuration eks max pods formula * fix test * regenerate doc * fix test * fix test * fix test --- Makefile | 1 + castai/resource_node_configuration.go | 28 +++++++++++++++++++ .../resource_node_configuration_eks_test.go | 8 ++++++ docs/resources/node_configuration.md | 2 ++ 4 files changed, 39 insertions(+) diff --git a/Makefile b/Makefile index efd092b5..f6163175 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ generate-all: generate-sdk generate-docs .PHONY: build build: init-examples build: generate-sdk +build: generate-docs build: @echo "==> Building terraform-provider-castai" go build diff --git a/castai/resource_node_configuration.go b/castai/resource_node_configuration.go index 8d705d6a..a928440f 100644 --- a/castai/resource_node_configuration.go +++ b/castai/resource_node_configuration.go @@ -221,6 +221,18 @@ func resourceNodeConfiguration() *schema.Resource { Description: "AWS KMS key ARN for encrypting EBS volume attached to the node", ValidateDiagFunc: validation.ToDiagFunc(validation.StringMatch(regexp.MustCompile(`arn:aws:kms:.*`), "Must be a valid KMS key ARN")), }, + "max_pods_per_node_formula": { + Type: schema.TypeString, + Optional: true, + Description: "Formula to calculate the maximum number of pods that can be run on a node.", + }, + "ips_per_prefix": { + Type: schema.TypeInt, + Optional: true, + Default: nil, + Description: "Number of IPs per prefix to be used for calculating max pods.", + ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 256)), + }, FieldNodeConfigurationEKSTargetGroup: { Type: schema.TypeList, Optional: true, @@ -646,6 +658,14 @@ func toEKSConfig(obj map[string]interface{}) *sdk.NodeconfigV1EKSConfig { out.VolumeKmsKeyArn = toPtr(v) } + if v, ok := obj["max_pods_per_node_formula"].(string); ok && v != "" { + out.MaxPodsPerNodeFormula = toPtr(v) + } + + if v, ok := obj["ips_per_prefix"].(int); ok && v != 0 { + out.IpsPerPrefix = toPtr(int32(v)) + } + if v, ok := obj[FieldNodeConfigurationEKSTargetGroup].([]interface{}); ok && len(v) > 0 && v[0] != nil { if e, ok := v[0].(map[string]interface{}); ok { out.TargetGroup = &sdk.NodeconfigV1TargetGroup{} @@ -698,6 +718,14 @@ func flattenEKSConfig(config *sdk.NodeconfigV1EKSConfig) []map[string]interface{ m["volume_kms_key_arn"] = toString(config.VolumeKmsKeyArn) } + if v := config.MaxPodsPerNodeFormula; v != nil { + m["max_pods_per_node_formula"] = toString(config.MaxPodsPerNodeFormula) + } + + if v := config.IpsPerPrefix; v != nil { + m["ips_per_prefix"] = *config.IpsPerPrefix + } + if v := config.TargetGroup; v != nil { if v.Arn != nil { if v.Port != nil { diff --git a/castai/resource_node_configuration_eks_test.go b/castai/resource_node_configuration_eks_test.go index e079afcc..73b5e45c 100644 --- a/castai/resource_node_configuration_eks_test.go +++ b/castai/resource_node_configuration_eks_test.go @@ -48,6 +48,8 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "eks.0.imds_hop_limit", "3"), resource.TestCheckResourceAttr(resourceName, "eks.0.volume_kms_key_arn", "arn:aws:kms:eu-central-1:012345:key/1d989ee1-59cd-4238-8018-79bae29d1109"), 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.target_group.0.arn", "arn:aws:test"), resource.TestCheckResourceAttr(resourceName, "aks.#", "0"), resource.TestCheckResourceAttr(resourceName, "kops.#", "0"), @@ -78,6 +80,8 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "eks.0.dns_cluster_ip", ""), resource.TestCheckResourceAttr(resourceName, "eks.0.security_groups.#", "1"), 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.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"), @@ -132,6 +136,8 @@ resource "castai_node_configuration" "test" { volume_kms_key_arn = "arn:aws:kms:eu-central-1:012345:key/1d989ee1-59cd-4238-8018-79bae29d1109" imds_v1 = true imds_hop_limit = 3 + max_pods_per_node_formula = "NUM_IP_PER_PREFIX-NUM_MAX_NET_INTERFACES" + ips_per_prefix = 4 target_group { arn = "arn:aws:test" } @@ -161,6 +167,8 @@ resource "castai_node_configuration" "test" { instance_profile_arn = aws_iam_instance_profile.test.arn security_groups = [aws_security_group.test.id] volume_throughput = 130 + max_pods_per_node_formula = "NUM_IP_PER_PREFIX+NUM_MAX_NET_INTERFACES" + ips_per_prefix = 3 target_group { arn = "arn:aws:test2" port = 80 diff --git a/docs/resources/node_configuration.md b/docs/resources/node_configuration.md index c59e5a6d..c4175d9f 100644 --- a/docs/resources/node_configuration.md +++ b/docs/resources/node_configuration.md @@ -104,7 +104,9 @@ Optional: - `dns_cluster_ip` (String) IP address to use for DNS queries within the cluster - `imds_hop_limit` (Number) Allow configure the IMDSv2 hop limit, the default is 2 - `imds_v1` (Boolean) When the value is true both IMDSv1 and IMDSv2 are enabled. Setting the value to false disables permanently IMDSv1 and might affect legacy workloads running on the node created with this configuration. The default is true if the flag isn't provided +- `ips_per_prefix` (Number) Number of IPs per prefix to be used for calculating max pods. - `key_pair_id` (String) AWS key pair ID to be used for CAST provisioned nodes. Has priority over ssh_public_key +- `max_pods_per_node_formula` (String) Formula to calculate the maximum number of pods that can be run on a node. - `target_group` (Block List, Max: 1) AWS target group configuration for CAST provisioned nodes (see [below for nested schema](#nestedblock--eks--target_group)) - `volume_iops` (Number) AWS EBS volume IOPS to be used for CAST provisioned nodes - `volume_kms_key_arn` (String) AWS KMS key ARN for encrypting EBS volume attached to the node