Skip to content

Commit 7ad4b6a

Browse files
committed
Added logic nodepool tags should not be same as cluster tags
Signed-off-by: Nootan Singh <snootan@vmware.com>
1 parent 229c50f commit 7ad4b6a

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

internal/resources/ekscluster/helpers.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package ekscluster
77
import (
88
"reflect"
99

10+
"github.com/pkg/errors"
11+
1012
eksmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/ekscluster"
1113
)
1214

@@ -194,17 +196,22 @@ func setEquality(s1, s2 []string) bool {
194196
return true
195197
}
196198

197-
func copyClusterTagsToNodepools(nodepoolTags map[string]string, eksTags map[string]string) map[string]string {
199+
func copyClusterTagsToNodepools(nodepoolTags map[string]string, eksTags map[string]string) (map[string]string, error) {
198200
npTags := make(map[string]string)
201+
202+
var err error
203+
199204
if len(nodepoolTags) > 0 {
200205
npTags = nodepoolTags
201206
}
202207

203208
for tmcTag, tmcVal := range eksTags {
204-
if _, ok := npTags[tmcTag]; !ok {
209+
if val, ok := npTags[tmcTag]; !ok {
205210
npTags[tmcTag] = tmcVal
211+
} else if val == tmcVal {
212+
err = errors.Errorf("key:%v, val:%v", tmcTag, val)
206213
}
207214
}
208215

209-
return npTags
216+
return npTags, err
210217
}

internal/resources/ekscluster/resource_ekscluster.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,12 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
588588
clusterSpec, nps := constructEksClusterSpec(d)
589589
// Copy tags from cluster to nodepool
590590
for _, npDefData := range nps {
591-
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
591+
var err error
592+
npDefData.Spec.Tags, err = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
593+
594+
if err != nil {
595+
return diag.FromErr(errors.Wrap(err, "Nodepool tags should not be same as cluster tags"))
596+
}
592597
}
593598

594599
clusterReq := &eksmodel.VmwareTanzuManageV1alpha1EksclusterCreateUpdateEksClusterRequest{
@@ -682,7 +687,10 @@ func resourceClusterInPlaceUpdate(ctx context.Context, d *schema.ResourceData, m
682687

683688
// Copy tags from cluster to nodepool
684689
for _, npDefData := range nodepools {
685-
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
690+
npDefData.Spec.Tags, err = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
691+
if err != nil {
692+
return diag.FromErr(errors.Wrap(err, "Nodepool tags should not be same as cluster tags"))
693+
}
686694
}
687695
// EKS cluster update API on TMC side ignores nodepools passed to it.
688696
// The nodepools have to be updated via separate nodepool API, hence we

internal/resources/ekscluster/resource_ekscluster_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
422422
RoleArn: controlPlaneRoleARN,
423423
Tags: map[string]string{
424424
"tmc.cloud.vmware.com/tmc-managed": "true",
425-
"testclustertag": "testclustertagvalue",
426-
"testingtag": "testingtagvalue",
427-
"testsametag": "testsametagval",
425+
"testtag": "testval",
426+
"newtesttag": "newtestval",
428427
},
429428
KubernetesNetworkConfig: &eksmodel.VmwareTanzuManageV1alpha1EksclusterKubernetesNetworkConfig{
430429
ServiceCidr: "10.100.0.0/16",
@@ -470,10 +469,9 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
470469
CapacityType: "ON_DEMAND",
471470
RootDiskSize: 40,
472471
Tags: map[string]string{
473-
"testnptag": "testnptagvalue",
474-
"testingtag": "testingnptagvalue",
475-
"testsametag": "testsametagval",
476-
"testclustertag": "testclustertagvalue",
472+
"testnptag": "testnptagvalue",
473+
"newtesttag": "testingtagvalue",
474+
"testtag": "testval",
477475
},
478476
NodeLabels: map[string]string{
479477
"testnplabelkey": "testnplabelvalue",
@@ -514,10 +512,9 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
514512
Spec: &eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolSpec{
515513
RoleArn: workerRoleArn,
516514
Tags: map[string]string{
517-
"testnptag": "testnptagvalue",
518-
"testingtag": "testingnptagvalue",
519-
"testsametag": "testsametagval",
520-
"testclustertag": "testclustertagvalue",
515+
"testnptag": "testnptagvalue",
516+
"newtesttag": "testingtagvalue",
517+
"testtag": "testval",
521518
},
522519
NodeLabels: map[string]string{
523520
"testnplabelkey": "testnplabelvalue",

internal/resources/testing/test_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const testDefaultCreateEksClusterScript = `
1818
config {
1919
kubernetes_version = "{{.KubernetesVersion}}"
2020
role_arn = "arn:aws:iam::{{.AWSAccountNumber}}:role/control-plane.{{.CloudFormationTemplateID}}.eks.tmc.cloud.vmware.com"
21-
tags = { "testclustertag" : "testclustertagvalue", "testingtag": "testingtagvalue", "testsametag":"testsametagval"}
21+
tags = { "testtag" : "testval", "newtesttag": "newtestval"}
2222
kubernetes_network_config {
2323
service_cidr = "10.100.0.0/16" // Forces new
2424
}
@@ -66,7 +66,7 @@ const testDefaultCreateEksClusterScript = `
6666
security_groups = ["sg-0a6768722e9716768"]
6767
}
6868
root_disk_size = 40 // Default: 20GiB, forces New
69-
tags = { "testnptag" : "testnptagvalue", "testingtag": "testingnptagvalue"}
69+
tags = { "testnptag" : "testnptagvalue", "newtesttag": "testingtagvalue"}
7070
node_labels = { "testnplabelkey" : "testnplabelvalue" }
7171
subnet_ids = [ // Required, forces new
7272
"subnet-0a184f6302af32a86",
@@ -97,7 +97,7 @@ const testDefaultCreateEksClusterScript = `
9797
spec {
9898
// Refer to nodepool's schema
9999
role_arn = "arn:aws:iam::{{.AWSAccountNumber}}:role/worker.{{.CloudFormationTemplateID}}.eks.tmc.cloud.vmware.com"
100-
tags = { "testnptag" : "testnptagvalue", "testingtag": "testingnptagvalue"}
100+
tags = { "testnptag" : "testnptagvalue", "newtesttag": "testingtagvalue"}
101101
node_labels = { "testnplabelkey" : "testnplabelvalue" }
102102
launch_template {
103103
name = "PLACE_HOLDER"

0 commit comments

Comments
 (0)