Skip to content

Conversation

@vakalapa
Copy link
Contributor

@vakalapa vakalapa commented Nov 12, 2021

Reason for Change:

  1. Splitting SetPolicy calls into HNS into 2, one for Nested and one for 1st level sets.
  2. Adding state to FakeHNS to help emulate HNS behavior
  3. Adding IPSet Manager Windows UTs
  4. Adding Policy Manager Windows UTs.

Issue Fixed:

Requirements:

Notes:

@huntergregory huntergregory added the npm Related to NPM. label Nov 18, 2021
assert.Equal(t, util.GetHashedName(setMetadata.GetPrefixName()), set.HashedName)

err := iMgr.applyIPSets()
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually change each test to have this:

toAddOrUpdateSetNames := []string{}
assertEqualContentsTestHelper(t, toAddOrUpdateSetNames, iMgr.toAddOrUpdateCache)
toDeleteSetNames := []string{}
assertEqualContentsTestHelper(t, toAddOrUpdateSetNames, iMgr.toAddOrUpdateCache)
calls := GetApplyIPSetTestCalls(toAdd, toDelete)
ioshim := common.NewMockIOShim(calls)
defer ioshim.VerifyCalls(t, calls)
iMgr := NewIPSetManager(ioshim)
...
iMgr.ApplyIPSets()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for another PR

return nil
}

type PolicyManagerCfg struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to add this in Constructor for pMgr in another PR (will touch code in dataplane.go, dataplane_test.go though)

}

func printAndWait() {
func printAndWait(wait bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if wait is false, function name could be simplified

}
}

func NewMockIOShimWithFakeHNS(hns *hnswrapper.Hnsv2wrapperFake) *IOShim {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably omit these fakes from coverage metrics

Copy link
Contributor

@JungukCho JungukCho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can resolve it in your next PR if they make sense.


const networkName = "azure"

var errorFakeHNS = errors.New("errorFakeHNS Error")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: seems Error is unnecessary for newErrorFakeHNS since errorFakeHNS has error context already, but it is ok as is.


type Hnsv2wrapperFake struct {
Cache FakeHNSCache
*sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious: special reason for pointer for sync.Mutex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint was not happy that i was passing a value and not reference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.. good to know why.

Copy link
Collaborator

@rbtr rbtr Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at the usage, but probably because you should never copy a sync.Mutex. In a fake this might be okay, but I would really discourage references to mutexes because any copy (like if you pass the struct by val in to parallel goroutines) of the object can lock all of them

if err != nil {
return newErrorFakeHNS(err.Error())
}
for _, setPolSetting := range setPolSettings.Policies {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: seem better HNSNetPol instead of setPolSetting

if err != nil {
return newErrorFakeHNS(err.Error())
}
for _, newPolicy := range setPolSettings.Policies {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be better to use consistent variables for newPolicy if there is no special contexts. Line 69 in this file uses a different name.

return newErrorFakeHNS(err.Error())
}
if _, ok := networkCache.Policies[setpol.Id]; !ok {
return newErrorFakeHNS(fmt.Sprintf("[FakeHNS] could not find %s ipset", setpol.Name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: not sure sometime it has [FakeHNS] or not.
It looks overlapped with

var errorFakeHNS = errors.New("errorFakeHNS Error")

func (fCache FakeHNSCache) ACLPolicies(epList map[string]string, policyID string) (map[string][]*FakeEndpointPolicy, error) {
aclPols := make(map[string][]*FakeEndpointPolicy)
for ip, epID := range epList {
epCache, ok := fCache.endpoints[epID]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: epCache -> cachedEP or just ep since I checked whether fCache.endpoints[epID] return another map ?

func (fEndpoint *FakeHostComputeEndpoint) RemovePolicy(toRemovePol *FakeEndpointPolicy) error {
for i, policy := range fEndpoint.Policies {
if reflect.DeepEqual(policy, toRemovePol) {
fEndpoint.Policies = append(fEndpoint.Policies[:i], fEndpoint.Policies[i+1:]...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: may do swap if ordering is not issue.
For example,

if i != len(fEndpoint.Policies) {
// replace it with last one
fEndpoint.Policies[i] = fEndpoint.Policies[len(fEndpoint.Policies)-1]
}
// remove last element.
fEndpoint.Policies = fEndpoint.Policies[:i-1]
return nil

}

)

const (
blockRulePriotity = 3000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be beneficial to put comments why they are 3000 and 222 for code maintenance.

policySettings.LocalAddresses = srcListStr
policySettings.RemoteAddresses = dstListStr
policySettings.RemotePorts = dstPortStr
policySettings.LocalPorts = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks unnecessary. policySettings has default value (empty string) for string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a preventive measure, can remove it.

if policySettings.Direction == hcn.DirectionTypeOut {
policySettings.LocalAddresses = dstListStr
policySettings.LocalPorts = dstPortStr
policySettings.RemotePorts = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks unnecessary. policySettings has default value (empty string) for string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed unlike the previous comment because the we are first setting the policy remote ports for In and then changing to out, so to override the previous assignment we are re-assigning this value to empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will be easier to understand to have if else based on hcn.DirectionTypeOut

if policySettings.Direction == hcn.DirectionTypeOut {
		policySettings.LocalAddresses = dstListStr
		policySettings.RemoteAddresses = srcListStr
                policySettings.LocalPorts = dstPortStr
}else  { // maybe In?
	policySettings.LocalAddresses = srcListStr
	policySettings.RemoteAddresses = dstListStr
	policySettings.RemotePorts = dstPortStr
}

If I missed something, stay with current code, but slightly adjust order in if condition to have the same order above assignment since it helps understand what wat changed.

	policySettings.LocalAddresses = srcListStr
	policySettings.RemoteAddresses = dstListStr
	policySettings.RemotePorts = dstPortStr
	policySettings.LocalPorts = ""
	if policySettings.Direction == hcn.DirectionTypeOut {
		policySettings.LocalAddresses = dstListStr
		policySettings.RemoteAddresses = srcListStr
		policySettings.RemotePorts = ""
                policySettings.LocalPorts = dstPortStr
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But do you see a huge readability issue with the way it is currently ? I feel like by explicitly assigning to "" it is clear what to expect from a ACL policy in a given direction and removes any assumptions ? wdyt ?

@vakalapa vakalapa merged commit ca3457e into master Nov 19, 2021
@vakalapa vakalapa deleted the vakr/winpoliciesreset branch November 19, 2021 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

npm Related to NPM.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants