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

Applied cluster tags to nodepools, don't report this as a terraform change #369

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions internal/resources/ekscluster/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func setResourceData(d *schema.ResourceData, eksCluster *eksmodel.VmwareTanzuMan
// see the explanation of this in the func doc of nodepoolPosMap
npPosMap := nodepoolPosMap(tfNodepools)

// get nodepool mapping of names with their details
npDataMap := nodepoolDetailsMap(tfNodepools)

nodepools := make([]*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolDefinition, len(tfNodepools))

for _, np := range remoteNodepools {
Expand All @@ -157,6 +160,8 @@ func setResourceData(d *schema.ResourceData, eksCluster *eksmodel.VmwareTanzuMan

if pos, ok := npPosMap[np.FullName.Name]; ok {
nodepools[pos] = npDef
// Add tf file nodepool tags as part of nodepool tags so that it will not show difference
nodepools[pos].Spec.Tags = npDataMap[np.FullName.Name].Spec.Tags
} else {
nodepools = append(nodepools, npDef)
}
Expand Down Expand Up @@ -191,3 +196,13 @@ func nodepoolPosMap(nps []*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolD

return ret
}

// Returns mapping of nodepool names to their corresponding details in the array.
func nodepoolDetailsMap(nps []*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolDefinition) map[string]*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolDefinition {
ret := map[string]*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolDefinition{}
for _, np := range nps {
ret[np.Info.Name] = np
}

return ret
}
15 changes: 15 additions & 0 deletions internal/resources/ekscluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,18 @@ func setEquality(s1, s2 []string) bool {

return true
}

func copyClusterTagsToNodepools(nodepoolTags map[string]string, eksTags map[string]string) map[string]string {
npTags := make(map[string]string)
if len(nodepoolTags) > 0 {
npTags = nodepoolTags
}

for tmcTag, tmcVal := range eksTags {
if _, ok := npTags[tmcTag]; !ok {
npTags[tmcTag] = tmcVal
}
}

return npTags
}
8 changes: 8 additions & 0 deletions internal/resources/ekscluster/resource_ekscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf

clusterFn := constructFullname(d)
clusterSpec, nps := constructEksClusterSpec(d)
// Copy tags from cluster to nodepool
for _, npDefData := range nps {
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
}

clusterReq := &eksmodel.VmwareTanzuManageV1alpha1EksclusterCreateUpdateEksClusterRequest{
EksCluster: &eksmodel.VmwareTanzuManageV1alpha1EksclusterEksCluster{
Expand Down Expand Up @@ -676,6 +680,10 @@ func resourceClusterInPlaceUpdate(ctx context.Context, d *schema.ResourceData, m

clusterSpec, nodepools := constructEksClusterSpec(d)

// Copy tags from cluster to nodepool
for _, npDefData := range nodepools {
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
}
// EKS cluster update API on TMC side ignores nodepools passed to it.
// The nodepools have to be updated via separate nodepool API, hence we
// deal with them separately.
Expand Down