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
Changes from 1 commit
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
32 changes: 26 additions & 6 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: expandKubernetesClusterMaintenanceConfiguration(maintenanceConfigRaw.([]interface{}), nil),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing in nil here could you please split the expand into two separate functions, one for create and one for update so it's clear at first glance that they are being handled differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @stephybun ! I've updated this PR as suggested.

}
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: expandKubernetesClusterMaintenanceConfiguration(maintenanceConfigRaw.([]interface{}), nil),
}
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 := expandKubernetesClusterMaintenanceConfiguration(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 := expandKubernetesClusterMaintenanceConfiguration(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 expandKubernetesClusterMaintenanceConfiguration(input []interface{}, existing *maintenanceconfigurations.MaintenanceConfigurationProperties) *maintenanceconfigurations.MaintenanceConfigurationProperties {
if len(input) == 0 {
return nil
}
Expand Down Expand Up @@ -4011,7 +4027,11 @@ func expandKubernetesClusterMaintenanceConfiguration(input []interface{}) *maint

if startDateRaw := value["start_date"]; startDateRaw != nil && startDateRaw.(string) != "" {
startDate, _ := time.Parse(time.RFC3339, startDateRaw.(string))
output.MaintenanceWindow.StartDate = utils.String(startDate.Format("2006-01-02"))
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 {
Expand Down