-
Notifications
You must be signed in to change notification settings - Fork 369
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
Add methods in pkg/agent/route for NodeNetworkPolicy #5692
Closed
hongliangl
wants to merge
1
commit into
antrea-io:main
from
hongliangl:20231110_interfaces_for_nnp_in_pkg_agent_route
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"github.com/stretchr/testify/assert" | ||
"github.com/vishvananda/netlink" | ||
"go.uber.org/mock/gomock" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
|
||
"antrea.io/antrea/pkg/agent/config" | ||
servicecidrtest "antrea.io/antrea/pkg/agent/servicecidr/testing" | ||
|
@@ -1721,3 +1722,192 @@ func TestAddAndDeleteNodeIP(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestAddAndDeleteNodeNetworkPolicyIPSet(t *testing.T) { | ||
ipv4SetName := "TEST-IPSET-4" | ||
ipv4Net1 := "1.1.1.1/32" | ||
ipv4Net2 := "2.2.2.2/32" | ||
ipv4Net3 := "3.3.3.3/32" | ||
ipv6SetName := "TEST-IPSET-6" | ||
ipv6Net1 := "fec0::1111/128" | ||
ipv6Net2 := "fec0::2222/128" | ||
ipv6Net3 := "fec0::3333/128" | ||
|
||
tests := []struct { | ||
name string | ||
ipsetName string | ||
prevIPSetEntries sets.Set[string] | ||
curIPSetEntries sets.Set[string] | ||
isIPv6 bool | ||
expectedCalls func(mockIPSet *ipsettest.MockInterfaceMockRecorder) | ||
}{ | ||
{ | ||
name: "IPv4, add an ipset and delete it", | ||
ipsetName: ipv4SetName, | ||
curIPSetEntries: sets.New[string](ipv4Net1, ipv4Net3), | ||
isIPv6: false, | ||
expectedCalls: func(mockIPSet *ipsettest.MockInterfaceMockRecorder) { | ||
mockIPSet.CreateIPSet(ipv4SetName, ipset.HashNet, false).Times(1) | ||
mockIPSet.AddEntry(ipv4SetName, ipv4Net1).Times(1) | ||
mockIPSet.AddEntry(ipv4SetName, ipv4Net3).Times(1) | ||
mockIPSet.DestroyIPSet(ipv4SetName).Times(1) | ||
}, | ||
}, | ||
{ | ||
name: "IPv4, update an ipset and delete it", | ||
ipsetName: ipv4SetName, | ||
prevIPSetEntries: sets.New[string](ipv4Net1, ipv4Net2), | ||
curIPSetEntries: sets.New[string](ipv4Net1, ipv4Net3), | ||
isIPv6: false, | ||
expectedCalls: func(mockIPSet *ipsettest.MockInterfaceMockRecorder) { | ||
mockIPSet.CreateIPSet(ipv4SetName, ipset.HashNet, false).Times(1) | ||
mockIPSet.AddEntry(ipv4SetName, ipv4Net3).Times(1) | ||
mockIPSet.DelEntry(ipv4SetName, ipv4Net2).Times(1) | ||
mockIPSet.DestroyIPSet(ipv4SetName).Times(1) | ||
}, | ||
}, | ||
{ | ||
name: "IPv6, add an ipset and delete it", | ||
ipsetName: ipv6SetName, | ||
curIPSetEntries: sets.New[string](ipv6Net1, ipv6Net3), | ||
isIPv6: true, | ||
expectedCalls: func(mockIPSet *ipsettest.MockInterfaceMockRecorder) { | ||
mockIPSet.CreateIPSet(ipv6SetName, ipset.HashNet, true).Times(1) | ||
mockIPSet.AddEntry(ipv6SetName, ipv6Net1).Times(1) | ||
mockIPSet.AddEntry(ipv6SetName, ipv6Net3).Times(1) | ||
mockIPSet.DestroyIPSet(ipv6SetName).Times(1) | ||
}, | ||
}, | ||
{ | ||
name: "IPv6, update an ipset and delete it", | ||
ipsetName: ipv6SetName, | ||
prevIPSetEntries: sets.New[string](ipv6Net1, ipv6Net2), | ||
curIPSetEntries: sets.New[string](ipv6Net1, ipv6Net3), | ||
isIPv6: true, | ||
expectedCalls: func(mockIPSet *ipsettest.MockInterfaceMockRecorder) { | ||
mockIPSet.CreateIPSet(ipv6SetName, ipset.HashNet, true).Times(1) | ||
mockIPSet.AddEntry(ipv6SetName, ipv6Net3).Times(1) | ||
mockIPSet.DelEntry(ipv6SetName, ipv6Net2).Times(1) | ||
mockIPSet.DestroyIPSet(ipv6SetName).Times(1) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockIPSet := ipsettest.NewMockInterface(ctrl) | ||
c := &Client{ipset: mockIPSet} | ||
tt.expectedCalls(mockIPSet.EXPECT()) | ||
|
||
assert.NoError(t, c.AddOrUpdateNodeNetworkPolicyIPSet(tt.ipsetName, tt.prevIPSetEntries, tt.curIPSetEntries, tt.isIPv6)) | ||
var exists bool | ||
if tt.isIPv6 { | ||
_, exists = c.nodeNetworkPolicyIPSetsIPv6.Load(tt.ipsetName) | ||
} else { | ||
_, exists = c.nodeNetworkPolicyIPSetsIPv4.Load(tt.ipsetName) | ||
} | ||
Comment on lines
+1804
to
+1808
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an internal function to reduce the code duplication. |
||
assert.True(t, exists) | ||
|
||
assert.NoError(t, c.DeleteNodeNetworkPolicyIPSet(tt.ipsetName, tt.isIPv6)) | ||
if tt.isIPv6 { | ||
_, exists = c.nodeNetworkPolicyIPSetsIPv6.Load(tt.ipsetName) | ||
} else { | ||
_, exists = c.nodeNetworkPolicyIPSetsIPv4.Load(tt.ipsetName) | ||
} | ||
assert.False(t, exists) | ||
}) | ||
} | ||
} | ||
|
||
func TestAddAndDeleteNodeNetworkPolicyIPTables(t *testing.T) { | ||
chain1 := "TEST-CHAIN1" | ||
chain2 := "TEST-CHAIN2" | ||
chains := []string{chain1, chain2} | ||
rules := [][]string{ | ||
{ | ||
"-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 80", | ||
"-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 443", | ||
}, | ||
{ | ||
"-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 80", | ||
"-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 443", | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
iptablesChains []string | ||
iptablesRules [][]string | ||
isIPv6 bool | ||
expectedCalls func(mockIPTables *iptablestest.MockInterfaceMockRecorder) | ||
}{ | ||
{ | ||
name: "IPv4", | ||
iptablesChains: chains, | ||
iptablesRules: rules, | ||
isIPv6: false, | ||
expectedCalls: func(mockIPTables *iptablestest.MockInterfaceMockRecorder) { | ||
mockIPTables.Restore(`*filter | ||
:TEST-CHAIN1 - [0:0] | ||
:TEST-CHAIN2 - [0:0] | ||
-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 80 | ||
-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 443 | ||
-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 80 | ||
-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 443 | ||
COMMIT | ||
`, false, false) | ||
mockIPTables.DeleteChain(iptables.ProtocolIPv4, iptables.FilterTable, chain1) | ||
mockIPTables.DeleteChain(iptables.ProtocolIPv4, iptables.FilterTable, chain2) | ||
}, | ||
}, | ||
{ | ||
name: "IPv6", | ||
iptablesChains: chains, | ||
iptablesRules: rules, | ||
isIPv6: true, | ||
expectedCalls: func(mockIPTables *iptablestest.MockInterfaceMockRecorder) { | ||
mockIPTables.Restore(`*filter | ||
:TEST-CHAIN1 - [0:0] | ||
:TEST-CHAIN2 - [0:0] | ||
-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 80 | ||
-A TEST-CHAIN1 -j ACCEPT -p tcp --dport 443 | ||
-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 80 | ||
-A TEST-CHAIN2 -j ACCEPT -p tcp --dport 443 | ||
COMMIT | ||
`, false, true) | ||
mockIPTables.DeleteChain(iptables.ProtocolIPv6, iptables.FilterTable, chain1) | ||
mockIPTables.DeleteChain(iptables.ProtocolIPv6, iptables.FilterTable, chain2) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockIPTables := iptablestest.NewMockInterface(ctrl) | ||
c := &Client{iptables: mockIPTables} | ||
tt.expectedCalls(mockIPTables.EXPECT()) | ||
|
||
assert.NoError(t, c.AddOrUpdateNodeNetworkPolicyIPTables(tt.iptablesChains, tt.iptablesRules, tt.isIPv6)) | ||
for _, chain := range chains { | ||
var exists bool | ||
if tt.isIPv6 { | ||
_, exists = c.nodeNetworkPolicyIPTablesIPv6.Load(chain) | ||
} else { | ||
_, exists = c.nodeNetworkPolicyIPTablesIPv4.Load(chain) | ||
} | ||
Comment on lines
+1893
to
+1897
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
assert.True(t, exists) | ||
} | ||
|
||
assert.NoError(t, c.DeleteNodeNetworkPolicyIPTables(tt.iptablesChains, tt.isIPv6)) | ||
for _, chain := range chains { | ||
var exists bool | ||
if tt.isIPv6 { | ||
_, exists = c.nodeNetworkPolicyIPTablesIPv6.Load(chain) | ||
} else { | ||
_, exists = c.nodeNetworkPolicyIPTablesIPv4.Load(chain) | ||
} | ||
assert.False(t, exists) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need "NodeNetworkPolicy" in the methods' name? I feel it's better to name it as a general one if the function has not to be bound with NodeNetworkPolicy.