@@ -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,7 +31,9 @@ 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
@@ -49,6 +52,19 @@ type awsTagging struct {
49
52
ClusterName string
50
53
}
51
54
55
+ // newAWSTags is a constructor function for awsTagging
56
+ func newAWSTags (clusterName string ) (awsTagging , error ) {
57
+ if clusterName != "" {
58
+ klog .Infof ("AWS cloud filtering on ClusterName: %v" , clusterName )
59
+ } else {
60
+ return awsTagging {}, errors .New ("AWS cloud failed to find ClusterName" )
61
+ }
62
+
63
+ return awsTagging {
64
+ ClusterName : clusterName ,
65
+ }, nil
66
+ }
67
+
52
68
// Extracts the cluster name from the given tags, if they are present
53
69
// If duplicate tags are found, returns an error
54
70
func findClusterName (tags []* ec2.Tag ) (string , error ) {
@@ -57,7 +73,7 @@ func findClusterName(tags []*ec2.Tag) (string, error) {
57
73
for _ , tag := range tags {
58
74
tagKey := aws .StringValue (tag .Key )
59
75
if strings .HasPrefix (tagKey , TagNameKubernetesClusterPrefix ) {
60
- name := aws . StringValue ( tag . Value )
76
+ name := strings . TrimPrefix ( tagKey , TagNameKubernetesClusterPrefix )
61
77
if clusterName != "" {
62
78
return "" , fmt .Errorf ("Found multiple cluster tags with prefix %s (%q and %q)" , TagNameKubernetesClusterPrefix , clusterName , name )
63
79
}
@@ -68,48 +84,19 @@ func findClusterName(tags []*ec2.Tag) (string, error) {
68
84
return clusterName , nil
69
85
}
70
86
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.
87
+ func (t * awsTagging ) hasClusterTag (tags []* ec2.Tag ) error {
101
88
if len (t .ClusterName ) == 0 {
102
- return true
89
+ return errors . New ( "cluster name is not configured (an empty value)" )
103
90
}
104
91
105
92
for _ , tag := range tags {
106
93
tagKey := aws .StringValue (tag .Key )
107
- if ( tagKey == TagNameKubernetesClusterPrefix ) && ( aws . StringValue ( tag . Value ) == t .ClusterName ) {
108
- return true
94
+ if tagKey == ( TagNameKubernetesClusterPrefix + t .ClusterName ) {
95
+ return nil
109
96
}
110
97
}
111
98
112
- return false
99
+ return errors . New ( "cluster tag does not exist" )
113
100
}
114
101
115
102
func (t * awsTagging ) buildTags (additionalTags map [string ]string , lifecycle string ) map [string ]string {
@@ -124,8 +111,9 @@ func (t *awsTagging) buildTags(additionalTags map[string]string, lifecycle strin
124
111
return tags
125
112
}
126
113
127
- // tag format: kubernetes.io/cluster/=<clusterName>
128
- tags [TagNameKubernetesClusterPrefix ] = t .ClusterName
114
+ // tag format: kubernetes.io/cluster/<clusterID> = shared|owned
115
+ tagKey := TagNameKubernetesClusterPrefix + t .ClusterName
116
+ tags [tagKey ] = lifecycle
129
117
130
118
return tags
131
119
}
0 commit comments