Skip to content

Commit

Permalink
azurerm_kubernetes_cluster: Cycle node-pool if node_pool has node lev…
Browse files Browse the repository at this point in the history
…el relevant changes (#21719)

* azurerm_kubernetes_cluster: Cycle node-pool if node_pool has node level relevant changes

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* azurerm_kubernetes_cluster: Remove ForceNew in the schema

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Update website/docs/r/kubernetes_cluster.html.markdown

Co-authored-by: stephybun <[email protected]>

* azurerm_kubernetes_cluster: slim the docs

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Add test for changing the OS Disk size

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Add test for changing the OS Disk size

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Add test for os sku

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Update internal/services/containers/kubernetes_cluster_scaling_resource_test.go

Co-authored-by: stephybun <[email protected]>

* Add test for os sku

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Add cycle support for more properties

* Add test for os sku

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* Add cycle support for more properties

* Refactor property condition

* Apply suggestions from code review

Co-authored-by: stephybun <[email protected]>

* Apply suggestions from code review

Co-authored-by: stephybun <[email protected]>

* doc fix

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* duplicate schemas

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* go fmt

Signed-off-by: Jan-Otto Kröpke <[email protected]>

* add note on updating certain values in default_node_pool and fix whitespacing

---------

Signed-off-by: Jan-Otto Kröpke <[email protected]>
Co-authored-by: stephybun <[email protected]>
  • Loading branch information
jkroepke and stephybun authored May 31, 2023
1 parent 0e9a067 commit 727c3c4
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
}, false),
},

"kubelet_config": schemaNodePoolKubeletConfig(),
"kubelet_config": schemaNodePoolKubeletConfigForceNew(),

"linux_os_config": schemaNodePoolLinuxOSConfig(),
"linux_os_config": schemaNodePoolLinuxOSConfigForceNew(),

"fips_enabled": {
Type: pluginsdk.TypeBool,
Expand Down
25 changes: 22 additions & 3 deletions internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2067,15 +2067,34 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}
}
}

// if the default node pool name has changed it means the initial attempt at resizing failed
if d.HasChange("default_node_pool.0.vm_size") || d.HasChange("default_node_pool.0.name") {
cycleNodePoolProperties := []string{
"default_node_pool.0.name",
"default_node_pool.0.enable_host_encryption",
"default_node_pool.0.enable_node_public_ip",
"default_node_pool.0.kubelet_config",
"default_node_pool.0.linux_os_config",
"default_node_pool.0.max_pods",
"default_node_pool.0.node_taints",
"default_node_pool.0.only_critical_addons_enabled",
"default_node_pool.0.os_disk_size_gb",
"default_node_pool.0.os_disk_type",
"default_node_pool.0.os_sku",
"default_node_pool.0.pod_subnet_id",
"default_node_pool.0.ultra_ssd_enabled",
"default_node_pool.0.vnet_subnet_id",
"default_node_pool.0.vm_size",
"default_node_pool.0.zones",
}

// if the default node pool name has changed, it means the initial attempt at resizing failed
if d.HasChanges(cycleNodePoolProperties...) {
log.Printf("[DEBUG] Cycling Default Node Pool..")
// to provide a seamless updating experience for the vm size of the default node pool we need to cycle the default
// node pool by provisioning a temporary system node pool, tearing down the former default node pool and then
// bringing up the new one.

if v := d.Get("default_node_pool.0.temporary_name_for_rotation").(string); v == "" {
return fmt.Errorf("`temporary_name_for_rotation` must be specified when updating `vm_size`")
return fmt.Errorf("`temporary_name_for_rotation` must be specified when updating any of the following properties %q", cycleNodePoolProperties)
}

temporaryNodePoolName := d.Get("default_node_pool.0.temporary_name_for_rotation").(string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ func TestAccKubernetesCluster_updateVmSizeAfterFailureWithTempWithoutDefault(t *
})
}

func TestAccKubernetesCluster_cycleSystemNodePool(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withHostTempDiskVmSize(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.updateOsDisk(data, "Ephemeral", 75),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("default_node_pool.0.temporary_name_for_rotation"),
{
Config: r.updateOsSku(data, "Mariner"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("default_node_pool.0.temporary_name_for_rotation"),
{
Config: r.updateZones(data, "Standard_D2ads_v5", "[1,2,3]"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("default_node_pool.0.temporary_name_for_rotation"),
{
Config: r.updateLinuxKernelSettings(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("default_node_pool.0.temporary_name_for_rotation"),
})
}

func TestAccKubernetesCluster_addAgent(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}
Expand Down Expand Up @@ -458,6 +501,41 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) withHostTempDiskVmSize(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_D2ads_v5"
}
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) basicWithTempName(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -516,7 +594,7 @@ resource "azurerm_kubernetes_cluster" "test" {
temporary_name_for_rotation = "temp"
node_count = 1
vm_size = "%s"
enable_host_encryption = true
enable_host_encryption = false
}
identity {
Expand All @@ -531,6 +609,190 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, vmSize)
}

func (KubernetesClusterResource) updateZones(data acceptance.TestData, vmSize, zones string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
default_node_pool {
name = "default"
temporary_name_for_rotation = "temp"
node_count = 1
vm_size = "%s"
zones = %s
enable_node_public_ip = true
max_pods = 60
only_critical_addons_enabled = true
kubelet_config {
pod_max_pid = 12346
}
linux_os_config {
sysctl_config {
vm_swappiness = 40
}
}
}
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, vmSize, zones)
}

func (KubernetesClusterResource) updateLinuxKernelSettings(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
default_node_pool {
name = "default"
temporary_name_for_rotation = "temp"
node_count = 1
vm_size = "Standard_D2ads_v5"
enable_node_public_ip = true
max_pods = 60
only_critical_addons_enabled = true
kubelet_config {
pod_max_pid = 12347
}
linux_os_config {
sysctl_config {
vm_swappiness = 45
}
}
}
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) updateOsDisk(data acceptance.TestData, osDiskType string, osDiskSize int) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
default_node_pool {
name = "default"
temporary_name_for_rotation = "temp"
node_count = 1
os_disk_type = "%s"
os_disk_size_gb = %d
vm_size = "Standard_D2ads_v5"
}
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, osDiskType, osDiskSize)
}

func (KubernetesClusterResource) updateOsSku(data acceptance.TestData, osSku string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
default_node_pool {
name = "default"
temporary_name_for_rotation = "temp"
node_count = 1
os_sku = "%s"
vm_size = "Standard_D2ads_v5"
kubelet_config {
pod_max_pid = 12346
}
linux_os_config {
sysctl_config {
vm_swappiness = 40
}
}
}
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, osSku)
}

func (KubernetesClusterResource) addAgentConfig(data acceptance.TestData, numberOfAgents int) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Loading

0 comments on commit 727c3c4

Please sign in to comment.