Skip to content

Commit

Permalink
Revert "disable leaked eni cleanup routine when vpc-resource-controll…
Browse files Browse the repository at this point in the history
…er is deployed (#2854)" (#2987)

* Revert "disable leaked eni cleanup routine when vpc-resource-controller is deployed (#2854)"

This reverts commit 9fdcb5f.

* Fix go.mod dependencies.
  • Loading branch information
orsenthil committed Jul 17, 2024
1 parent 2e00de5 commit e8faf2f
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 444 deletions.
45 changes: 15 additions & 30 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/ipamd/datastore"

"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/awssession"
"github.com/aws/amazon-vpc-cni-k8s/pkg/config"
"github.com/aws/amazon-vpc-cni-k8s/pkg/ec2wrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/eventrecorder"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
Expand All @@ -55,11 +54,11 @@ const (

// AllocENI need to choose a first free device number between 0 and maxENI
// 100 is a hard limit because we use vlanID + 100 for pod networking table names
maxENIs = 100

// ENI tags
eniCreatedAtTagKey = "node.k8s.amazonaws.com/createdAt"

maxENIs = 100
clusterNameEnvVar = "CLUSTER_NAME"
eniNodeTagKey = "node.k8s.amazonaws.com/instance_id"
eniCreatedAtTagKey = "node.k8s.amazonaws.com/createdAt"
eniClusterTagKey = "cluster.k8s.amazonaws.com/name"
additionalEniTagsEnvVar = "ADDITIONAL_ENI_TAGS"
reservedTagKeyPrefix = "k8s.amazonaws.com"
subnetDiscoveryTagKey = "kubernetes.io/role/cni"
Expand Down Expand Up @@ -214,8 +213,6 @@ type EC2InstanceMetadataCache struct {
enablePrefixDelegation bool

clusterName string
clusterNameEnvVal string
nodeName string
additionalENITags map[string]string

imds TypedIMDS
Expand Down Expand Up @@ -356,17 +353,15 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)
}

// New creates an EC2InstanceMetadataCache
func New(useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Enabled, v6Enabled bool, clusterName, nodeName string) (*EC2InstanceMetadataCache, error) {
func New(useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Enabled, v6Enabled bool) (*EC2InstanceMetadataCache, error) {
// ctx is passed to initWithEC2Metadata func to cancel spawned go-routines when tests are run
ctx := context.Background()

sess := awssession.New()
ec2Metadata := ec2metadata.New(sess)
cache := &EC2InstanceMetadataCache{}
cache.imds = TypedIMDS{instrumentedIMDS{ec2Metadata}}
cache.clusterName = clusterName
cache.clusterNameEnvVal = os.Getenv(config.ClusterNameEnv)
cache.nodeName = nodeName
cache.clusterName = os.Getenv(clusterNameEnvVar)
cache.additionalENITags = loadAdditionalENITags()

region, err := ec2Metadata.Region()
Expand Down Expand Up @@ -987,24 +982,14 @@ func (cache *EC2InstanceMetadataCache) tryCreateNetworkInterface(input *ec2.Crea
// buildENITags computes the desired AWS Tags for eni
func (cache *EC2InstanceMetadataCache) buildENITags() map[string]string {
tags := map[string]string{
// TODO: deprecate instance ID tag to replace with nodename to align with tag used in vpc-resource-controller
config.ENIInstanceIDTag: cache.instanceID,
eniNodeTagKey: cache.instanceID,
}

// 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
// If it is not set then likely the controller is not running, so skip
// If clusterName is provided,
// tag the ENI with "cluster.k8s.amazonaws.com/name=<cluster_name>"
if cache.clusterName != "" {
tags[fmt.Sprintf(config.ClusterNameTagKeyFormat, cache.clusterName)] = config.ClusterNameTagValue
tags[config.ENINodeNameTagKey] = cache.nodeName
tags[config.ENIOwnerTagKey] = config.ENIOwnerTagValue
}

if cache.clusterNameEnvVal != "" {
// TODO: deprecate this tag to replace with "kubernetes.io/cluster/<cluster-name>:owned" to align with tag used in vpc-resource-controller
// for backward compatibily, add tag if CLUSTER_NAME ENV is set
tags[config.ClusterNameTagKey] = cache.clusterNameEnvVal
tags[eniClusterTagKey] = cache.clusterName
}

for key, value := range cache.additionalENITags {
tags[key] = value
}
Expand Down Expand Up @@ -1892,7 +1877,7 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
{
Name: aws.String("tag-key"),
Values: []*string{
aws.String(config.ENIInstanceIDTag),
aws.String(eniNodeTagKey),
},
},
{
Expand All @@ -1908,11 +1893,11 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
},
},
}
if cache.clusterNameEnvVal != "" {
if cache.clusterName != "" {
leakedENIFilters = append(leakedENIFilters, &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", config.ClusterNameTagKey)),
Name: aws.String(fmt.Sprintf("tag:%s", eniClusterTagKey)),
Values: []*string{
aws.String(cache.clusterNameEnvVal),
aws.String(cache.clusterName),
},
})
}
Expand Down
Loading

0 comments on commit e8faf2f

Please sign in to comment.