Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust the NSG validation logic for GA #3356

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions pkg/validate/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,10 @@ func (dv *dynamic) createSubnetMapByID(ctx context.Context, subnets []Subnet) (m
return subnetByID, nil
}

// checkPreconfiguredNSG checks whether all the subnets have or don't have NSG attached.
// when the PreconfigureNSG feature flag is on and only some of the subnets are attached with an NSG,
// it returns an error. If none of the subnets is attached, the feature is no longer active and the
// cluster installation process should fall back to using the managed nsg.
func (dv *dynamic) checkPreconfiguredNSG(subnetByID map[string]*mgmtnetwork.Subnet) (api.PreconfiguredNSG, error) {
// checkPreconfiguredNSG checks whether all the subnets have an NSG attached.
// when the PreconfigureNSG feature flag is on and not all subnets are attached,
// it returns an error.
func (dv *dynamic) checkPreconfiguredNSG(subnetByID map[string]*mgmtnetwork.Subnet) error {
var attached int
for _, subnet := range subnetByID {
if subnetHasNSGAttached(subnet) {
Expand All @@ -674,24 +673,16 @@ func (dv *dynamic) checkPreconfiguredNSG(subnetByID map[string]*mgmtnetwork.Subn
// all subnets have an attached NSG
if attached == len(subnetByID) {
dv.log.Info("all subnets are attached, BYO NSG")
return api.PreconfiguredNSGEnabled, nil // correct setup by customer
return nil // correct setup by customer
}

// no subnets have attached NSG, fallback
if attached == 0 {
dv.log.Info("no subnets are attached, no longer BYO NSG. Fall back to using cluster NSG.")
return api.PreconfiguredNSGDisabled, nil
return &api.CloudError{
StatusCode: http.StatusBadRequest,
CloudErrorBody: &api.CloudErrorBody{
Code: api.CloudErrorCodeInvalidLinkedVNet,
Message: errMsgNSGNotProperlyAttached,
},
}

// some subnets have NSGs attached, error out
return api.PreconfiguredNSGDisabled,
&api.CloudError{
StatusCode: http.StatusBadRequest,
CloudErrorBody: &api.CloudErrorBody{
Code: api.CloudErrorCodeInvalidLinkedVNet,
Message: errMsgNSGNotProperlyAttached,
},
}
}

func (dv *dynamic) ValidateSubnets(ctx context.Context, oc *api.OpenShiftCluster, subnets []Subnet) error {
Expand All @@ -703,7 +694,8 @@ func (dv *dynamic) ValidateSubnets(ctx context.Context, oc *api.OpenShiftCluster

if oc.Properties.ProvisioningState == api.ProvisioningStateCreating {
if oc.Properties.NetworkProfile.PreconfiguredNSG == api.PreconfiguredNSGEnabled {
oc.Properties.NetworkProfile.PreconfiguredNSG, err = dv.checkPreconfiguredNSG(subnetByID)
dv.log.Info("cluster creation with preconfigured-nsg")
err = dv.checkPreconfiguredNSG(subnetByID)
if err != nil {
return err
}
Expand Down
24 changes: 9 additions & 15 deletions pkg/validate/dynamic/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ func TestValidateNatGatewaysPermissionsWithCheckAccess(t *testing.T) {
}
}

func TestCheckBYONsg(t *testing.T) {
func TestCheckPreconfiguredNSG(t *testing.T) {
subnetWithNSG := &mgmtnetwork.Subnet{
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
Expand All @@ -1686,26 +1686,24 @@ func TestCheckBYONsg(t *testing.T) {
}

for _, tt := range []struct {
name string
subnetByID map[string]*mgmtnetwork.Subnet
preconfiguredNSG api.PreconfiguredNSG
wantErr string
name string
subnetByID map[string]*mgmtnetwork.Subnet
wantErr string
}{
{
name: "pass: all subnets are attached (BYONSG)",
name: "pass: all subnets are attached",
subnetByID: map[string]*mgmtnetwork.Subnet{
"A": subnetWithNSG,
"B": subnetWithNSG,
},
preconfiguredNSG: api.PreconfiguredNSGEnabled,
},
{
name: "pass: no subnets are attached (no longer BYONSG)",
name: "fail: no subnets are attached",
subnetByID: map[string]*mgmtnetwork.Subnet{
"A": subnetWithoutNSG,
"B": subnetWithoutNSG,
},
preconfiguredNSG: api.PreconfiguredNSGDisabled,
wantErr: "400: InvalidLinkedVNet: : When the enable-preconfigured-nsg option is specified, both the master and worker subnets should have network security groups (NSG) attached to them before starting the cluster installation.",
},
{
name: "fail: parts of the subnets are attached",
Expand All @@ -1714,19 +1712,15 @@ func TestCheckBYONsg(t *testing.T) {
"B": subnetWithoutNSG,
"C": subnetWithNSG,
},
preconfiguredNSG: api.PreconfiguredNSGDisabled,
wantErr: "400: InvalidLinkedVNet: : When the enable-preconfigured-nsg option is specified, both the master and worker subnets should have network security groups (NSG) attached to them before starting the cluster installation.",
wantErr: "400: InvalidLinkedVNet: : When the enable-preconfigured-nsg option is specified, both the master and worker subnets should have network security groups (NSG) attached to them before starting the cluster installation.",
},
} {
t.Run(tt.name, func(t *testing.T) {
dv := &dynamic{
log: logrus.NewEntry(logrus.StandardLogger()),
}
preconfiguredNSG, err := dv.checkPreconfiguredNSG(tt.subnetByID)
err := dv.checkPreconfiguredNSG(tt.subnetByID)
utilerror.AssertErrorMessage(t, err, tt.wantErr)
if preconfiguredNSG != tt.preconfiguredNSG {
t.Errorf("preconfiguredNSG got %s, want %s", preconfiguredNSG, tt.preconfiguredNSG)
}
})
}
}
Expand Down
Loading