@@ -30,6 +30,7 @@ import (
3030 "github.com/aws/amazon-vpc-cni-k8s/pkg/ipamd/datastore"
3131
3232 "github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/awssession"
33+ "github.com/aws/amazon-vpc-cni-k8s/pkg/config"
3334 "github.com/aws/amazon-vpc-cni-k8s/pkg/ec2wrapper"
3435 "github.com/aws/amazon-vpc-cni-k8s/pkg/utils/eventrecorder"
3536 "github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
@@ -54,11 +55,11 @@ const (
5455
5556 // AllocENI need to choose a first free device number between 0 and maxENI
5657 // 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+
6263 additionalEniTagsEnvVar = "ADDITIONAL_ENI_TAGS"
6364 reservedTagKeyPrefix = "k8s.amazonaws.com"
6465 subnetDiscoveryTagKey = "kubernetes.io/role/cni"
@@ -213,6 +214,8 @@ type EC2InstanceMetadataCache struct {
213214 enablePrefixDelegation bool
214215
215216 clusterName string
217+ clusterNameEnvVal string
218+ nodeName string
216219 additionalENITags map [string ]string
217220
218221 imds TypedIMDS
@@ -353,15 +356,17 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)
353356}
354357
355358// 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 ) {
357360 // ctx is passed to initWithEC2Metadata func to cancel spawned go-routines when tests are run
358361 ctx := context .Background ()
359362
360363 sess := awssession .New ()
361364 ec2Metadata := ec2metadata .New (sess )
362365 cache := & EC2InstanceMetadataCache {}
363366 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
365370 cache .additionalENITags = loadAdditionalENITags ()
366371
367372 region , err := ec2Metadata .Region ()
@@ -982,14 +987,24 @@ func (cache *EC2InstanceMetadataCache) tryCreateNetworkInterface(input *ec2.Crea
982987// buildENITags computes the desired AWS Tags for eni
983988func (cache * EC2InstanceMetadataCache ) buildENITags () map [string ]string {
984989 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 ,
986992 }
987993
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
990996 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
9921006 }
1007+
9931008 for key , value := range cache .additionalENITags {
9941009 tags [key ] = value
9951010 }
@@ -1877,7 +1892,7 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
18771892 {
18781893 Name : aws .String ("tag-key" ),
18791894 Values : []* string {
1880- aws .String (eniNodeTagKey ),
1895+ aws .String (config . ENIInstanceIDTag ),
18811896 },
18821897 },
18831898 {
@@ -1893,11 +1908,11 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
18931908 },
18941909 },
18951910 }
1896- if cache .clusterName != "" {
1911+ if cache .clusterNameEnvVal != "" {
18971912 leakedENIFilters = append (leakedENIFilters , & ec2.Filter {
1898- Name : aws .String (fmt .Sprintf ("tag:%s" , eniClusterTagKey )),
1913+ Name : aws .String (fmt .Sprintf ("tag:%s" , config . ClusterNameTagKey )),
18991914 Values : []* string {
1900- aws .String (cache .clusterName ),
1915+ aws .String (cache .clusterNameEnvVal ),
19011916 },
19021917 })
19031918 }
0 commit comments