@@ -18,6 +18,7 @@ package securitygroup
18
18
19
19
import (
20
20
"context"
21
+ "reflect"
21
22
"strings"
22
23
"testing"
23
24
@@ -34,6 +35,7 @@ import (
34
35
"sigs.k8s.io/controller-runtime/pkg/client/fake"
35
36
36
37
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
38
+ ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
37
39
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors"
38
40
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/filter"
39
41
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
@@ -1192,11 +1194,11 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1192
1194
_ = infrav1 .AddToScheme (scheme )
1193
1195
1194
1196
testCases := []struct {
1195
- name string
1196
- networkSpec infrav1.NetworkSpec
1197
- networkStatus infrav1.NetworkStatus
1198
- expectedAdditionalIngresRule infrav1.IngressRule
1199
- wantErr bool
1197
+ name string
1198
+ networkSpec infrav1.NetworkSpec
1199
+ networkStatus infrav1.NetworkStatus
1200
+ expectedAdditionalIngressRule infrav1.IngressRule
1201
+ wantErr bool
1200
1202
}{
1201
1203
{
1202
1204
name : "default control plane security group is used" ,
@@ -1220,7 +1222,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1220
1222
},
1221
1223
},
1222
1224
},
1223
- expectedAdditionalIngresRule : infrav1.IngressRule {
1225
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1224
1226
Description : "test" ,
1225
1227
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1226
1228
FromPort : 9345 ,
@@ -1251,7 +1253,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1251
1253
},
1252
1254
},
1253
1255
},
1254
- expectedAdditionalIngresRule : infrav1.IngressRule {
1256
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1255
1257
Description : "test" ,
1256
1258
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1257
1259
FromPort : 9345 ,
@@ -1282,7 +1284,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1282
1284
},
1283
1285
},
1284
1286
},
1285
- expectedAdditionalIngresRule : infrav1.IngressRule {
1287
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1286
1288
Description : "test" ,
1287
1289
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1288
1290
FromPort : 9345 ,
@@ -1314,7 +1316,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1314
1316
},
1315
1317
},
1316
1318
},
1317
- expectedAdditionalIngresRule : infrav1.IngressRule {
1319
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1318
1320
Description : "test" ,
1319
1321
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1320
1322
FromPort : 9345 ,
@@ -1345,7 +1347,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1345
1347
},
1346
1348
},
1347
1349
},
1348
- expectedAdditionalIngresRule : infrav1.IngressRule {
1350
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1349
1351
Description : "test" ,
1350
1352
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1351
1353
FromPort : 9345 ,
@@ -1376,7 +1378,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1376
1378
},
1377
1379
NatGatewaysIPs : []string {"test-ip" },
1378
1380
},
1379
- expectedAdditionalIngresRule : infrav1.IngressRule {
1381
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1380
1382
Description : "test" ,
1381
1383
Protocol : infrav1 .SecurityGroupProtocolTCP ,
1382
1384
CidrBlocks : []string {"test-ip/32" },
@@ -1437,20 +1439,125 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
1437
1439
}
1438
1440
found = true
1439
1441
1440
- if r .Protocol != tc .expectedAdditionalIngresRule .Protocol {
1441
- t .Fatalf ("Expected protocol %s, got %s" , tc .expectedAdditionalIngresRule .Protocol , r .Protocol )
1442
+ if r .Protocol != tc .expectedAdditionalIngressRule .Protocol {
1443
+ t .Fatalf ("Expected protocol %s, got %s" , tc .expectedAdditionalIngressRule .Protocol , r .Protocol )
1442
1444
}
1443
1445
1444
- if r .FromPort != tc .expectedAdditionalIngresRule .FromPort {
1445
- t .Fatalf ("Expected from port %d, got %d" , tc .expectedAdditionalIngresRule .FromPort , r .FromPort )
1446
+ if r .FromPort != tc .expectedAdditionalIngressRule .FromPort {
1447
+ t .Fatalf ("Expected from port %d, got %d" , tc .expectedAdditionalIngressRule .FromPort , r .FromPort )
1446
1448
}
1447
1449
1448
- if r .ToPort != tc .expectedAdditionalIngresRule .ToPort {
1449
- t .Fatalf ("Expected to port %d, got %d" , tc .expectedAdditionalIngresRule .ToPort , r .ToPort )
1450
+ if r .ToPort != tc .expectedAdditionalIngressRule .ToPort {
1451
+ t .Fatalf ("Expected to port %d, got %d" , tc .expectedAdditionalIngressRule .ToPort , r .ToPort )
1450
1452
}
1451
1453
1452
- if ! sets .New (tc .expectedAdditionalIngresRule .SourceSecurityGroupIDs ... ).Equal (sets .New (r .SourceSecurityGroupIDs ... )) {
1453
- t .Fatalf ("Expected source security group IDs %v, got %v" , tc .expectedAdditionalIngresRule .SourceSecurityGroupIDs , r .SourceSecurityGroupIDs )
1454
+ if ! sets .New [string ](tc .expectedAdditionalIngressRule .SourceSecurityGroupIDs ... ).Equal (sets .New [string ](tc .expectedAdditionalIngressRule .SourceSecurityGroupIDs ... )) {
1455
+ t .Fatalf ("Expected source security group IDs %v, got %v" , tc .expectedAdditionalIngressRule .SourceSecurityGroupIDs , r .SourceSecurityGroupIDs )
1456
+ }
1457
+ }
1458
+
1459
+ if ! found {
1460
+ t .Fatal ("Additional ingress rule was not found" )
1461
+ }
1462
+ })
1463
+ }
1464
+ }
1465
+
1466
+ func TestAdditionalManagedControlPlaneSecurityGroup (t * testing.T ) {
1467
+ scheme := runtime .NewScheme ()
1468
+ _ = ekscontrolplanev1 .AddToScheme (scheme )
1469
+
1470
+ testCases := []struct {
1471
+ name string
1472
+ networkSpec infrav1.NetworkSpec
1473
+ expectedAdditionalIngressRule infrav1.IngressRule
1474
+ }{
1475
+ {
1476
+ name : "default control plane security group is used" ,
1477
+ networkSpec : infrav1.NetworkSpec {
1478
+ AdditionalControlPlaneIngressRules : []infrav1.IngressRule {
1479
+ {
1480
+ Description : "test" ,
1481
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1482
+ FromPort : 9345 ,
1483
+ ToPort : 9345 ,
1484
+ },
1485
+ },
1486
+ },
1487
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1488
+ Description : "test" ,
1489
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1490
+ FromPort : 9345 ,
1491
+ ToPort : 9345 ,
1492
+ SourceSecurityGroupIDs : []string {"cp-sg-id" },
1493
+ },
1494
+ },
1495
+ {
1496
+ name : "don't set source security groups if cidr blocks are set" ,
1497
+ networkSpec : infrav1.NetworkSpec {
1498
+ AdditionalControlPlaneIngressRules : []infrav1.IngressRule {
1499
+ {
1500
+ Description : "test" ,
1501
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1502
+ FromPort : 9345 ,
1503
+ ToPort : 9345 ,
1504
+ CidrBlocks : []string {"test-cidr-block" },
1505
+ },
1506
+ },
1507
+ },
1508
+ expectedAdditionalIngressRule : infrav1.IngressRule {
1509
+ Description : "test" ,
1510
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1511
+ FromPort : 9345 ,
1512
+ ToPort : 9345 ,
1513
+ CidrBlocks : []string {"test-cidr-block" },
1514
+ },
1515
+ },
1516
+ }
1517
+
1518
+ for _ , tc := range testCases {
1519
+ t .Run (tc .name , func (t * testing.T ) {
1520
+ cs , err := scope .NewManagedControlPlaneScope (scope.ManagedControlPlaneScopeParams {
1521
+ Client : fake .NewClientBuilder ().WithScheme (scheme ).Build (),
1522
+ Cluster : & clusterv1.Cluster {
1523
+ ObjectMeta : metav1.ObjectMeta {Name : "test-cluster" },
1524
+ },
1525
+ ControlPlane : & ekscontrolplanev1.AWSManagedControlPlane {
1526
+ Spec : ekscontrolplanev1.AWSManagedControlPlaneSpec {
1527
+ NetworkSpec : tc .networkSpec ,
1528
+ },
1529
+ Status : ekscontrolplanev1.AWSManagedControlPlaneStatus {
1530
+ Network : infrav1.NetworkStatus {
1531
+ SecurityGroups : map [infrav1.SecurityGroupRole ]infrav1.SecurityGroup {
1532
+ infrav1 .SecurityGroupControlPlane : {
1533
+ ID : "cp-sg-id" ,
1534
+ },
1535
+ infrav1 .SecurityGroupNode : {
1536
+ ID : "node-sg-id" ,
1537
+ },
1538
+ },
1539
+ },
1540
+ },
1541
+ },
1542
+ })
1543
+ if err != nil {
1544
+ t .Fatalf ("Failed to create test context: %v" , err )
1545
+ }
1546
+
1547
+ s := NewService (cs , testSecurityGroupRoles )
1548
+ rules , err := s .getSecurityGroupIngressRules (infrav1 .SecurityGroupControlPlane )
1549
+ if err != nil {
1550
+ t .Fatalf ("Failed to lookup controlplane security group ingress rules: %v" , err )
1551
+ }
1552
+
1553
+ found := false
1554
+ for _ , r := range rules {
1555
+ if r .Description == "test" {
1556
+ found = true
1557
+
1558
+ if ! reflect .DeepEqual (r , tc .expectedAdditionalIngressRule ) {
1559
+ t .Fatalf ("Expected ingress rule %#v, got %#v" , tc .expectedAdditionalIngressRule , r )
1560
+ }
1454
1561
}
1455
1562
}
1456
1563
0 commit comments