@@ -16,6 +16,7 @@ limitations under the License.
16
16
package v2
17
17
18
18
import (
19
+ "errors"
19
20
"fmt"
20
21
"strings"
21
22
"time"
@@ -30,14 +31,17 @@ import (
30
31
const (
31
32
// TagNameKubernetesClusterPrefix is the tag name we use to differentiate multiple
32
33
// logically independent clusters running in the same AZ.
33
- // tag format: kubernetes.io/cluster/=<clusterName>
34
+ // tag format: kubernetes.io/cluster/<clusterID> = shared|owned
35
+ // The tag key = TagNameKubernetesClusterPrefix + clusterID
36
+ // The tag value is an ownership value
34
37
TagNameKubernetesClusterPrefix = "kubernetes.io/cluster/"
35
38
36
39
// createTag* is configuration of exponential backoff for CreateTag call. We
37
40
// retry mainly because if we create an object, we cannot tag it until it is
38
41
// "fully created" (eventual consistency). Starting with 1 second, doubling
39
42
// it every step and taking 9 steps results in 255 second total waiting
40
43
// time.
44
+ // TODO: revisit these values
41
45
createTagInitialDelay = 1 * time .Second
42
46
createTagFactor = 2.0
43
47
createTagSteps = 9
@@ -49,6 +53,19 @@ type awsTagging struct {
49
53
ClusterName string
50
54
}
51
55
56
+ // newAWSTags is a constructor function for awsTagging
57
+ func newAWSTags (clusterName string ) (awsTagging , error ) {
58
+ if clusterName != "" {
59
+ klog .Infof ("AWS cloud filtering on ClusterName: %v" , clusterName )
60
+ } else {
61
+ return awsTagging {}, errors .New ("No ClusterName found in the config" )
62
+ }
63
+
64
+ return awsTagging {
65
+ ClusterName : clusterName ,
66
+ }, nil
67
+ }
68
+
52
69
// Extracts the cluster name from the given tags, if they are present
53
70
// If duplicate tags are found, returns an error
54
71
func findClusterName (tags []* ec2.Tag ) (string , error ) {
@@ -57,7 +74,7 @@ func findClusterName(tags []*ec2.Tag) (string, error) {
57
74
for _ , tag := range tags {
58
75
tagKey := aws .StringValue (tag .Key )
59
76
if strings .HasPrefix (tagKey , TagNameKubernetesClusterPrefix ) {
60
- name := aws . StringValue ( tag . Value )
77
+ name := strings . TrimPrefix ( tagKey , TagNameKubernetesClusterPrefix )
61
78
if clusterName != "" {
62
79
return "" , fmt .Errorf ("Found multiple cluster tags with prefix %s (%q and %q)" , TagNameKubernetesClusterPrefix , clusterName , name )
63
80
}
@@ -68,64 +85,36 @@ func findClusterName(tags []*ec2.Tag) (string, error) {
68
85
return clusterName , nil
69
86
}
70
87
71
- func (t * awsTagging ) init (clusterName string ) error {
72
- t .ClusterName = clusterName
73
-
74
- if clusterName != "" {
75
- klog .Infof ("AWS cloud filtering on ClusterName: %v" , clusterName )
76
- } else {
77
- return fmt .Errorf ("AWS cloud failed to find ClusterName" )
78
- }
79
-
80
- return nil
81
- }
82
-
83
- // Extracts a cluster name from the given tags, if one is present
84
- // If no clusterName is found, returns "", nil
85
- // If multiple (different) clusterNames are found, returns an error
86
- func (t * awsTagging ) initFromTags (tags []* ec2.Tag ) error {
87
- clusterName , err := findClusterName (tags )
88
- if err != nil {
89
- return err
90
- }
91
-
92
- if clusterName == "" {
93
- klog .Errorf ("Tag %q not found; Kubernetes may behave unexpectedly." , TagNameKubernetesClusterPrefix )
94
- }
95
-
96
- return t .init (clusterName )
97
- }
98
-
99
- func (t * awsTagging ) hasClusterTag (tags []* ec2.Tag ) bool {
100
- // if the clusterName is not configured -- we consider all instances.
88
+ func (t * awsTagging ) hasClusterTag (tags []* ec2.Tag ) error {
101
89
if len (t .ClusterName ) == 0 {
102
- return true
90
+ return errors . New ( "cluster name is not configured (an empty value)" )
103
91
}
104
92
105
93
for _ , tag := range tags {
106
94
tagKey := aws .StringValue (tag .Key )
107
- if ( tagKey == TagNameKubernetesClusterPrefix ) && ( aws . StringValue ( tag . Value ) == t .ClusterName ) {
108
- return true
95
+ if tagKey == ( TagNameKubernetesClusterPrefix + t .ClusterName ) {
96
+ return nil
109
97
}
110
98
}
111
99
112
- return false
100
+ return errors . New ( "cluster tag does not exist" )
113
101
}
114
102
115
103
func (t * awsTagging ) buildTags (additionalTags map [string ]string , lifecycle string ) map [string ]string {
116
104
tags := make (map [string ]string )
117
- for k , v := range additionalTags {
118
- tags [k ] = v
105
+ for tagKey , tagValue := range additionalTags {
106
+ tags [tagKey ] = tagValue
119
107
}
120
108
121
109
// no clusterName is a sign of misconfigured cluster, but we can't be tagging the resources with empty
122
110
// strings
111
+ // TODO: revise the logic
123
112
if len (t .ClusterName ) == 0 {
124
113
return tags
125
114
}
126
115
127
- // tag format: kubernetes.io/cluster/=<clusterName>
128
- tags [TagNameKubernetesClusterPrefix ] = t . ClusterName
116
+ // tag format: kubernetes.io/cluster/<clusterID> = shared|owned
117
+ tags [TagNameKubernetesClusterPrefix + t . ClusterName ] = lifecycle
129
118
130
119
return tags
131
120
}
@@ -141,10 +130,10 @@ func (t *awsTagging) createTags(ec2Client EC2, resourceID string, lifecycle stri
141
130
}
142
131
143
132
var awsTags []* ec2.Tag
144
- for k , v := range tags {
133
+ for tagKey , tagValue := range tags {
145
134
tag := & ec2.Tag {
146
- Key : aws .String (k ),
147
- Value : aws .String (v ),
135
+ Key : aws .String (tagKey ),
136
+ Value : aws .String (tagValue ),
148
137
}
149
138
awsTags = append (awsTags , tag )
150
139
}
0 commit comments