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

Fix LKE shuffled node pool diffing #386

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Changes from all 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
39 changes: 38 additions & 1 deletion linode/resource_linode_lke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func resourceLinodeLKEClusterRead(ctx context.Context, d *schema.ResourceData, m
return diag.Errorf("Error parsing Linode LKE Cluster ID: %s", err)
}

declaredPools, ok := d.Get("pool").([]interface{})
if !ok {
return diag.Errorf("failed to parse linode lke cluster pools: %d", id)
}

cluster, err := client.GetLKECluster(context.Background(), id)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
Expand Down Expand Up @@ -171,8 +176,9 @@ func resourceLinodeLKEClusterRead(ctx context.Context, d *schema.ResourceData, m
d.Set("tags", cluster.Tags)
d.Set("status", cluster.Status)
d.Set("kubeconfig", kubeconfig.KubeConfig)
d.Set("pool", flattenLinodeLKEClusterPools(pools))
d.Set("api_endpoints", flattenLinodeLKEClusterAPIEndpoints(endpoints))
d.Set("pool", flattenLinodeLKEClusterPools(matchPoolsWithSchema(pools, declaredPools)))

return nil
}

Expand Down Expand Up @@ -604,3 +610,34 @@ func flattenLinodeLKEClusterAPIEndpoints(apiEndpoints []linodego.LKEClusterAPIEn
}
return flattened
}

// This cannot currently be handled efficiently by a DiffSuppressFunc
// See: https://github.com/hashicorp/terraform-plugin-sdk/issues/477
func matchPoolsWithSchema(pools []linodego.LKEClusterPool, declaredPools []interface{}) []linodego.LKEClusterPool {
result := make([]linodego.LKEClusterPool, len(declaredPools))

poolMap := make(map[int]linodego.LKEClusterPool, len(declaredPools))
for _, pool := range pools {
poolMap[pool.ID] = pool
}

for i, declaredPool := range declaredPools {
declaredPool := declaredPool.(map[string]interface{})

for key, pool := range poolMap {
if pool.Count != declaredPool["count"] || pool.Type != declaredPool["type"] {
continue
}

result[i] = pool
delete(poolMap, key)
break
}
}

for _, pool := range poolMap {
result = append(result, pool)
}

return result
}