Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_kubernetes_cluster - do not pass the maintenance window start date if it's not changed #23985

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1848,7 +1848,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 @@ -1859,7 +1859,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 @@ -2499,8 +2499,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 @@ -2518,7 +2526,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 @@ -3959,7 +3975,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 @@ -4021,6 +4037,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
Loading