Skip to content

Commit 53c8739

Browse files
committed
tags: initial implementation of tags
1 parent 3d37978 commit 53c8739

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

Diff for: pkg/providers/v2/tags.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import (
3030
const (
3131
// TagNameKubernetesClusterPrefix is the tag name we use to differentiate multiple
3232
// logically independent clusters running in the same AZ.
33-
// tag format: kubernetes.io/cluster/=<clusterName>
33+
// tag format: kubernetes.io/cluster/<clusterID> = shared|owned
34+
// The tag key = TagNameKubernetesClusterPrefix + clusterID
35+
// The tag value is an ownership value
3436
TagNameKubernetesClusterPrefix = "kubernetes.io/cluster/"
3537

3638
// createTag* is configuration of exponential backoff for CreateTag call. We
@@ -57,7 +59,7 @@ func findClusterName(tags []*ec2.Tag) (string, error) {
5759
for _, tag := range tags {
5860
tagKey := aws.StringValue(tag.Key)
5961
if strings.HasPrefix(tagKey, TagNameKubernetesClusterPrefix) {
60-
name := aws.StringValue(tag.Value)
62+
name := strings.TrimPrefix(tagKey, TagNameKubernetesClusterPrefix)
6163
if clusterName != "" {
6264
return "", fmt.Errorf("Found multiple cluster tags with prefix %s (%q and %q)", TagNameKubernetesClusterPrefix, clusterName, name)
6365
}
@@ -104,7 +106,7 @@ func (t *awsTagging) hasClusterTag(tags []*ec2.Tag) bool {
104106

105107
for _, tag := range tags {
106108
tagKey := aws.StringValue(tag.Key)
107-
if (tagKey == TagNameKubernetesClusterPrefix) && (aws.StringValue(tag.Value) == t.ClusterName) {
109+
if tagKey == (TagNameKubernetesClusterPrefix + t.ClusterName) {
108110
return true
109111
}
110112
}

Diff for: pkg/providers/v2/tags_test.go

+28-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func TestFindClusterName(t *testing.T) {
26-
grid := []struct {
26+
testCases := []struct {
2727
Tags map[string]string
2828
ExpectedClusterName string
2929
ExpectError bool
@@ -33,7 +33,19 @@ func TestFindClusterName(t *testing.T) {
3333
},
3434
{
3535
Tags: map[string]string{
36-
TagNameKubernetesClusterPrefix: TestClusterID,
36+
TagNameKubernetesClusterPrefix + TestClusterID: "owned",
37+
},
38+
ExpectedClusterName: TestClusterID,
39+
},
40+
{
41+
Tags: map[string]string{
42+
TagNameKubernetesClusterPrefix + TestClusterID: "shared",
43+
},
44+
ExpectedClusterName: TestClusterID,
45+
},
46+
{
47+
Tags: map[string]string{
48+
TagNameKubernetesClusterPrefix + TestClusterID: "",
3749
},
3850
ExpectedClusterName: TestClusterID,
3951
},
@@ -43,26 +55,33 @@ func TestFindClusterName(t *testing.T) {
4355
},
4456
ExpectedClusterName: "",
4557
},
58+
{
59+
Tags: map[string]string{
60+
TagNameKubernetesClusterPrefix + "a": "",
61+
TagNameKubernetesClusterPrefix + "b": "",
62+
},
63+
ExpectError: true,
64+
},
4665
}
47-
for _, g := range grid {
66+
for _, testCase := range testCases {
4867
var ec2Tags []*ec2.Tag
49-
for k, v := range g.Tags {
68+
for k, v := range testCase.Tags {
5069
ec2Tags = append(ec2Tags, &ec2.Tag{Key: aws.String(k), Value: aws.String(v)})
5170
}
5271
clusterName, err := findClusterName(ec2Tags)
53-
if g.ExpectError {
72+
if testCase.ExpectError {
5473
if err == nil {
55-
t.Errorf("expected error for tags %v", g.Tags)
74+
t.Errorf("expected error for tags %v", testCase.Tags)
5675
continue
5776
}
5877
} else {
5978
if err != nil {
60-
t.Errorf("unexpected error for tags %v: %v", g.Tags, err)
79+
t.Errorf("unexpected error for tags %v: %v", testCase.Tags, err)
6180
continue
6281
}
6382

64-
if g.ExpectedClusterName != clusterName {
65-
t.Errorf("unexpected new clusterName for tags %v: %s vs %s", g.Tags, g.ExpectedClusterName, clusterName)
83+
if testCase.ExpectedClusterName != clusterName {
84+
t.Errorf("unexpected new clusterName for tags %v: %s vs %s", testCase.Tags, testCase.ExpectedClusterName, clusterName)
6685
continue
6786
}
6887
}

0 commit comments

Comments
 (0)