From e872af9c005aa4eb740c272db624954e5c2bd7f7 Mon Sep 17 00:00:00 2001 From: Steph Date: Mon, 18 Mar 2024 14:32:47 +0100 Subject: [PATCH] rename automatic_channel_upgrade and node_os_channel_upgrade --- .../kubernetes_cluster_other_resource_test.go | 102 ++++++++++++--- .../containers/kubernetes_cluster_resource.go | 117 +++++++++++++----- 2 files changed, 169 insertions(+), 50 deletions(-) diff --git a/internal/services/containers/kubernetes_cluster_other_resource_test.go b/internal/services/containers/kubernetes_cluster_other_resource_test.go index c56256236b61..1a7165d33b1a 100644 --- a/internal/services/containers/kubernetes_cluster_other_resource_test.go +++ b/internal/services/containers/kubernetes_cluster_other_resource_test.go @@ -17,6 +17,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -490,53 +491,60 @@ func TestAccKubernetesCluster_upgradeChannel(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test") r := KubernetesClusterResource{} + autoUpgradeChannel := "automatic_upgrade_channel" + nodeOsUpgradeChannel := "node_os_upgrade_channel" + if !features.FourPointOhBeta() { + autoUpgradeChannel = "automatic_channel_upgrade" + nodeOsUpgradeChannel = "node_os_channel_upgrade" + } + data.ResourceTest(t, r, []acceptance.TestStep{ { Config: r.upgradeChannelConfig(data, olderKubernetesVersion, "rapid"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("kubernetes_version").HasValue(olderKubernetesVersion), - check.That(data.ResourceName).Key("automatic_channel_upgrade").HasValue("rapid"), + check.That(data.ResourceName).Key(autoUpgradeChannel).HasValue("rapid"), ), }, - data.ImportStep("node_os_channel_upgrade"), + data.ImportStep(nodeOsUpgradeChannel), { Config: r.upgradeChannelConfig(data, olderKubernetesVersion, "patch"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("kubernetes_version").HasValue(olderKubernetesVersion), - check.That(data.ResourceName).Key("automatic_channel_upgrade").HasValue("patch"), + check.That(data.ResourceName).Key(autoUpgradeChannel).HasValue("patch"), ), }, - data.ImportStep("node_os_channel_upgrade"), + data.ImportStep(nodeOsUpgradeChannel), { Config: r.upgradeChannelConfig(data, olderKubernetesVersion, "node-image"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("kubernetes_version").HasValue(olderKubernetesVersion), - check.That(data.ResourceName).Key("automatic_channel_upgrade").HasValue("node-image"), + check.That(data.ResourceName).Key(autoUpgradeChannel).HasValue("node-image"), ), }, - data.ImportStep("node_os_channel_upgrade"), + data.ImportStep(nodeOsUpgradeChannel), { // unset = none Config: r.upgradeChannelConfig(data, olderKubernetesVersion, ""), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("kubernetes_version").HasValue(olderKubernetesVersion), - check.That(data.ResourceName).Key("automatic_channel_upgrade").HasValue(""), + check.That(data.ResourceName).Key(autoUpgradeChannel).HasValue(""), ), }, - data.ImportStep("node_os_channel_upgrade"), + data.ImportStep(nodeOsUpgradeChannel), { Config: r.upgradeChannelConfig(data, olderKubernetesVersion, "stable"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("kubernetes_version").HasValue(olderKubernetesVersion), - check.That(data.ResourceName).Key("automatic_channel_upgrade").HasValue("stable"), + check.That(data.ResourceName).Key(autoUpgradeChannel).HasValue("stable"), ), }, - data.ImportStep("node_os_channel_upgrade"), + data.ImportStep(nodeOsUpgradeChannel), }) } @@ -957,12 +965,16 @@ func TestAccKubernetesCluster_nodeOsUpgradeChannel(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test") r := KubernetesClusterResource{} + nodeOsUpgradeChannel := "node_os_upgrade_channel" + if !features.FourPointOhBeta() { + nodeOsUpgradeChannel = "node_os_channel_upgrade" + } data.ResourceTest(t, r, []acceptance.TestStep{ { Config: r.nodeOsUpgradeChannel(data, "Unmanaged"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("node_os_channel_upgrade").HasValue("Unmanaged"), + check.That(data.ResourceName).Key(nodeOsUpgradeChannel).HasValue("Unmanaged"), ), }, // TODO add this back in when upgrading to 2023-06-02-preview @@ -972,7 +984,7 @@ func TestAccKubernetesCluster_nodeOsUpgradeChannel(t *testing.T) { Config: r.nodeOsUpgradeChannel(data, "SecurityPatch"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("node_os_channel_upgrade").HasValue("SecurityPatch"), + check.That(data.ResourceName).Key(nodeOsUpgradeChannel).HasValue("SecurityPatch"), ), }, // data.ImportStep(), @@ -980,7 +992,7 @@ func TestAccKubernetesCluster_nodeOsUpgradeChannel(t *testing.T) { Config: r.nodeOsUpgradeChannel(data, "NodeImage"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("node_os_channel_upgrade").HasValue("NodeImage"), + check.That(data.ResourceName).Key(nodeOsUpgradeChannel).HasValue("NodeImage"), ), }, // data.ImportStep(), @@ -988,7 +1000,7 @@ func TestAccKubernetesCluster_nodeOsUpgradeChannel(t *testing.T) { Config: r.nodeOsUpgradeChannel(data, "None"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("node_os_channel_upgrade").HasValue("None"), + check.That(data.ResourceName).Key(nodeOsUpgradeChannel).HasValue("None"), ), }, // data.ImportStep(), @@ -2220,7 +2232,8 @@ func (KubernetesClusterResource) upgradeChannelConfig(data acceptance.TestData, upgradeChannel = "null" } - return fmt.Sprintf(` + if !features.FourPointOhBeta() { + return fmt.Sprintf(` provider "azurerm" { features {} } @@ -2249,6 +2262,37 @@ resource "azurerm_kubernetes_cluster" "test" { type = "SystemAssigned" } } +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion, upgradeChannel) + } + 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" + kubernetes_version = %q + automatic_upgrade_channel = %s + node_os_upgrade_channel = "NodeImage" + + default_node_pool { + name = "default" + vm_size = "Standard_DS2_v2" + node_count = 1 + } + + identity { + type = "SystemAssigned" + } +} `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion, upgradeChannel) } @@ -3104,7 +3148,8 @@ resource "azurerm_kubernetes_cluster" "test" { } func (KubernetesClusterResource) nodeOsUpgradeChannel(data acceptance.TestData, nodeOsUpgradeChannel string) string { - return fmt.Sprintf(` + if !features.FourPointOhBeta() { + return fmt.Sprintf(` provider "azurerm" { features {} } @@ -3127,6 +3172,31 @@ resource "azurerm_kubernetes_cluster" "test" { type = "SystemAssigned" } } +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, nodeOsUpgradeChannel) + } + 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" + node_os_upgrade_channel = "%s" + default_node_pool { + name = "default" + vm_size = "Standard_DS2_v2" + node_count = 1 + } + identity { + type = "SystemAssigned" + } +} `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, nodeOsUpgradeChannel) } diff --git a/internal/services/containers/kubernetes_cluster_resource.go b/internal/services/containers/kubernetes_cluster_resource.go index d09569a6c214..00ce30e5aad2 100644 --- a/internal/services/containers/kubernetes_cluster_resource.go +++ b/internal/services/containers/kubernetes_cluster_resource.go @@ -178,17 +178,6 @@ func resourceKubernetesCluster() *pluginsdk.Resource { }, }, - "automatic_channel_upgrade": { - Type: pluginsdk.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(managedclusters.UpgradeChannelPatch), - string(managedclusters.UpgradeChannelRapid), - string(managedclusters.UpgradeChannelStable), - string(managedclusters.UpgradeChannelNodeNegativeimage), - }, false), - }, - "auto_scaler_profile": { Type: pluginsdk.TypeList, Optional: true, @@ -966,17 +955,6 @@ func resourceKubernetesCluster() *pluginsdk.Resource { }, }, - "node_os_channel_upgrade": { - Type: pluginsdk.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(managedclusters.NodeOSUpgradeChannelNodeImage), - string(managedclusters.NodeOSUpgradeChannelNone), - string(managedclusters.NodeOSUpgradeChannelSecurityPatch), - string(managedclusters.NodeOSUpgradeChannelUnmanaged), - }, false), - }, - "key_management_service": { Type: pluginsdk.TypeList, Optional: true, @@ -1592,6 +1570,49 @@ func resourceKubernetesCluster() *pluginsdk.Resource { Default: 48, ValidateFunc: validation.IntBetween(24, 2160), } + resource.Schema["automatic_channel_upgrade"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(managedclusters.UpgradeChannelPatch), + string(managedclusters.UpgradeChannelRapid), + string(managedclusters.UpgradeChannelStable), + string(managedclusters.UpgradeChannelNodeNegativeimage), + }, false), + } + resource.Schema["node_os_channel_upgrade"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(managedclusters.NodeOSUpgradeChannelNodeImage), + string(managedclusters.NodeOSUpgradeChannelNone), + string(managedclusters.NodeOSUpgradeChannelSecurityPatch), + string(managedclusters.NodeOSUpgradeChannelUnmanaged), + }, false), + } + } + + if features.FourPointOhBeta() { + resource.Schema["automatic_upgrade_channel"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(managedclusters.UpgradeChannelPatch), + string(managedclusters.UpgradeChannelRapid), + string(managedclusters.UpgradeChannelStable), + string(managedclusters.UpgradeChannelNodeNegativeimage), + }, false), + } + resource.Schema["node_os_upgrade_channel"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(managedclusters.NodeOSUpgradeChannelNodeImage), + string(managedclusters.NodeOSUpgradeChannelNone), + string(managedclusters.NodeOSUpgradeChannelSecurityPatch), + string(managedclusters.NodeOSUpgradeChannelUnmanaged), + }, false), + } } return resource @@ -1742,13 +1763,21 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{} } autoUpgradeProfile := &managedclusters.ManagedClusterAutoUpgradeProfile{} - autoChannelUpgrade := d.Get("automatic_channel_upgrade").(string) - nodeOsChannelUpgrade := d.Get("node_os_channel_upgrade").(string) + + autoChannelUpgrade := d.Get("automatic_upgrade_channel").(string) + nodeOsChannelUpgrade := d.Get("node_os_upgrade_channel").(string) + if !features.FourPointOhBeta() { + autoChannelUpgrade = d.Get("automatic_channel_upgrade").(string) + nodeOsChannelUpgrade = d.Get("node_os_channel_upgrade").(string) + } // this check needs to be separate and gated since node_os_channel_upgrade is a preview feature if nodeOsChannelUpgrade != "" && autoChannelUpgrade != "" { if autoChannelUpgrade == string(managedclusters.UpgradeChannelNodeNegativeimage) && nodeOsChannelUpgrade != string(managedclusters.NodeOSUpgradeChannelNodeImage) { - return fmt.Errorf("`node_os_channel_upgrade` cannot be set to a value other than `NodeImage` if `automatic_channel_upgrade` is set to `node-image`") + if !features.FourPointOhBeta() { + return fmt.Errorf("`node_os_channel_upgrade` cannot be set to a value other than `NodeImage` if `automatic_channel_upgrade` is set to `node-image`") + } + return fmt.Errorf("`node_os_upgrade_channel` cannot be set to a value other than `NodeImage` if `automatic_upgrade_channel` is set to `node-image`") } } @@ -2231,27 +2260,36 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{} existing.Model.Sku.Tier = &skuTier } - if d.HasChange("automatic_channel_upgrade") { + autoUpgradeChannel := "automatic_upgrade_channel" + nodeOsUpgradeChannel := "node_os_upgrade_channel" + if !features.FourPointOhBeta() { + autoUpgradeChannel = "automatic_channel_upgrade" + nodeOsUpgradeChannel = "node_os_channel_upgrade" + } + if d.HasChange(autoUpgradeChannel) { updateCluster = true if existing.Model.Properties.AutoUpgradeProfile == nil { existing.Model.Properties.AutoUpgradeProfile = &managedclusters.ManagedClusterAutoUpgradeProfile{} } - channel := d.Get("automatic_channel_upgrade").(string) + channel := d.Get(autoUpgradeChannel).(string) if channel == "" { channel = string(managedclusters.UpgradeChannelNone) } existing.Model.Properties.AutoUpgradeProfile.UpgradeChannel = pointer.To(managedclusters.UpgradeChannel(channel)) } - if d.HasChange("node_os_channel_upgrade") { + if d.HasChange(nodeOsUpgradeChannel) { updateCluster = true - if d.Get("automatic_channel_upgrade").(string) == string(managedclusters.UpgradeChannelNodeNegativeimage) && d.Get("node_os_channel_upgrade").(string) != string(managedclusters.NodeOSUpgradeChannelNodeImage) { - return fmt.Errorf("`node_os_channel_upgrade` must be set to `NodeImage` if `automatic_channel_upgrade` is set to `node-image`") + if d.Get(autoUpgradeChannel).(string) == string(managedclusters.UpgradeChannelNodeNegativeimage) && d.Get(nodeOsUpgradeChannel).(string) != string(managedclusters.NodeOSUpgradeChannelNodeImage) { + if !features.FourPointOhBeta() { + return fmt.Errorf("`node_os_channel_upgrade` must be set to `NodeImage` if `automatic_channel_upgrade` is set to `node-image`") + } + return fmt.Errorf("`node_os_upgrade_channel` must be set to `NodeImage` if `automatic_upgrade_channel` is set to `node-image`") } if existing.Model.Properties.AutoUpgradeProfile == nil { existing.Model.Properties.AutoUpgradeProfile = &managedclusters.ManagedClusterAutoUpgradeProfile{} } - existing.Model.Properties.AutoUpgradeProfile.NodeOSUpgradeChannel = pointer.To(managedclusters.NodeOSUpgradeChannel(d.Get("node_os_channel_upgrade").(string))) + existing.Model.Properties.AutoUpgradeProfile.NodeOSUpgradeChannel = pointer.To(managedclusters.NodeOSUpgradeChannel(d.Get(nodeOsUpgradeChannel).(string))) } if d.HasChange("http_proxy_config") { @@ -2650,12 +2688,23 @@ func resourceKubernetesClusterRead(d *pluginsdk.ResourceData, meta interface{}) nodeOSUpgradeChannel = string(*profile.NodeOSUpgradeChannel) } } - d.Set("automatic_channel_upgrade", upgradeChannel) + + if !features.FourPointOhBeta() { + d.Set("automatic_channel_upgrade", upgradeChannel) + } else { + d.Set("automatic_upgrade_channel", upgradeChannel) + } // the API returns `node_os_channel_upgrade` when `automatic_channel_upgrade` is set to `node-image` // since it's a preview feature we will only set this if it's explicitly been set in the config for the time being - if v, ok := d.GetOk("node_os_channel_upgrade"); ok && v.(string) != "" { - d.Set("node_os_channel_upgrade", nodeOSUpgradeChannel) + if !features.FourPointOhBeta() { + if v, ok := d.GetOk("node_os_channel_upgrade"); ok && v.(string) != "" { + d.Set("node_os_channel_upgrade", nodeOSUpgradeChannel) + } + } else { + if v, ok := d.GetOk("node_os_upgrade_channel"); ok && v.(string) != "" { + d.Set("node_os_upgrade_channel", nodeOSUpgradeChannel) + } } customCaTrustCertList := flattenCustomCaTrustCerts(props.SecurityProfile)