@@ -30,6 +30,7 @@ import (
30
30
"github.com/aws/amazon-vpc-cni-k8s/pkg/ipamd/datastore"
31
31
32
32
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/awssession"
33
+ "github.com/aws/amazon-vpc-cni-k8s/pkg/config"
33
34
"github.com/aws/amazon-vpc-cni-k8s/pkg/ec2wrapper"
34
35
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/eventrecorder"
35
36
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
@@ -54,11 +55,11 @@ const (
54
55
55
56
// AllocENI need to choose a first free device number between 0 and maxENI
56
57
// 100 is a hard limit because we use vlanID + 100 for pod networking table names
57
- maxENIs = 100
58
- clusterNameEnvVar = "CLUSTER_NAME"
59
- eniNodeTagKey = "node.k8s.amazonaws.com/instance_id"
60
- eniCreatedAtTagKey = "node.k8s.amazonaws.com/createdAt"
61
- eniClusterTagKey = "cluster.k8s.amazonaws.com/name"
58
+ maxENIs = 100
59
+
60
+ // ENI tags
61
+ eniCreatedAtTagKey = "node.k8s.amazonaws.com/createdAt"
62
+
62
63
additionalEniTagsEnvVar = "ADDITIONAL_ENI_TAGS"
63
64
reservedTagKeyPrefix = "k8s.amazonaws.com"
64
65
subnetDiscoveryTagKey = "kubernetes.io/role/cni"
@@ -213,6 +214,8 @@ type EC2InstanceMetadataCache struct {
213
214
enablePrefixDelegation bool
214
215
215
216
clusterName string
217
+ clusterNameEnvVal string
218
+ nodeName string
216
219
additionalENITags map [string ]string
217
220
218
221
imds TypedIMDS
@@ -353,15 +356,17 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)
353
356
}
354
357
355
358
// New creates an EC2InstanceMetadataCache
356
- func New (useSubnetDiscovery , useCustomNetworking , disableLeakedENICleanup , v4Enabled , v6Enabled bool ) (* EC2InstanceMetadataCache , error ) {
359
+ func New (useSubnetDiscovery , useCustomNetworking , disableLeakedENICleanup , v4Enabled , v6Enabled bool , clusterName , nodeName string ) (* EC2InstanceMetadataCache , error ) {
357
360
// ctx is passed to initWithEC2Metadata func to cancel spawned go-routines when tests are run
358
361
ctx := context .Background ()
359
362
360
363
sess := awssession .New ()
361
364
ec2Metadata := ec2metadata .New (sess )
362
365
cache := & EC2InstanceMetadataCache {}
363
366
cache .imds = TypedIMDS {instrumentedIMDS {ec2Metadata }}
364
- cache .clusterName = os .Getenv (clusterNameEnvVar )
367
+ cache .clusterName = clusterName
368
+ cache .clusterNameEnvVal = os .Getenv (config .ClusterNameEnv )
369
+ cache .nodeName = nodeName
365
370
cache .additionalENITags = loadAdditionalENITags ()
366
371
367
372
region , err := ec2Metadata .Region ()
@@ -982,14 +987,24 @@ func (cache *EC2InstanceMetadataCache) tryCreateNetworkInterface(input *ec2.Crea
982
987
// buildENITags computes the desired AWS Tags for eni
983
988
func (cache * EC2InstanceMetadataCache ) buildENITags () map [string ]string {
984
989
tags := map [string ]string {
985
- eniNodeTagKey : cache .instanceID ,
990
+ // TODO: deprecate instance ID tag to replace with nodename to align with tag used in vpc-resource-controller
991
+ config .ENIInstanceIDTag : cache .instanceID ,
986
992
}
987
993
988
- // If clusterName is provided,
989
- // tag the ENI with "cluster.k8s.amazonaws.com/name=<cluster_name>"
994
+ // clusterName is set from CNINode created by vpc-resource-controller, add the new tags only when it is set so controller can deleted leaked ENIs
995
+ // If it is not set then likely the controller is not running, so skip
990
996
if cache .clusterName != "" {
991
- tags [eniClusterTagKey ] = cache .clusterName
997
+ tags [fmt .Sprintf (config .ClusterNameTagKeyFormat , cache .clusterName )] = config .ClusterNameTagValue
998
+ tags [config .ENINodeNameTagKey ] = cache .nodeName
999
+ tags [config .ENIOwnerTagKey ] = config .ENIOwnerTagValue
1000
+ }
1001
+
1002
+ if cache .clusterNameEnvVal != "" {
1003
+ // TODO: deprecate this tag to replace with "kubernetes.io/cluster/<cluster-name>:owned" to align with tag used in vpc-resource-controller
1004
+ // for backward compatibily, add tag if CLUSTER_NAME ENV is set
1005
+ tags [config .ClusterNameTagKey ] = cache .clusterNameEnvVal
992
1006
}
1007
+
993
1008
for key , value := range cache .additionalENITags {
994
1009
tags [key ] = value
995
1010
}
@@ -1877,7 +1892,7 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
1877
1892
{
1878
1893
Name : aws .String ("tag-key" ),
1879
1894
Values : []* string {
1880
- aws .String (eniNodeTagKey ),
1895
+ aws .String (config . ENIInstanceIDTag ),
1881
1896
},
1882
1897
},
1883
1898
{
@@ -1893,11 +1908,11 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
1893
1908
},
1894
1909
},
1895
1910
}
1896
- if cache .clusterName != "" {
1911
+ if cache .clusterNameEnvVal != "" {
1897
1912
leakedENIFilters = append (leakedENIFilters , & ec2.Filter {
1898
- Name : aws .String (fmt .Sprintf ("tag:%s" , eniClusterTagKey )),
1913
+ Name : aws .String (fmt .Sprintf ("tag:%s" , config . ClusterNameTagKey )),
1899
1914
Values : []* string {
1900
- aws .String (cache .clusterName ),
1915
+ aws .String (cache .clusterNameEnvVal ),
1901
1916
},
1902
1917
})
1903
1918
}
0 commit comments