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 - allow resizing of default_node_pool #20628

Merged
merged 17 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
105 changes: 95 additions & 10 deletions internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func resourceKubernetesCluster() *pluginsdk.Resource {
pluginsdk.ForceNewIfChange("api_server_access_profile.0.subnet_id", func(ctx context.Context, old, new, meta interface{}) bool {
return old != "" && new == ""
}),
pluginsdk.ForceNewIf("default_node_pool.0.name", func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool {
old, new := d.GetChange("default_node_pool.0.name")
defaultName := d.Get("default_node_pool.0.name")
tempName := d.Get("default_node_pool.0.temp_name_for_vm_resize")

// if the default node pool name has been set to temp_name_for_vm_resize it means resizing failed
// we should not try to recreate the cluster, another apply will attempt the resize again
if old != "" && old == tempName {
return new != defaultName
}
return true
}),
),

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down Expand Up @@ -1978,13 +1990,10 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}

// update the node pool using the separate API
if d.HasChange("default_node_pool") {
log.Printf("[DEBUG] Updating of Default Node Pool..")

agentProfiles, err := ExpandDefaultNodePool(d)
if err != nil {
return fmt.Errorf("expanding `default_node_pool`: %+v", err)
}

agentProfile := ConvertDefaultNodePoolToAgentPool(agentProfiles)
defaultNodePoolId := agentpools.NewAgentPoolID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, *agentProfile.Name)

Expand All @@ -2004,15 +2013,72 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}
}
}

agentPool, err := nodePoolsClient.CreateOrUpdate(ctx, defaultNodePoolId, agentProfile)
if err != nil {
return fmt.Errorf("updating Default Node Pool %s %+v", defaultNodePoolId, err)
}
// if the default node pool name has changed it means the initial attempt at resizing failed
if d.HasChange("default_node_pool.0.vm_size") || d.HasChange("default_node_pool.0.name") {
log.Printf("[DEBUG] Cycling Default Node Pool..")
// to provide a seamless updating experience for the vm size of the default node pool we need to cycle the default
// node pool by provisioning a temporary system node pool, tearing down the former default node pool and then
// bringing up the new one.

if v := d.Get("default_node_pool.0.temp_name_for_vm_resize").(string); v == "" {
return fmt.Errorf("`temp_name_for_vm_resize` must be specified when updating `vm_size`")
}

if err := agentPool.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("waiting for update of Default Node Pool %s: %+v", defaultNodePoolId, err)
temporaryNodePoolName := d.Get("default_node_pool.0.temp_name_for_vm_resize").(string)
tempNodePoolId := agentpools.NewAgentPoolID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, temporaryNodePoolName)

tempExisting, err := nodePoolsClient.Get(ctx, tempNodePoolId)
if !response.WasNotFound(tempExisting.HttpResponse) && err != nil {
return fmt.Errorf("checking for existing temporary %s: %+v", tempNodePoolId, err)
}

defaultExisting, err := nodePoolsClient.Get(ctx, defaultNodePoolId)
if !response.WasNotFound(defaultExisting.HttpResponse) && err != nil {
return fmt.Errorf("checking for existing default %s: %+v", defaultNodePoolId, err)
}

tempAgentProfile := agentProfile
tempAgentProfile.Name = &temporaryNodePoolName
// if the temp node pool already exists due to a previous failure, don't bother spinning it up
if tempExisting.Model == nil {
if err := retrySystemNodePoolCreation(ctx, nodePoolsClient, tempNodePoolId, tempAgentProfile); err != nil {
return fmt.Errorf("creating temporary %s: %+v", tempNodePoolId, err)
}
}

ignorePodDisruptionBudget := true
deleteOpts := agentpools.DeleteOperationOptions{
IgnorePodDisruptionBudget: &ignorePodDisruptionBudget,
}
// delete the old default node pool if it exists
if defaultExisting.Model != nil {
if err := nodePoolsClient.DeleteThenPoll(ctx, defaultNodePoolId, deleteOpts); err != nil {
return fmt.Errorf("deleting default %s: %+v", defaultNodePoolId, err)
}
}

// create the default node pool with the new vm size
if err := retrySystemNodePoolCreation(ctx, nodePoolsClient, defaultNodePoolId, agentProfile); err != nil {
// if creation of the default node pool fails we automatically fall back to the temporary node pool
// in func findDefaultNodePool
log.Printf("[DEBUG] Creation of resized default node pool failed")
return fmt.Errorf("creating default %s: %+v", defaultNodePoolId, err)
}

if err := nodePoolsClient.DeleteThenPoll(ctx, tempNodePoolId, deleteOpts); err != nil {
return fmt.Errorf("deleting temporary %s: %+v", tempNodePoolId, err)
}

log.Printf("[DEBUG] Cycled Default Node Pool..")
} else {
log.Printf("[DEBUG] Updating of Default Node Pool..")

if err := nodePoolsClient.CreateOrUpdateThenPoll(ctx, defaultNodePoolId, agentProfile); err != nil {
return fmt.Errorf("updating Default Node Pool %s %+v", defaultNodePoolId, err)
}

log.Printf("[DEBUG] Updated Default Node Pool.")
}
log.Printf("[DEBUG] Updated Default Node Pool.")
}

if d.HasChange("maintenance_window") {
Expand Down Expand Up @@ -3792,3 +3858,22 @@ func flattenKubernetesClusterAzureMonitorProfile(input *managedclusters.ManagedC
},
}
}

func retrySystemNodePoolCreation(ctx context.Context, client *agentpools.AgentPoolsClient, id agentpools.AgentPoolId, profile agentpools.AgentPool) error {
// retries the creation of a system node pool 3 times
retries := 3
attempt := 0
for attempt < retries {
if err := client.CreateOrUpdateThenPoll(ctx, id, profile); err == nil {
break
} else {
// only return the error on the final retry
if attempt == 2 {
return err
}

attempt++
}
}
return nil
stephybun marked this conversation as resolved.
Show resolved Hide resolved
}
Loading