@@ -34,6 +34,7 @@ import (
3434 "sigs.k8s.io/controller-runtime/pkg/client/fake"
3535
3636 infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
37+ ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
3738 "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors"
3839 "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/filter"
3940 "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
@@ -1079,6 +1080,122 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
10791080 }
10801081}
10811082
1083+ func TestAdditionalManagedControlPlaneSecurityGroup (t * testing.T ) {
1084+ scheme := runtime .NewScheme ()
1085+ _ = ekscontrolplanev1 .AddToScheme (scheme )
1086+
1087+ testCases := []struct {
1088+ name string
1089+ networkSpec infrav1.NetworkSpec
1090+ expectedAdditionalIngresRule infrav1.IngressRule
1091+ }{
1092+ {
1093+ name : "default control plane security group is used" ,
1094+ networkSpec : infrav1.NetworkSpec {
1095+ AdditionalControlPlaneIngressRules : []infrav1.IngressRule {
1096+ {
1097+ Description : "test" ,
1098+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1099+ FromPort : 9345 ,
1100+ ToPort : 9345 ,
1101+ },
1102+ },
1103+ },
1104+ expectedAdditionalIngresRule : infrav1.IngressRule {
1105+ Description : "test" ,
1106+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1107+ FromPort : 9345 ,
1108+ ToPort : 9345 ,
1109+ SourceSecurityGroupIDs : []string {"cp-sg-id" },
1110+ },
1111+ },
1112+ {
1113+ name : "don't set source security groups if cidr blocks are set" ,
1114+ networkSpec : infrav1.NetworkSpec {
1115+ AdditionalControlPlaneIngressRules : []infrav1.IngressRule {
1116+ {
1117+ Description : "test" ,
1118+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1119+ FromPort : 9345 ,
1120+ ToPort : 9345 ,
1121+ CidrBlocks : []string {"test-cidr-block" },
1122+ },
1123+ },
1124+ },
1125+ expectedAdditionalIngresRule : infrav1.IngressRule {
1126+ Description : "test" ,
1127+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
1128+ FromPort : 9345 ,
1129+ ToPort : 9345 ,
1130+ },
1131+ },
1132+ }
1133+
1134+ for _ , tc := range testCases {
1135+ t .Run (tc .name , func (t * testing.T ) {
1136+ cs , err := scope .NewManagedControlPlaneScope (scope.ManagedControlPlaneScopeParams {
1137+ Client : fake .NewClientBuilder ().WithScheme (scheme ).Build (),
1138+ Cluster : & clusterv1.Cluster {
1139+ ObjectMeta : metav1.ObjectMeta {Name : "test-cluster" },
1140+ },
1141+ ControlPlane : & ekscontrolplanev1.AWSManagedControlPlane {
1142+ Spec : ekscontrolplanev1.AWSManagedControlPlaneSpec {
1143+ NetworkSpec : tc .networkSpec ,
1144+ },
1145+ Status : ekscontrolplanev1.AWSManagedControlPlaneStatus {
1146+ Network : infrav1.NetworkStatus {
1147+ SecurityGroups : map [infrav1.SecurityGroupRole ]infrav1.SecurityGroup {
1148+ infrav1 .SecurityGroupControlPlane : {
1149+ ID : "cp-sg-id" ,
1150+ },
1151+ infrav1 .SecurityGroupNode : {
1152+ ID : "node-sg-id" ,
1153+ },
1154+ },
1155+ },
1156+ },
1157+ },
1158+ })
1159+ if err != nil {
1160+ t .Fatalf ("Failed to create test context: %v" , err )
1161+ }
1162+
1163+ s := NewService (cs , testSecurityGroupRoles )
1164+ rules , err := s .getSecurityGroupIngressRules (infrav1 .SecurityGroupControlPlane )
1165+ if err != nil {
1166+ t .Fatalf ("Failed to lookup controlplane security group ingress rules: %v" , err )
1167+ }
1168+
1169+ found := false
1170+ for _ , r := range rules {
1171+ if r .Description == "test" {
1172+ found = true
1173+
1174+ if r .Protocol != tc .expectedAdditionalIngresRule .Protocol {
1175+ t .Fatalf ("Expected protocol %s, got %s" , tc .expectedAdditionalIngresRule .Protocol , r .Protocol )
1176+ }
1177+
1178+ if r .FromPort != tc .expectedAdditionalIngresRule .FromPort {
1179+ t .Fatalf ("Expected from port %d, got %d" , tc .expectedAdditionalIngresRule .FromPort , r .FromPort )
1180+ }
1181+
1182+ if r .ToPort != tc .expectedAdditionalIngresRule .ToPort {
1183+ t .Fatalf ("Expected to port %d, got %d" , tc .expectedAdditionalIngresRule .ToPort , r .ToPort )
1184+ }
1185+
1186+ if ! sets .New [string ](tc .expectedAdditionalIngresRule .SourceSecurityGroupIDs ... ).Equal (sets .New [string ](tc .expectedAdditionalIngresRule .SourceSecurityGroupIDs ... )) {
1187+ t .Fatalf ("Expected source security group IDs %v, got %v" , tc .expectedAdditionalIngresRule .SourceSecurityGroupIDs , r .SourceSecurityGroupIDs )
1188+ }
1189+ }
1190+ }
1191+
1192+ if ! found {
1193+ t .Fatal ("Additional ingress rule was not found" )
1194+ }
1195+ })
1196+ }
1197+ }
1198+
10821199func TestControlPlaneLoadBalancerIngressRules (t * testing.T ) {
10831200 scheme := runtime .NewScheme ()
10841201 _ = infrav1 .AddToScheme (scheme )
0 commit comments