14
14
package pod_eni
15
15
16
16
import (
17
- "net/url"
18
- "path"
17
+ "fmt"
19
18
"strings"
20
19
"testing"
21
20
@@ -24,7 +23,7 @@ import (
24
23
k8sUtils "github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/utils"
25
24
"github.com/aws/amazon-vpc-cni-k8s/test/framework/utils"
26
25
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/aws/vpc"
27
- v1 "k8s.io/api/core/v1"
26
+ corev1 "k8s.io/api/core/v1"
28
27
29
28
. "github.com/onsi/ginkgo/v2"
30
29
. "github.com/onsi/gomega"
@@ -35,26 +34,22 @@ const AmazonEKSVPCResourceControllerARN = "arn:aws:iam::aws:policy/AmazonEKSVPCR
35
34
var (
36
35
f * framework.Framework
37
36
err error
38
- // Key pair used for creating new self managed node group
39
- keyPairName = "pod-eni-test"
40
37
// Security Group that will be used to to create Security Group Policy
41
38
securityGroupId string
42
39
// Ports that will be opened on the Security Group used for testing
43
40
openPort = 80
44
- // Size of the Auto Scaling Group used for testing Security Group For Pods
45
- asgSize = 3
46
- // Nitro Based instance type only
47
- instanceType = "c5.xlarge"
41
+ // Port than metrics server listens on
42
+ metricsPort = 8080
48
43
// Maximum number of Branch Interface created across all the self managed nodes
49
44
totalBranchInterface int
50
- // Self managed node group
51
- nodeGroupProperties awsUtils.NodeGroupProperties
52
45
// Cluster Role name derived from cluster Role ARN, used to attach VPC Controller Policy
53
46
clusterRoleName string
54
- // NodeSecurityGroupId for Node-Node communication
55
- nodeSecurityGroupID string
47
+ // Cluster security group ID for node to node communication
48
+ clusterSGID string
56
49
57
- node v1.Node
50
+ targetNode corev1.Node
51
+ // Number of nodes in cluster
52
+ numNodes int
58
53
)
59
54
60
55
func TestSecurityGroupForPods (t * testing.T ) {
@@ -65,25 +60,15 @@ func TestSecurityGroupForPods(t *testing.T) {
65
60
var _ = BeforeSuite (func () {
66
61
f = framework .New (framework .GlobalOptions )
67
62
68
- By ("creating ec2 key-pair for the new node group" )
69
- _ , err := f .CloudServices .EC2 ().CreateKey (keyPairName )
70
- Expect (err ).ToNot (HaveOccurred ())
71
-
72
63
By ("creating a new security group used in Security Group Policy" )
73
64
securityGroupOutput , err := f .CloudServices .EC2 ().CreateSecurityGroup ("pod-eni-automation" ,
74
65
"test created by vpc cni automation test suite" , f .Options .AWSVPCID )
75
66
Expect (err ).ToNot (HaveOccurred ())
76
67
securityGroupId = * securityGroupOutput .GroupId
77
68
78
69
By ("authorizing egress and ingress on security group for client-server communication" )
79
- f .CloudServices .EC2 ().
80
- AuthorizeSecurityGroupEgress (securityGroupId , "TCP" , openPort , openPort , "0.0.0.0/0" )
81
- f .CloudServices .EC2 ().
82
- AuthorizeSecurityGroupIngress (securityGroupId , "TCP" , openPort , openPort , "0.0.0.0/0" )
83
-
84
- By ("getting the cluster VPC Config" )
85
- clusterVPCConfig , err := awsUtils .GetClusterVPCConfig (f )
86
- Expect (err ).ToNot (HaveOccurred ())
70
+ f .CloudServices .EC2 ().AuthorizeSecurityGroupEgress (securityGroupId , "TCP" , openPort , openPort , "0.0.0.0/0" )
71
+ f .CloudServices .EC2 ().AuthorizeSecurityGroupIngress (securityGroupId , "TCP" , openPort , openPort , "0.0.0.0/0" )
87
72
88
73
By ("getting the cluster role name" )
89
74
describeClusterOutput , err := f .CloudServices .EKS ().DescribeCluster (f .Options .ClusterName )
@@ -95,69 +80,38 @@ var _ = BeforeSuite(func() {
95
80
AttachRolePolicy (AmazonEKSVPCResourceControllerARN , clusterRoleName )
96
81
Expect (err ).ToNot (HaveOccurred ())
97
82
98
- nodeGroupProperties = awsUtils.NodeGroupProperties {
99
- NgLabelKey : "node-type" ,
100
- NgLabelVal : "pod-eni-node" ,
101
- AsgSize : asgSize ,
102
- NodeGroupName : "pod-eni-node" ,
103
- Subnet : clusterVPCConfig .PublicSubnetList ,
104
- InstanceType : instanceType ,
105
- KeyPairName : keyPairName ,
106
- ContainerRuntime : f .Options .ContainerRuntime ,
107
- }
108
-
109
- if f .Options .InstanceType == "arm64" {
110
- // override instanceType for arm64
111
- instanceType = "m6g.large"
112
- nodeGroupProperties .InstanceType = instanceType
113
- nodeGroupProperties .NodeImageId = "ami-087fca294139386b6"
114
- }
115
-
116
- totalBranchInterface = vpc .Limits [instanceType ].BranchInterface * asgSize
117
-
118
- By ("creating a new self managed node group" )
119
- err = awsUtils .CreateAndWaitTillSelfManagedNGReady (f , nodeGroupProperties )
83
+ By ("getting branch ENI limits" )
84
+ nodeList , err := f .K8sResourceManagers .NodeManager ().GetNodes (f .Options .NgNameLabelKey , f .Options .NgNameLabelVal )
120
85
Expect (err ).ToNot (HaveOccurred ())
86
+ numNodes = len (nodeList .Items )
87
+ Expect (numNodes ).Should (BeNumerically (">" , 1 ))
121
88
122
- By ("Get Reference to any node from the self managed node group" )
123
- nodeList , err := f .K8sResourceManagers .NodeManager ().GetNodes (nodeGroupProperties .NgLabelKey ,
124
- nodeGroupProperties .NgLabelVal )
125
- Expect (err ).ToNot (HaveOccurred ())
126
- Expect (len (nodeList .Items )).Should (BeNumerically (">" , 0 ))
127
-
128
- // Get ref to any node from newly created nodegroup
129
- By ("Getting providerID of the node" )
130
- node = nodeList .Items [0 ]
131
- providerID := node .Spec .ProviderID
132
- Expect (len (providerID )).To (BeNumerically (">" , 0 ))
133
-
134
- By ("Get InstanceID from the node" )
135
- awsUrl , err := url .Parse (providerID )
136
- Expect (err ).NotTo (HaveOccurred ())
137
-
138
- instanceID := path .Base (awsUrl .Path )
139
- Expect (len (instanceID )).To (BeNumerically (">" , 0 ))
89
+ node := nodeList .Items [0 ]
90
+ instanceID := k8sUtils .GetInstanceIDFromNode (node )
91
+ nodeInstance , err := f .CloudServices .EC2 ().DescribeInstance (instanceID )
92
+ instanceType := * nodeInstance .InstanceType
93
+ totalBranchInterface = vpc .Limits [instanceType ].BranchInterface * numNodes
140
94
141
- By ("Fetching Node Security GroupId " )
142
- instance , err := f .CloudServices .EC2 ().DescribeInstance ( instanceID )
95
+ By ("Getting Cluster Security Group ID " )
96
+ clusterRes , err := f .CloudServices .EKS ().DescribeCluster ( f . Options . ClusterName )
143
97
Expect (err ).NotTo (HaveOccurred ())
144
-
145
- networkInterface := instance .NetworkInterfaces [0 ]
146
- securityGroups := networkInterface .Groups
147
- nodeSecurityGroupPrefix := nodeGroupProperties .NgLabelVal + "-NodeSecurityGroup"
148
- for _ , group := range securityGroups {
149
- if strings .HasPrefix (* group .GroupName , nodeSecurityGroupPrefix ) {
150
- nodeSecurityGroupID = * group .GroupId
151
- break
152
- }
153
- }
154
- Expect (len (nodeSecurityGroupID )).To (BeNumerically (">" , 0 ))
98
+ clusterSGID = * (clusterRes .Cluster .ResourcesVpcConfig .ClusterSecurityGroupId )
99
+ fmt .Fprintf (GinkgoWriter , "cluster security group is %s\n " , clusterSGID )
155
100
156
101
By ("enabling pod eni on aws-node DaemonSet" )
157
102
k8sUtils .AddEnvVarToDaemonSetAndWaitTillUpdated (f , utils .AwsNodeName ,
158
103
utils .AwsNodeNamespace , utils .AwsNodeName , map [string ]string {
159
104
"ENABLE_POD_ENI" : "true" ,
160
105
})
106
+
107
+ By ("terminating instances" )
108
+ err = awsUtils .TerminateInstances (f , f .Options .NgNameLabelKey , f .Options .NgNameLabelVal )
109
+ Expect (err ).ToNot (HaveOccurred ())
110
+
111
+ By ("getting target node" )
112
+ nodeList , err = f .K8sResourceManagers .NodeManager ().GetNodes (f .Options .NgNameLabelKey , f .Options .NgNameLabelVal )
113
+ Expect (err ).ToNot (HaveOccurred ())
114
+ targetNode = nodeList .Items [0 ]
161
115
})
162
116
163
117
var _ = AfterSuite (func () {
@@ -167,20 +121,15 @@ var _ = AfterSuite(func() {
167
121
"ENABLE_POD_ENI" : {},
168
122
})
169
123
170
- By ("deleting the key-pair used to create nodegroup" )
171
- err = f .CloudServices .EC2 ().DeleteKey (keyPairName )
172
- Expect (err ).ToNot (HaveOccurred ())
173
-
174
- By ("deleting the self managed node group" )
175
- err = awsUtils .DeleteAndWaitTillSelfManagedNGStackDeleted (f , nodeGroupProperties )
124
+ By ("terminating instances" )
125
+ err := awsUtils .TerminateInstances (f , f .Options .NgNameLabelKey , f .Options .NgNameLabelVal )
176
126
Expect (err ).ToNot (HaveOccurred ())
177
127
178
128
By ("deleting the security group" )
179
129
err = f .CloudServices .EC2 ().DeleteSecurityGroup (securityGroupId )
180
130
Expect (err ).ToNot (HaveOccurred ())
181
131
182
132
By ("detaching the AmazonEKSVPCResourceController policy from the cluster role" )
183
- err = f .CloudServices .IAM ().
184
- DetachRolePolicy (AmazonEKSVPCResourceControllerARN , clusterRoleName )
133
+ err = f .CloudServices .IAM ().DetachRolePolicy (AmazonEKSVPCResourceControllerARN , clusterRoleName )
185
134
Expect (err ).ToNot (HaveOccurred ())
186
135
})
0 commit comments