Skip to content

Commit ef32333

Browse files
authored
adding eni owner tag if cluster name is present (#3228)
1 parent 2532faf commit ef32333

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

pkg/awsutils/awsutils.go

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ const (
6464
eniNodeTagKey = "node.k8s.amazonaws.com/instance_id"
6565
eniCreatedAtTagKey = "node.k8s.amazonaws.com/createdAt"
6666
eniClusterTagKey = "cluster.k8s.amazonaws.com/name"
67+
eniOwnerTagKey = "eks:eni:owner"
68+
eniOwnerTagValue = "amazon-vpc-cni"
6769
additionalEniTagsEnvVar = "ADDITIONAL_ENI_TAGS"
6870
reservedTagKeyPrefix = "k8s.amazonaws.com"
6971
subnetDiscoveryTagKey = "kubernetes.io/role/cni"
@@ -1060,6 +1062,7 @@ func (cache *EC2InstanceMetadataCache) buildENITags() map[string]string {
10601062
// tag the ENI with "cluster.k8s.amazonaws.com/name=<cluster_name>"
10611063
if cache.clusterName != "" {
10621064
tags[eniClusterTagKey] = cache.clusterName
1065+
tags[eniOwnerTagKey] = eniOwnerTagValue
10631066
}
10641067
for key, value := range cache.additionalENITags {
10651068
tags[key] = value

pkg/awsutils/awsutils_test.go

+57-43
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ func TestEC2InstanceMetadataCache_buildENITags(t *testing.T) {
13041304
instanceID: "i-xxxxx",
13051305
},
13061306
want: map[string]string{
1307-
"node.k8s.amazonaws.com/instance_id": "i-xxxxx",
1307+
eniNodeTagKey: "i-xxxxx",
13081308
},
13091309
},
13101310
{
@@ -1314,8 +1314,9 @@ func TestEC2InstanceMetadataCache_buildENITags(t *testing.T) {
13141314
clusterName: "awesome-cluster",
13151315
},
13161316
want: map[string]string{
1317-
"node.k8s.amazonaws.com/instance_id": "i-xxxxx",
1318-
"cluster.k8s.amazonaws.com/name": "awesome-cluster",
1317+
eniNodeTagKey: "i-xxxxx",
1318+
eniClusterTagKey: "awesome-cluster",
1319+
eniOwnerTagKey: eniOwnerTagValue,
13191320
},
13201321
},
13211322
{
@@ -1328,9 +1329,9 @@ func TestEC2InstanceMetadataCache_buildENITags(t *testing.T) {
13281329
},
13291330
},
13301331
want: map[string]string{
1331-
"node.k8s.amazonaws.com/instance_id": "i-xxxxx",
1332-
"tagKey-1": "tagVal-1",
1333-
"tagKey-2": "tagVal-2",
1332+
eniNodeTagKey: "i-xxxxx",
1333+
"tagKey-1": "tagVal-1",
1334+
"tagKey-2": "tagVal-2",
13341335
},
13351336
},
13361337
}
@@ -1375,7 +1376,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
13751376
Filters: []ec2types.Filter{
13761377
{
13771378
Name: aws.String("tag-key"),
1378-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1379+
Values: []string{eniNodeTagKey},
13791380
},
13801381
{
13811382
Name: aws.String("status"),
@@ -1408,7 +1409,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
14081409
Filters: []ec2types.Filter{
14091410
{
14101411
Name: aws.String("tag-key"),
1411-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1412+
Values: []string{eniNodeTagKey},
14121413
},
14131414
{
14141415
Name: aws.String("status"),
@@ -1430,11 +1431,11 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
14301431
Status: "available",
14311432
TagSet: []ec2types.Tag{
14321433
{
1433-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1434+
Key: aws.String(eniNodeTagKey),
14341435
Value: aws.String("i-xxxxx"),
14351436
},
14361437
{
1437-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1438+
Key: aws.String(eniCreatedAtTagKey),
14381439
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
14391440
},
14401441
},
@@ -1452,11 +1453,11 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
14521453
Status: "available",
14531454
TagSet: []ec2types.Tag{
14541455
{
1455-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1456+
Key: aws.String(eniNodeTagKey),
14561457
Value: aws.String("i-xxxxx"),
14571458
},
14581459
{
1459-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1460+
Key: aws.String(eniCreatedAtTagKey),
14601461
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
14611462
},
14621463
},
@@ -1473,7 +1474,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
14731474
Filters: []ec2types.Filter{
14741475
{
14751476
Name: aws.String("tag-key"),
1476-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1477+
Values: []string{eniNodeTagKey},
14771478
},
14781479
{
14791480
Name: aws.String("status"),
@@ -1495,11 +1496,11 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
14951496
Status: "available",
14961497
TagSet: []ec2types.Tag{
14971498
{
1498-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1499+
Key: aws.String(eniNodeTagKey),
14991500
Value: aws.String("i-xxxxx"),
15001501
},
15011502
{
1502-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1503+
Key: aws.String(eniCreatedAtTagKey),
15031504
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
15041505
},
15051506
},
@@ -1522,7 +1523,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
15221523
Filters: []ec2types.Filter{
15231524
{
15241525
Name: aws.String("tag-key"),
1525-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1526+
Values: []string{eniNodeTagKey},
15261527
},
15271528
{
15281529
Name: aws.String("status"),
@@ -1544,11 +1545,11 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
15441545
Status: "available",
15451546
TagSet: []ec2types.Tag{
15461547
{
1547-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1548+
Key: aws.String(eniNodeTagKey),
15481549
Value: aws.String("i-xxxxx"),
15491550
},
15501551
{
1551-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1552+
Key: aws.String(eniCreatedAtTagKey),
15521553
Value: aws.String(now.Format(time.RFC3339)),
15531554
},
15541555
},
@@ -1571,7 +1572,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
15711572
Filters: []ec2types.Filter{
15721573
{
15731574
Name: aws.String("tag-key"),
1574-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1575+
Values: []string{eniNodeTagKey},
15751576
},
15761577
{
15771578
Name: aws.String("status"),
@@ -1604,7 +1605,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
16041605
Filters: []ec2types.Filter{
16051606
{
16061607
Name: aws.String("tag-key"),
1607-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1608+
Values: []string{eniNodeTagKey},
16081609
},
16091610
{
16101611
Name: aws.String("status"),
@@ -1630,15 +1631,15 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
16301631
Status: "available",
16311632
TagSet: []ec2types.Tag{
16321633
{
1633-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1634+
Key: aws.String(eniNodeTagKey),
16341635
Value: aws.String("i-xxxxx"),
16351636
},
16361637
{
1637-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1638+
Key: aws.String(eniCreatedAtTagKey),
16381639
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
16391640
},
16401641
{
1641-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1642+
Key: aws.String(eniClusterTagKey),
16421643
Value: aws.String("awesome-cluster"),
16431644
},
16441645
},
@@ -1656,15 +1657,15 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
16561657
Status: "available",
16571658
TagSet: []ec2types.Tag{
16581659
{
1659-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1660+
Key: aws.String(eniNodeTagKey),
16601661
Value: aws.String("i-xxxxx"),
16611662
},
16621663
{
1663-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1664+
Key: aws.String(eniCreatedAtTagKey),
16641665
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
16651666
},
16661667
{
1667-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1668+
Key: aws.String(eniClusterTagKey),
16681669
Value: aws.String("awesome-cluster"),
16691670
},
16701671
},
@@ -1681,7 +1682,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
16811682
Filters: []ec2types.Filter{
16821683
{
16831684
Name: aws.String("tag-key"),
1684-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1685+
Values: []string{eniNodeTagKey},
16851686
},
16861687
{
16871688
Name: aws.String("status"),
@@ -1707,15 +1708,15 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
17071708
Status: "available",
17081709
TagSet: []ec2types.Tag{
17091710
{
1710-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1711+
Key: aws.String(eniNodeTagKey),
17111712
Value: aws.String("i-xxxxx"),
17121713
},
17131714
{
1714-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1715+
Key: aws.String(eniCreatedAtTagKey),
17151716
Value: aws.String(tenMinuteAgo.Format(time.RFC3339)),
17161717
},
17171718
{
1718-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1719+
Key: aws.String(eniClusterTagKey),
17191720
Value: aws.String("awesome-cluster"),
17201721
},
17211722
},
@@ -1738,7 +1739,7 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
17381739
Filters: []ec2types.Filter{
17391740
{
17401741
Name: aws.String("tag-key"),
1741-
Values: []string{"node.k8s.amazonaws.com/instance_id"},
1742+
Values: []string{eniNodeTagKey},
17421743
},
17431744
{
17441745
Name: aws.String("status"),
@@ -1764,15 +1765,15 @@ func TestEC2InstanceMetadataCache_getLeakedENIs(t *testing.T) {
17641765
Status: "available",
17651766
TagSet: []ec2types.Tag{
17661767
{
1767-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1768+
Key: aws.String(eniNodeTagKey),
17681769
Value: aws.String("i-xxxxx"),
17691770
},
17701771
{
1771-
Key: aws.String("node.k8s.amazonaws.com/createdAt"),
1772+
Key: aws.String(eniCreatedAtTagKey),
17721773
Value: aws.String(now.Format(time.RFC3339)),
17731774
},
17741775
{
1775-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1776+
Key: aws.String(eniClusterTagKey),
17761777
Value: aws.String("awesome-cluster"),
17771778
},
17781779
},
@@ -1852,11 +1853,15 @@ func TestEC2InstanceMetadataCache_TagENI(t *testing.T) {
18521853
Resources: []string{"eni-xxxx"},
18531854
Tags: []ec2types.Tag{
18541855
{
1855-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1856+
Key: aws.String(eniClusterTagKey),
18561857
Value: aws.String("awesome-cluster"),
18571858
},
18581859
{
1859-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1860+
Key: aws.String(eniOwnerTagKey),
1861+
Value: aws.String(eniOwnerTagValue),
1862+
},
1863+
{
1864+
Key: aws.String(eniNodeTagKey),
18601865
Value: aws.String("i-xxxx"),
18611866
},
18621867
},
@@ -1880,8 +1885,9 @@ func TestEC2InstanceMetadataCache_TagENI(t *testing.T) {
18801885
args: args{
18811886
eniID: "eni-xxxx",
18821887
currentTags: map[string]string{
1883-
"node.k8s.amazonaws.com/instance_id": "i-xxxx",
1884-
"cluster.k8s.amazonaws.com/name": "awesome-cluster",
1888+
eniNodeTagKey: "i-xxxx",
1889+
eniClusterTagKey: "awesome-cluster",
1890+
eniOwnerTagKey: eniOwnerTagValue,
18851891
},
18861892
},
18871893
wantErr: nil,
@@ -1897,9 +1903,13 @@ func TestEC2InstanceMetadataCache_TagENI(t *testing.T) {
18971903
Resources: []string{"eni-xxxx"},
18981904
Tags: []ec2types.Tag{
18991905
{
1900-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1906+
Key: aws.String(eniClusterTagKey),
19011907
Value: aws.String("awesome-cluster"),
19021908
},
1909+
{
1910+
Key: aws.String(eniOwnerTagKey),
1911+
Value: aws.String(eniOwnerTagValue),
1912+
},
19031913
},
19041914
},
19051915
},
@@ -1908,8 +1918,8 @@ func TestEC2InstanceMetadataCache_TagENI(t *testing.T) {
19081918
args: args{
19091919
eniID: "eni-xxxx",
19101920
currentTags: map[string]string{
1911-
"node.k8s.amazonaws.com/instance_id": "i-xxxx",
1912-
"anotherKey": "anotherDay",
1921+
eniNodeTagKey: "i-xxxx",
1922+
"anotherKey": "anotherDay",
19131923
},
19141924
},
19151925
wantErr: nil,
@@ -1925,11 +1935,15 @@ func TestEC2InstanceMetadataCache_TagENI(t *testing.T) {
19251935
Resources: []string{"eni-xxxx"},
19261936
Tags: []ec2types.Tag{
19271937
{
1928-
Key: aws.String("cluster.k8s.amazonaws.com/name"),
1938+
Key: aws.String(eniClusterTagKey),
19291939
Value: aws.String("awesome-cluster"),
19301940
},
19311941
{
1932-
Key: aws.String("node.k8s.amazonaws.com/instance_id"),
1942+
Key: aws.String(eniOwnerTagKey),
1943+
Value: aws.String(eniOwnerTagValue),
1944+
},
1945+
{
1946+
Key: aws.String(eniNodeTagKey),
19331947
Value: aws.String("i-xxxx"),
19341948
},
19351949
},

0 commit comments

Comments
 (0)