Skip to content

Commit 1f41e28

Browse files
committed
OCM-6929 | fix: Improved UX for error handling in private clusters
1 parent 3d808c2 commit 1f41e28

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

cmd/create/cluster/cmd.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net"
2323
"os"
2424
"reflect"
25+
"slices"
2526
"strconv"
2627
"strings"
2728
"time"
@@ -2209,9 +2210,12 @@ func run(cmd *cobra.Command, _ []string) {
22092210
r.Reporter.Errorf("%s", filterError)
22102211
os.Exit(1)
22112212
}
2213+
2214+
var excludedPublicSubnets []string
22122215
if privateLink {
2213-
subnets = filterPrivateSubnets(subnets, r)
2216+
subnets, excludedPublicSubnets = filterPrivateSubnets(subnets, r)
22142217
}
2218+
22152219
if len(subnets) == 0 {
22162220
r.Reporter.Warnf("No subnets found in current region that are valid for the chosen CIDR ranges")
22172221
if isHostedCP {
@@ -2234,13 +2238,17 @@ func run(cmd *cobra.Command, _ []string) {
22342238
// Verify subnets provided exist.
22352239
if subnetsProvided {
22362240
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)
22422246
}
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+
}) {
22442252
r.Reporter.Errorf("Could not find the following subnet provided in region '%s': %s",
22452253
r.AWSClient.GetRegion(), subnetArg)
22462254
os.Exit(1)
@@ -3700,8 +3708,8 @@ func handleOidcConfigOptions(r *rosa.Runtime, cmd *cobra.Command, isSTS bool, is
37003708
return oidcConfig
37013709
}
37023710

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{}
37053713
filteredSubnets := []ec2types.Subnet{}
37063714
publicSubnetMap, err := r.AWSClient.FetchPublicSubnetMap(initialSubnets)
37073715
if err != nil {
@@ -3712,8 +3720,8 @@ func filterPrivateSubnets(initialSubnets []ec2types.Subnet, r *rosa.Runtime) []e
37123720
skip := false
37133721
if isPublic, ok := publicSubnetMap[awssdk.ToString(subnet.SubnetId)]; ok {
37143722
if isPublic {
3715-
excludedSubnetsDueToPublic = append(
3716-
excludedSubnetsDueToPublic,
3723+
excludedPublicSubnets = append(
3724+
excludedPublicSubnets,
37173725
awssdk.ToString(subnet.SubnetId),
37183726
)
37193727
skip = true
@@ -3723,12 +3731,12 @@ func filterPrivateSubnets(initialSubnets []ec2types.Subnet, r *rosa.Runtime) []e
37233731
filteredSubnets = append(filteredSubnets, subnet)
37243732
}
37253733
}
3726-
if len(excludedSubnetsDueToPublic) > 0 {
3734+
if len(excludedPublicSubnets) > 0 {
37273735
r.Reporter.Warnf("The following subnets have been excluded"+
37283736
" because they have an Internet Gateway Targetded Route and the Cluster choice is private: %s",
3729-
helper.SliceToSortedString(excludedSubnetsDueToPublic))
3737+
helper.SliceToSortedString(excludedPublicSubnets))
37303738
}
3731-
return filteredSubnets
3739+
return filteredSubnets, excludedPublicSubnets
37323740
}
37333741

37343742
// filterCidrRangeSubnets filters the initial set of subnets to those that are part of the machine network,

0 commit comments

Comments
 (0)