Skip to content

Commit

Permalink
Refactor to address the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nwnt committed Jan 18, 2024
1 parent 016ba7b commit 6b122e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
21 changes: 9 additions & 12 deletions pkg/monitor/azure/nsg/nsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,20 @@ func (n *NSGMonitor) Monitor(ctx context.Context) []error {
workerProfiles, _ := api.GetEnrichedWorkerProfiles(n.oc.Properties)
workerSubnets := make([]subnetNSGConfig, 0, len(workerProfiles))
workerPrefixes := make([]netip.Prefix, 0, len(workerProfiles))
subnetSet := map[string]struct{}{}
subnetToMonitor := map[string]struct{}{}

for _, wp := range workerProfiles {
// Customer can configure a machineset with an invalid subnet.
// In such case, the subnetID will be empty.
//
// We do not need to consider profiles with 0 machines.
if len(wp.SubnetID) == 0 || wp.Count == 0 {
continue
}

// Many profiles can have the same subnet ID. To minimize the possibility of throttling, we only get it once.
if _, ok := subnetSet[wp.SubnetID]; ok {
continue
// We do not need to monitor any profiles with 0 machines either.
// To minimize the possibility of throttling, we only get it once.
if len(wp.SubnetID) != 0 && wp.Count != 0 {
subnetToMonitor[wp.SubnetID] = struct{}{}
}
subnetSet[wp.SubnetID] = struct{}{}
}

s, err := n.toSubnetConfig(ctx, wp.SubnetID)
for subnetID := range subnetToMonitor {
s, err := n.toSubnetConfig(ctx, subnetID)
if err != nil {
// FP has no access to the subnet
return []error{err}
Expand Down
8 changes: 3 additions & 5 deletions pkg/monitor/azure/nsg/nsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,19 +475,17 @@ func TestMonitor(t *testing.T) {
workerSubnet1.Properties.NetworkSecurityGroup = &nsg2
workerSubnet2.Properties.NetworkSecurityGroup = &nsg2

_1 := mock.EXPECT().
mock.EXPECT().
Get(ctx, resourcegroupName, vNetName, masterSubnetName, options).
Return(masterSubnet, nil)

_2 := mock.EXPECT().
mock.EXPECT().
Get(ctx, resourcegroupName, vNetName, workerSubnet1Name, options).
Return(workerSubnet1, nil)

_3 := mock.EXPECT().
mock.EXPECT().
Get(ctx, resourcegroupName, vNetName, workerSubnet2Name, options).
Return(workerSubnet2, nil)

gomock.InOrder(_1, _2, _3)
},
mockEmitter: func(mock *mock_metrics.MockEmitter) {
mock.EXPECT().EmitGauge(MetricInvalidDenyRule, int64(1), map[string]string{
Expand Down

0 comments on commit 6b122e5

Please sign in to comment.