Skip to content

Commit

Permalink
Added logic nodepool tags should not be same as cluster tags
Browse files Browse the repository at this point in the history
Signed-off-by: Nootan Singh <snootan@vmware.com>
  • Loading branch information
snootan committed Feb 2, 2024
1 parent 229c50f commit 7ad4b6a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
13 changes: 10 additions & 3 deletions internal/resources/ekscluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package ekscluster
import (
"reflect"

"github.com/pkg/errors"

eksmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/ekscluster"
)

Expand Down Expand Up @@ -194,17 +196,22 @@ func setEquality(s1, s2 []string) bool {
return true
}

func copyClusterTagsToNodepools(nodepoolTags map[string]string, eksTags map[string]string) map[string]string {
func copyClusterTagsToNodepools(nodepoolTags map[string]string, eksTags map[string]string) (map[string]string, error) {
npTags := make(map[string]string)

var err error

if len(nodepoolTags) > 0 {
npTags = nodepoolTags
}

for tmcTag, tmcVal := range eksTags {
if _, ok := npTags[tmcTag]; !ok {
if val, ok := npTags[tmcTag]; !ok {
npTags[tmcTag] = tmcVal
} else if val == tmcVal {
err = errors.Errorf("key:%v, val:%v", tmcTag, val)
}
}

return npTags
return npTags, err
}
12 changes: 10 additions & 2 deletions internal/resources/ekscluster/resource_ekscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,12 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
clusterSpec, nps := constructEksClusterSpec(d)
// Copy tags from cluster to nodepool
for _, npDefData := range nps {
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
var err error
npDefData.Spec.Tags, err = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)

if err != nil {
return diag.FromErr(errors.Wrap(err, "Nodepool tags should not be same as cluster tags"))
}
}

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

// Copy tags from cluster to nodepool
for _, npDefData := range nodepools {
npDefData.Spec.Tags = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
npDefData.Spec.Tags, err = copyClusterTagsToNodepools(npDefData.Spec.Tags, clusterSpec.Config.Tags)
if err != nil {
return diag.FromErr(errors.Wrap(err, "Nodepool tags should not be same as cluster 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
Expand Down
19 changes: 8 additions & 11 deletions internal/resources/ekscluster/resource_ekscluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,8 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
RoleArn: controlPlaneRoleARN,
Tags: map[string]string{
"tmc.cloud.vmware.com/tmc-managed": "true",
"testclustertag": "testclustertagvalue",
"testingtag": "testingtagvalue",
"testsametag": "testsametagval",
"testtag": "testval",
"newtesttag": "newtestval",
},
KubernetesNetworkConfig: &eksmodel.VmwareTanzuManageV1alpha1EksclusterKubernetesNetworkConfig{
ServiceCidr: "10.100.0.0/16",
Expand Down Expand Up @@ -470,10 +469,9 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
CapacityType: "ON_DEMAND",
RootDiskSize: 40,
Tags: map[string]string{
"testnptag": "testnptagvalue",
"testingtag": "testingnptagvalue",
"testsametag": "testsametagval",
"testclustertag": "testclustertagvalue",
"testnptag": "testnptagvalue",
"newtesttag": "testingtagvalue",
"testtag": "testval",
},
NodeLabels: map[string]string{
"testnplabelkey": "testnplabelvalue",
Expand Down Expand Up @@ -514,10 +512,9 @@ func getMockEksClusterSpec(accountID string, templateID string) (eksmodel.Vmware
Spec: &eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolSpec{
RoleArn: workerRoleArn,
Tags: map[string]string{
"testnptag": "testnptagvalue",
"testingtag": "testingnptagvalue",
"testsametag": "testsametagval",
"testclustertag": "testclustertagvalue",
"testnptag": "testnptagvalue",
"newtesttag": "testingtagvalue",
"testtag": "testval",
},
NodeLabels: map[string]string{
"testnplabelkey": "testnplabelvalue",
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/testing/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const testDefaultCreateEksClusterScript = `
config {
kubernetes_version = "{{.KubernetesVersion}}"
role_arn = "arn:aws:iam::{{.AWSAccountNumber}}:role/control-plane.{{.CloudFormationTemplateID}}.eks.tmc.cloud.vmware.com"
tags = { "testclustertag" : "testclustertagvalue", "testingtag": "testingtagvalue", "testsametag":"testsametagval"}
tags = { "testtag" : "testval", "newtesttag": "newtestval"}
kubernetes_network_config {
service_cidr = "10.100.0.0/16" // Forces new
}
Expand Down Expand Up @@ -66,7 +66,7 @@ const testDefaultCreateEksClusterScript = `
security_groups = ["sg-0a6768722e9716768"]
}
root_disk_size = 40 // Default: 20GiB, forces New
tags = { "testnptag" : "testnptagvalue", "testingtag": "testingnptagvalue"}
tags = { "testnptag" : "testnptagvalue", "newtesttag": "testingtagvalue"}
node_labels = { "testnplabelkey" : "testnplabelvalue" }
subnet_ids = [ // Required, forces new
"subnet-0a184f6302af32a86",
Expand Down Expand Up @@ -97,7 +97,7 @@ const testDefaultCreateEksClusterScript = `
spec {
// Refer to nodepool's schema
role_arn = "arn:aws:iam::{{.AWSAccountNumber}}:role/worker.{{.CloudFormationTemplateID}}.eks.tmc.cloud.vmware.com"
tags = { "testnptag" : "testnptagvalue", "testingtag": "testingnptagvalue"}
tags = { "testnptag" : "testnptagvalue", "newtesttag": "testingtagvalue"}
node_labels = { "testnplabelkey" : "testnplabelvalue" }
launch_template {
name = "PLACE_HOLDER"
Expand Down

0 comments on commit 7ad4b6a

Please sign in to comment.