Skip to content

Commit

Permalink
Fix NCS clusters with labels (#100)
Browse files Browse the repository at this point in the history
* Fix NCS clusters with labels

Don't try to read/subtract the project labels

* fix corner case when emptying cluster labels

previously, if a cluster update removed all cluster labels, subsequent
runs with no change would show a permanent difference to be applied
  • Loading branch information
multi-io authored Feb 23, 2024
1 parent 0d43d4c commit cbd0190
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions metakube/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,18 +324,12 @@ func metakubeResourceClusterRead(ctx context.Context, d *schema.ResourceData, m
_ = d.Set("dc_name", r.Payload.Spec.Cloud.DatacenterName)
_ = d.Set("name", r.Payload.Name)
if len(r.Payload.Labels) > 0 {
resourceProject, err := getProject(k, projectID)
if err != nil {
return diag.FromErr(err)
}
if labels := mapExclude(r.Payload.Labels, resourceProject.Labels); len(labels) > 0 {
if err := d.Set("labels", labels); err != nil {
return diag.Diagnostics{{
Severity: diag.Error,
Summary: "Invalid value",
AttributePath: cty.Path{cty.GetAttrStep{Name: "labels"}},
}}
}
if err := d.Set("labels", r.Payload.Labels); err != nil {
return diag.Diagnostics{{
Severity: diag.Error,
Summary: "Invalid value",
AttributePath: cty.Path{cty.GetAttrStep{Name: "labels"}},
}}
}
}

Expand Down Expand Up @@ -638,20 +632,29 @@ func metakubeResourceClusterSendPatchReq(ctx context.Context, d *schema.Resource
"spec": clusterSpec,
})

var patchedCluster *models.Cluster
err := retry.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate), func() *retry.RetryError {
_, err := k.client.Project.PatchClusterV2(p, k.auth)
patchResult, err := k.client.Project.PatchClusterV2(p, k.auth)
if err != nil {
if e, ok := err.(*project.PatchClusterV2Default); ok && e.Code() == http.StatusConflict {
return retry.RetryableError(fmt.Errorf("cluster patch conflict: %v", err))
}
return retry.NonRetryableError(fmt.Errorf("patch cluster '%s': %v", d.Id(), stringifyResponseError(err)))
}
patchedCluster = patchResult.GetPayload()
return nil
})
if err != nil {
return err
}

if patchedCluster.Labels == nil {
// if the cluster has no labels after patching, set them to nil in the state data explicitly
// otherwise, if the cluster had labels before that were all removed by the patch, remnants
// (empty labels) would remain in the data, showing up as a permanent difference on subsequent runs
_ = d.Set("labels", nil)
}

return nil
}

Expand Down

0 comments on commit cbd0190

Please sign in to comment.