Skip to content

Commit

Permalink
rename automatic_channel_upgrade and node_os_channel_upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun committed Mar 18, 2024
1 parent dbcfa3c commit e872af9
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 50 deletions.
102 changes: 86 additions & 16 deletions internal/services/containers/kubernetes_cluster_other_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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),
})
}

Expand Down Expand Up @@ -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
Expand All @@ -972,23 +984,23 @@ 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(),
{
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(),
{
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(),
Expand Down Expand Up @@ -2220,7 +2232,8 @@ func (KubernetesClusterResource) upgradeChannelConfig(data acceptance.TestData,
upgradeChannel = "null"
}

return fmt.Sprintf(`
if !features.FourPointOhBeta() {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {}
}
Expand All @@ -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)
}

Expand Down
117 changes: 83 additions & 34 deletions internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`")
}
}

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e872af9

Please sign in to comment.