Skip to content

Commit

Permalink
azurerm_kubernetes_cluster - do not pass the maintenance window sta…
Browse files Browse the repository at this point in the history
…rt date if it's not changed (#23985)

* `azurerm_kubernetes_cluster` - do not pass the maintenance window start date if it's not changed

* update

* update testcase

* make terrafmt
  • Loading branch information
ms-henglu authored Dec 4, 2023
1 parent 5991864 commit 6ed0029
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerservice/2023-04-02-preview/agentpools"
Expand Down Expand Up @@ -2342,21 +2343,23 @@ resource "azurerm_kubernetes_cluster" "test" {
}

func (KubernetesClusterResource) completeMaintenanceConfigAutoUpgrade(data acceptance.TestData) string {
startDate := time.Now().Format("2006-01-02T00:00:00Z")
endDate := time.Now().AddDate(0, 0, 4).Format("2006-01-02T00:00:00Z")
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
name = "acctestRG-aks-%[2]d"
location = "%[1]s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
name = "acctestaks%[2]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
dns_prefix = "acctestaks%[2]d"
default_node_pool {
name = "default"
node_count = 1
Expand All @@ -2374,19 +2377,19 @@ resource "azurerm_kubernetes_cluster" "test" {
week_index = "First"
start_time = "07:00"
utc_offset = "+01:00"
start_date = "2023-11-26T00:00:00Z"
start_date = "%[3]s"
not_allowed {
end = "2023-11-30T00:00:00Z"
start = "2023-11-26T00:00:00Z"
end = "%[4]s"
start = "%[3]s"
}
not_allowed {
end = "2023-12-30T00:00:00Z"
start = "2023-12-26T00:00:00Z"
end = "%[4]s"
start = "%[3]s"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
`, data.Locations.Primary, data.RandomInteger, startDate, endDate)
}

func (KubernetesClusterResource) completeMaintenanceConfigDefault(data acceptance.TestData) string {
Expand Down Expand Up @@ -2444,21 +2447,23 @@ resource "azurerm_kubernetes_cluster" "test" {
}

func (KubernetesClusterResource) completeMaintenanceConfigNodeOs(data acceptance.TestData) string {
startDate := time.Now().Format("2006-01-02T00:00:00Z")
endDate := time.Now().AddDate(0, 0, 4).Format("2006-01-02T00:00:00Z")
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
name = "acctestRG-aks-%[2]d"
location = "%[1]s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
name = "acctestaks%[2]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
dns_prefix = "acctestaks%[2]d"
default_node_pool {
name = "default"
node_count = 1
Expand All @@ -2475,19 +2480,19 @@ resource "azurerm_kubernetes_cluster" "test" {
day_of_month = 5
start_time = "07:00"
utc_offset = "+01:00"
start_date = "2023-11-26T00:00:00Z"
start_date = "%[3]s"
not_allowed {
end = "2023-11-30T00:00:00Z"
start = "2023-11-26T00:00:00Z"
end = "%[4]s"
start = "%[3]s"
}
not_allowed {
end = "2023-12-30T00:00:00Z"
start = "2023-12-26T00:00:00Z"
end = "%[4]s"
start = "%[3]s"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
`, data.Locations.Primary, data.RandomInteger, startDate, endDate)
}

func (KubernetesClusterResource) ultraSSD(data acceptance.TestData, ultraSSDEnabled bool) string {
Expand Down
92 changes: 87 additions & 5 deletions internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
if maintenanceConfigRaw, ok := d.GetOk("maintenance_window_auto_upgrade"); ok {
client := meta.(*clients.Client).Containers.MaintenanceConfigurationsClient
parameters := maintenanceconfigurations.MaintenanceConfiguration{
Properties: expandKubernetesClusterMaintenanceConfiguration(maintenanceConfigRaw.([]interface{})),
Properties: expandKubernetesClusterMaintenanceConfigurationForCreate(maintenanceConfigRaw.([]interface{})),
}
maintenanceId := maintenanceconfigurations.NewMaintenanceConfigurationID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, "aksManagedAutoUpgradeSchedule")
if _, err := client.CreateOrUpdate(ctx, maintenanceId, parameters); err != nil {
Expand All @@ -1857,7 +1857,7 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
if maintenanceConfigRaw, ok := d.GetOk("maintenance_window_node_os"); ok {
client := meta.(*clients.Client).Containers.MaintenanceConfigurationsClient
parameters := maintenanceconfigurations.MaintenanceConfiguration{
Properties: expandKubernetesClusterMaintenanceConfiguration(maintenanceConfigRaw.([]interface{})),
Properties: expandKubernetesClusterMaintenanceConfigurationForCreate(maintenanceConfigRaw.([]interface{})),
}
maintenanceId := maintenanceconfigurations.NewMaintenanceConfigurationID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, "aksManagedNodeOSUpgradeSchedule")
if _, err := client.CreateOrUpdate(ctx, maintenanceId, parameters); err != nil {
Expand Down Expand Up @@ -2496,8 +2496,16 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}

if d.HasChange("maintenance_window_auto_upgrade") {
client := meta.(*clients.Client).Containers.MaintenanceConfigurationsClient
maintenanceWindowProperties := expandKubernetesClusterMaintenanceConfiguration(d.Get("maintenance_window_auto_upgrade").([]interface{}))
maintenanceId := maintenanceconfigurations.NewMaintenanceConfigurationID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, "aksManagedAutoUpgradeSchedule")
existing, err := client.Get(ctx, maintenanceId)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("retrieving Auto Upgrade Schedule Maintenance Configuration for %s: %+v", id, err)
}
var existingProperties *maintenanceconfigurations.MaintenanceConfigurationProperties
if existing.Model != nil {
existingProperties = existing.Model.Properties
}
maintenanceWindowProperties := expandKubernetesClusterMaintenanceConfigurationForUpdate(d.Get("maintenance_window_auto_upgrade").([]interface{}), existingProperties)
if maintenanceWindowProperties != nil {
parameters := maintenanceconfigurations.MaintenanceConfiguration{
Properties: maintenanceWindowProperties,
Expand All @@ -2515,7 +2523,15 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}
if d.HasChange("maintenance_window_node_os") {
client := meta.(*clients.Client).Containers.MaintenanceConfigurationsClient
maintenanceId := maintenanceconfigurations.NewMaintenanceConfigurationID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, "aksManagedNodeOSUpgradeSchedule")
maintenanceWindowProperties := expandKubernetesClusterMaintenanceConfiguration(d.Get("maintenance_window_node_os").([]interface{}))
existing, err := client.Get(ctx, maintenanceId)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("retrieving Node OS Upgrade Schedule Maintenance Configuration for %s: %+v", id, err)
}
var existingProperties *maintenanceconfigurations.MaintenanceConfigurationProperties
if existing.Model != nil {
existingProperties = existing.Model.Properties
}
maintenanceWindowProperties := expandKubernetesClusterMaintenanceConfigurationForUpdate(d.Get("maintenance_window_node_os").([]interface{}), existingProperties)
if maintenanceWindowProperties != nil {
parameters := maintenanceconfigurations.MaintenanceConfiguration{
Properties: maintenanceWindowProperties,
Expand Down Expand Up @@ -3957,7 +3973,7 @@ func expandKubernetesClusterMaintenanceConfigurationDefault(input []interface{})
}
}

func expandKubernetesClusterMaintenanceConfiguration(input []interface{}) *maintenanceconfigurations.MaintenanceConfigurationProperties {
func expandKubernetesClusterMaintenanceConfigurationForCreate(input []interface{}) *maintenanceconfigurations.MaintenanceConfigurationProperties {
if len(input) == 0 {
return nil
}
Expand Down Expand Up @@ -4019,6 +4035,72 @@ func expandKubernetesClusterMaintenanceConfiguration(input []interface{}) *maint
return output
}

func expandKubernetesClusterMaintenanceConfigurationForUpdate(input []interface{}, existing *maintenanceconfigurations.MaintenanceConfigurationProperties) *maintenanceconfigurations.MaintenanceConfigurationProperties {
if len(input) == 0 {
return nil
}
value := input[0].(map[string]interface{})

var schedule maintenanceconfigurations.Schedule

if value["frequency"] == "Daily" {
schedule = maintenanceconfigurations.Schedule{
Daily: &maintenanceconfigurations.DailySchedule{
IntervalDays: int64(value["interval"].(int)),
},
}
}
if value["frequency"] == "Weekly" {
schedule = maintenanceconfigurations.Schedule{
Weekly: &maintenanceconfigurations.WeeklySchedule{
IntervalWeeks: int64(value["interval"].(int)),
DayOfWeek: maintenanceconfigurations.WeekDay(value["day_of_week"].(string)),
},
}
}
if value["frequency"] == "AbsoluteMonthly" {
schedule = maintenanceconfigurations.Schedule{
AbsoluteMonthly: &maintenanceconfigurations.AbsoluteMonthlySchedule{
DayOfMonth: int64(value["day_of_month"].(int)),
IntervalMonths: int64(value["interval"].(int)),
},
}
}
if value["frequency"] == "RelativeMonthly" {
schedule = maintenanceconfigurations.Schedule{
RelativeMonthly: &maintenanceconfigurations.RelativeMonthlySchedule{
DayOfWeek: maintenanceconfigurations.WeekDay(value["day_of_week"].(string)),
WeekIndex: maintenanceconfigurations.Type(value["week_index"].(string)),
IntervalMonths: int64(value["interval"].(int)),
},
}
}

output := &maintenanceconfigurations.MaintenanceConfigurationProperties{
MaintenanceWindow: &maintenanceconfigurations.MaintenanceWindow{
StartTime: value["start_time"].(string),
UtcOffset: utils.String(value["utc_offset"].(string)),
NotAllowedDates: expandKubernetesClusterMaintenanceConfigurationDateSpans(value["not_allowed"].(*pluginsdk.Set).List()),
Schedule: schedule,
},
}

if startDateRaw := value["start_date"]; startDateRaw != nil && startDateRaw.(string) != "" {
startDate, _ := time.Parse(time.RFC3339, startDateRaw.(string))
startDateStr := startDate.Format("2006-01-02")
// start_date is an Optional+Computed property, the default value returned by the API could be invalid during update, so we only set it if it's different from the existing value
if existing == nil || existing.MaintenanceWindow.StartDate == nil || *existing.MaintenanceWindow.StartDate != startDateStr {
output.MaintenanceWindow.StartDate = pointer.To(startDateStr)
}
}

if duration := value["duration"]; duration != nil && duration.(int) != 0 {
output.MaintenanceWindow.DurationHours = int64(duration.(int))
}

return output
}

func expandKubernetesClusterMaintenanceConfigurationTimeSpans(input []interface{}) *[]maintenanceconfigurations.TimeSpan {
results := make([]maintenanceconfigurations.TimeSpan, 0)
for _, item := range input {
Expand Down

0 comments on commit 6ed0029

Please sign in to comment.