Skip to content

Commit 040d2ec

Browse files
author
Furkhat Kasymov Genii Uulu
committed
Add GKE max_pods_per_node_formula field
1 parent 5615556 commit 040d2ec

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

castai/resource_node_configuration.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ const (
6767
aksDiskCacheReadWrite = "ReadWrite"
6868
)
6969

70+
const (
71+
nodeConfigurationGKEMaxPodsPerNodeDefault = 110
72+
)
73+
7074
func resourceNodeConfiguration() *schema.Resource {
7175
return &schema.Resource{
7276
CreateContext: resourceNodeConfigurationCreate,
@@ -508,15 +512,26 @@ func resourceNodeConfiguration() *schema.Resource {
508512
FieldNodeConfigurationGKE: {
509513
Type: schema.TypeList,
510514
Optional: true,
515+
Computed: true,
511516
MaxItems: 1,
512517
Elem: &schema.Resource{
513518
Schema: map[string]*schema.Schema{
519+
"max_pods_per_node_formula": {
520+
Type: schema.TypeString,
521+
Optional: true,
522+
Description: `This is an advanced configuration field. In general, we recommend using max_pods_per_node instead.
523+
This field accepts a formula to calculate the maximum number of pods that can run on a node. This will affect the pod CIDR range that the node reserves. The following variables are available for use in the formula and will be bound to numeric values before evaluation:
524+
525+
NUM_CPU - Number of CPUs available on the node
526+
NUM_RAM_GB - Amount of RAM in gigabytes available on the node`,
527+
ConflictsWith: []string{FieldNodeConfigurationGKE + ".0.max_pods_per_node"},
528+
},
514529
"max_pods_per_node": {
515530
Type: schema.TypeInt,
516-
Default: 110,
517531
Optional: true,
518532
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(10, 256)),
519533
Description: "Maximum number of pods that can be run on a node, which affects how many IP addresses you will need for each node. Defaults to 110",
534+
ConflictsWith: []string{FieldNodeConfigurationGKE + ".0.max_pods_per_node_formula"},
520535
},
521536
"network_tags": {
522537
Type: schema.TypeList,
@@ -617,10 +632,37 @@ func resourceNodeConfiguration() *schema.Resource {
617632
},
618633
},
619634
},
620-
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
635+
CustomizeDiff: customizeGKEMaxPodsField,
636+
}
637+
}
638+
639+
func customizeGKEMaxPodsField(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
640+
if v, ok := d.GetOk(FieldNodeConfigurationGKE); ok {
641+
gkeList := v.([]interface{})
642+
gke := gkeList[0].(map[string]interface{})
643+
644+
static, formula := gke["max_pods_per_node"], gke["max_pods_per_node_formula"]
645+
staticNotSet := static == nil || static.(int) == 0
646+
formulaNotSet := formula == nil || formula.(string) == ""
647+
if staticNotSet && formulaNotSet {
648+
gke["max_pods_per_node"] = nodeConfigurationGKEMaxPodsPerNodeDefault
649+
gkeList = []interface{}{gke}
650+
if err := d.SetNew(FieldNodeConfigurationGKE, gkeList); err != nil {
651+
return err
652+
}
621653
return nil
622-
},
654+
}
655+
if !formulaNotSet {
656+
gke["max_pods_per_node"] = nil
657+
gkeList = []interface{}{gke}
658+
if err := d.SetNew(FieldNodeConfigurationGKE, gkeList); err != nil {
659+
return err
660+
}
661+
return nil
662+
663+
}
623664
}
665+
return nil
624666
}
625667

626668
func resourceNodeConfigurationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -1491,7 +1533,10 @@ func toGKEConfig(obj map[string]interface{}) *sdk.NodeconfigV1GKEConfig {
14911533
}
14921534

14931535
out := &sdk.NodeconfigV1GKEConfig{}
1494-
if v, ok := obj["max_pods_per_node"].(int); ok {
1536+
if v, ok := obj["max_pods_per_node_formula"].(string); ok && len(v) > 0 {
1537+
out.MaxPodsPerNodeFormula = toPtr(v)
1538+
}
1539+
if v, ok := obj["max_pods_per_node"].(int); ok && v != 0 {
14951540
out.MaxPodsPerNode = toPtr(int32(v))
14961541
}
14971542
if v, ok := obj["network_tags"].([]interface{}); ok {
@@ -1597,6 +1642,9 @@ func flattenGKEConfig(config *sdk.NodeconfigV1GKEConfig) []map[string]interface{
15971642
return nil
15981643
}
15991644
m := map[string]interface{}{}
1645+
if v := config.MaxPodsPerNodeFormula; v != nil {
1646+
m["max_pods_per_node_formula"] = *config.MaxPodsPerNodeFormula
1647+
}
16001648
if v := config.MaxPodsPerNode; v != nil {
16011649
m["max_pods_per_node"] = *config.MaxPodsPerNode
16021650
}

castai/resource_node_configuration_gke_test.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,45 @@ func TestAccGKE_ResourceNodeConfiguration(t *testing.T) {
3737
resource.TestCheckResourceAttr(resourceName, "eks.#", "0"),
3838
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
3939
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node", "31"),
40+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node_formula", ""),
41+
resource.TestCheckResourceAttr(resourceName, "gke.0.disk_type", "pd-balanced"),
42+
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.0", "ab"),
43+
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.1", "bc"),
44+
resource.TestCheckResourceAttr(resourceName, "gke.0.zones.#", "0"),
45+
resource.TestCheckResourceAttr(resourceName, "gke.0.on_host_maintenance", "MIGRATE"),
46+
),
47+
},
48+
{
49+
Config: testAccGKENodeConfigurationConfigWithoutMaxPods(rName, clusterName, projectID),
50+
Check: resource.ComposeTestCheckFunc(
51+
resource.TestCheckResourceAttr(resourceName, "name", rName),
52+
resource.TestCheckResourceAttr(resourceName, "disk_cpu_ratio", "35"),
53+
resource.TestCheckResourceAttr(resourceName, "drain_timeout_sec", "10"),
54+
resource.TestCheckResourceAttr(resourceName, "min_disk_size", "122"),
55+
resource.TestCheckResourceAttr(resourceName, "aks.#", "0"),
56+
resource.TestCheckResourceAttr(resourceName, "eks.#", "0"),
57+
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
58+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node", "110"),
59+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node_formula", ""),
60+
resource.TestCheckResourceAttr(resourceName, "gke.0.disk_type", "pd-balanced"),
61+
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.0", "ab"),
62+
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.1", "bc"),
63+
resource.TestCheckResourceAttr(resourceName, "gke.0.zones.#", "0"),
64+
resource.TestCheckResourceAttr(resourceName, "gke.0.on_host_maintenance", "MIGRATE"),
65+
),
66+
},
67+
{
68+
Config: testAccGKENodeConfigurationConfigWithMaxPodsFormula(rName, clusterName, projectID),
69+
Check: resource.ComposeTestCheckFunc(
70+
resource.TestCheckResourceAttr(resourceName, "name", rName),
71+
resource.TestCheckResourceAttr(resourceName, "disk_cpu_ratio", "35"),
72+
resource.TestCheckResourceAttr(resourceName, "drain_timeout_sec", "10"),
73+
resource.TestCheckResourceAttr(resourceName, "min_disk_size", "122"),
74+
resource.TestCheckResourceAttr(resourceName, "aks.#", "0"),
75+
resource.TestCheckResourceAttr(resourceName, "eks.#", "0"),
76+
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
77+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node", "0"),
78+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node_formula", "NUM_CPU"),
4079
resource.TestCheckResourceAttr(resourceName, "gke.0.disk_type", "pd-balanced"),
4180
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.0", "ab"),
4281
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.1", "bc"),
@@ -53,6 +92,7 @@ func TestAccGKE_ResourceNodeConfiguration(t *testing.T) {
5392
resource.TestCheckResourceAttr(resourceName, "eks.#", "0"),
5493
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
5594
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node", "32"),
95+
resource.TestCheckResourceAttr(resourceName, "gke.0.max_pods_per_node_formula", ""),
5696
resource.TestCheckResourceAttr(resourceName, "gke.0.disk_type", "pd-ssd"),
5797
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.0", "bb"),
5898
resource.TestCheckResourceAttr(resourceName, "gke.0.network_tags.1", "dd"),
@@ -81,6 +121,35 @@ func TestAccGKE_ResourceNodeConfiguration(t *testing.T) {
81121
}
82122

83123
func testAccGKENodeConfigurationConfig(rName, clusterName, projectID string) string {
124+
const gkeParams = `
125+
max_pods_per_node = 31
126+
network_tags = ["ab", "bc"]
127+
disk_type = "pd-balanced"
128+
on_host_maintenance = "MIGRATE"
129+
`
130+
return testAccGKENodeConfigurationConfigWithGKEConfig(rName, clusterName, projectID, gkeParams)
131+
}
132+
133+
func testAccGKENodeConfigurationConfigWithoutMaxPods(rName, clusterName, projectID string) string {
134+
const gkeParams = `
135+
network_tags = ["ab", "bc"]
136+
disk_type = "pd-balanced"
137+
on_host_maintenance = "MIGRATE"
138+
`
139+
return testAccGKENodeConfigurationConfigWithGKEConfig(rName, clusterName, projectID, gkeParams)
140+
}
141+
142+
func testAccGKENodeConfigurationConfigWithMaxPodsFormula(rName, clusterName, projectID string) string {
143+
const gkeParams = `
144+
max_pods_per_node_formula = "NUM_CPU"
145+
network_tags = ["ab", "bc"]
146+
disk_type = "pd-balanced"
147+
on_host_maintenance = "MIGRATE"
148+
`
149+
return testAccGKENodeConfigurationConfigWithGKEConfig(rName, clusterName, projectID, gkeParams)
150+
}
151+
152+
func testAccGKENodeConfigurationConfigWithGKEConfig(rName, clusterName, projectID, gkeParams string) string {
84153
return ConfigCompose(testAccGKEClusterConfig(rName, clusterName, projectID), fmt.Sprintf(`
85154
resource "castai_node_configuration" "test" {
86155
name = %[1]q
@@ -93,18 +162,15 @@ resource "castai_node_configuration" "test" {
93162
env = "development"
94163
}
95164
gke {
96-
max_pods_per_node = 31
97-
network_tags = ["ab", "bc"]
98-
disk_type = "pd-balanced"
99-
on_host_maintenance = "MIGRATE"
165+
%s
100166
}
101167
}
102168
103169
resource "castai_node_configuration_default" "test" {
104170
cluster_id = castai_gke_cluster.test.id
105171
configuration_id = castai_node_configuration.test.id
106172
}
107-
`, rName))
173+
`, rName, gkeParams))
108174
}
109175

110176
func testAccGKENodeConfigurationUpdated(rName, clusterName, projectID string) string {

docs/resources/node_configuration.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)