@@ -22,6 +22,7 @@ import (
22
22
"net"
23
23
"os"
24
24
"reflect"
25
+ "slices"
25
26
"strconv"
26
27
"strings"
27
28
"time"
@@ -2209,9 +2210,12 @@ func run(cmd *cobra.Command, _ []string) {
2209
2210
r .Reporter .Errorf ("%s" , filterError )
2210
2211
os .Exit (1 )
2211
2212
}
2213
+
2214
+ var excludedPublicSubnets []string
2212
2215
if privateLink {
2213
- subnets = filterPrivateSubnets (subnets , r )
2216
+ subnets , excludedPublicSubnets = filterPrivateSubnets (subnets , r )
2214
2217
}
2218
+
2215
2219
if len (subnets ) == 0 {
2216
2220
r .Reporter .Warnf ("No subnets found in current region that are valid for the chosen CIDR ranges" )
2217
2221
if isHostedCP {
@@ -2234,13 +2238,17 @@ func run(cmd *cobra.Command, _ []string) {
2234
2238
// Verify subnets provided exist.
2235
2239
if subnetsProvided {
2236
2240
for _ , subnetArg := range subnetIDs {
2237
- verifiedSubnet := false
2238
- for _ , subnet := range subnets {
2239
- if awssdk . ToString ( subnet . SubnetId ) == subnetArg {
2240
- verifiedSubnet = true
2241
- }
2241
+ // Check if subnet is in the excluded list of public subnets
2242
+ if slices . Contains ( excludedPublicSubnets , subnetArg ) {
2243
+ r . Reporter . Errorf ( "Cluster is set as private, cannot use public '%s'" ,
2244
+ subnetArg )
2245
+ os . Exit ( 1 )
2242
2246
}
2243
- if ! verifiedSubnet {
2247
+
2248
+ // Check if the provided subnet exists in the filtered list
2249
+ if ! slices .ContainsFunc (subnets , func (subnet ec2types.Subnet ) bool {
2250
+ return awssdk .ToString (subnet .SubnetId ) == subnetArg
2251
+ }) {
2244
2252
r .Reporter .Errorf ("Could not find the following subnet provided in region '%s': %s" ,
2245
2253
r .AWSClient .GetRegion (), subnetArg )
2246
2254
os .Exit (1 )
@@ -3700,8 +3708,8 @@ func handleOidcConfigOptions(r *rosa.Runtime, cmd *cobra.Command, isSTS bool, is
3700
3708
return oidcConfig
3701
3709
}
3702
3710
3703
- func filterPrivateSubnets (initialSubnets []ec2types.Subnet , r * rosa.Runtime ) []ec2types.Subnet {
3704
- excludedSubnetsDueToPublic := []string {}
3711
+ func filterPrivateSubnets (initialSubnets []ec2types.Subnet , r * rosa.Runtime ) ( []ec2types.Subnet , [] string ) {
3712
+ excludedPublicSubnets := []string {}
3705
3713
filteredSubnets := []ec2types.Subnet {}
3706
3714
publicSubnetMap , err := r .AWSClient .FetchPublicSubnetMap (initialSubnets )
3707
3715
if err != nil {
@@ -3712,8 +3720,8 @@ func filterPrivateSubnets(initialSubnets []ec2types.Subnet, r *rosa.Runtime) []e
3712
3720
skip := false
3713
3721
if isPublic , ok := publicSubnetMap [awssdk .ToString (subnet .SubnetId )]; ok {
3714
3722
if isPublic {
3715
- excludedSubnetsDueToPublic = append (
3716
- excludedSubnetsDueToPublic ,
3723
+ excludedPublicSubnets = append (
3724
+ excludedPublicSubnets ,
3717
3725
awssdk .ToString (subnet .SubnetId ),
3718
3726
)
3719
3727
skip = true
@@ -3723,12 +3731,12 @@ func filterPrivateSubnets(initialSubnets []ec2types.Subnet, r *rosa.Runtime) []e
3723
3731
filteredSubnets = append (filteredSubnets , subnet )
3724
3732
}
3725
3733
}
3726
- if len (excludedSubnetsDueToPublic ) > 0 {
3734
+ if len (excludedPublicSubnets ) > 0 {
3727
3735
r .Reporter .Warnf ("The following subnets have been excluded" +
3728
3736
" because they have an Internet Gateway Targetded Route and the Cluster choice is private: %s" ,
3729
- helper .SliceToSortedString (excludedSubnetsDueToPublic ))
3737
+ helper .SliceToSortedString (excludedPublicSubnets ))
3730
3738
}
3731
- return filteredSubnets
3739
+ return filteredSubnets , excludedPublicSubnets
3732
3740
}
3733
3741
3734
3742
// filterCidrRangeSubnets filters the initial set of subnets to those that are part of the machine network,
0 commit comments