Skip to content

Commit ea5c9a7

Browse files
authored
Add logic to deal with 0.0.0.0/0 which ipset not support. (#599)
* Add logic to deal with 0.0.0.0/0 which ipset not support. * Add unit test for checking 0.0.0.0/0 ipset entry logic.
1 parent cf25cd3 commit ea5c9a7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

npm/nwpolicy.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,19 @@ func createCidrsRule(ingressOrEgress, policyName, ns string, ipsetEntries [][]st
211211
log.Printf("Error creating ipset %s", ipCidrSet)
212212
}
213213
for _, ipCidrEntry := range util.DropEmptyFields(ipCidrSet) {
214-
if err := ipsMgr.AddToSet(setName, ipCidrEntry, util.IpsetNetHashFlag); err != nil {
215-
log.Printf("Error adding ip cidrs %s into ipset %s", ipCidrEntry, ipCidrSet)
214+
// Ipset doesn't allow 0.0.0.0/0 to be added. A general solution is split 0.0.0.0/1 in half which convert to
215+
// 1.0.0.0/1 and 128.0.0.0/1
216+
if (ipCidrEntry == "0.0.0.0/0") {
217+
splitEntry := [2]string{"1.0.0.0/1", "128.0.0.0/1"}
218+
for _, entry := range splitEntry {
219+
if err := ipsMgr.AddToSet(setName, entry, util.IpsetNetHashFlag); err != nil {
220+
log.Printf("Error adding ip cidrs %s into ipset %s", entry, ipCidrSet)
221+
}
222+
}
223+
} else {
224+
if err := ipsMgr.AddToSet(setName, ipCidrEntry, util.IpsetNetHashFlag); err != nil {
225+
log.Printf("Error adding ip cidrs %s into ipset %s", ipCidrEntry, ipCidrSet)
226+
}
216227
}
217228
}
218229
}

npm/nwpolicy_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@ func TestAddNetworkPolicy(t *testing.T) {
7777
Spec: networkingv1.NetworkPolicySpec{
7878
Ingress: []networkingv1.NetworkPolicyIngressRule{
7979
networkingv1.NetworkPolicyIngressRule{
80-
From: []networkingv1.NetworkPolicyPeer{{
81-
PodSelector: &metav1.LabelSelector{
82-
MatchLabels: map[string]string{"app": "test"},
80+
From: []networkingv1.NetworkPolicyPeer{
81+
networkingv1.NetworkPolicyPeer{
82+
PodSelector: &metav1.LabelSelector{
83+
MatchLabels: map[string]string{"app": "test"},
84+
},
8385
},
84-
}},
86+
networkingv1.NetworkPolicyPeer{
87+
IPBlock: &networkingv1.IPBlock{
88+
CIDR: "0.0.0.0/0",
89+
},
90+
},
91+
},
8592
Ports: []networkingv1.NetworkPolicyPort{{
8693
Protocol: &tcp,
8794
Port: &port8000,
@@ -98,6 +105,17 @@ func TestAddNetworkPolicy(t *testing.T) {
98105
}
99106
npMgr.Unlock()
100107

108+
ipsMgr = npMgr.nsMap[util.KubeAllNamespacesFlag].ipsMgr
109+
110+
// Check whether 0.0.0.0/0 got translated to 1.0.0.0/1 and 128.0.0.0/1
111+
if ! ipsMgr.Exists("allow-ingress-in-ns-test-nwpolicy-0in", "1.0.0.0/1", util.IpsetNetHashFlag) {
112+
t.Errorf("TestDeleteFromSet failed @ ipsMgr.AddToSet")
113+
}
114+
115+
if ! ipsMgr.Exists("allow-ingress-in-ns-test-nwpolicy-0in", "128.0.0.0/1", util.IpsetNetHashFlag) {
116+
t.Errorf("TestDeleteFromSet failed @ ipsMgr.AddToSet")
117+
}
118+
101119
allowEgress := &networkingv1.NetworkPolicy{
102120
ObjectMeta: metav1.ObjectMeta{
103121
Name: "allow-egress",

0 commit comments

Comments
 (0)