Skip to content

Commit d9e747c

Browse files
committed
fix(networkpolicy): improve cilium chainer detection and handling
- Use node capabilities to determine if cilium chainer is present - Check for existence of cilium_net link as a fallback- Add unit tests for allowEBPFNetworkPolicy function - Update integration tests to cover new behavior Signed-off-by: l1b0k <[email protected]>
1 parent f4e59a6 commit d9e747c

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

cmd/terway-cli/cni_linux.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,26 @@ func switchDataPathV2() bool {
3838
// new node ( based on user require).
3939
// true -> false: keep cilium chain, but disable policy
4040
func allowEBPFNetworkPolicy(require bool) (bool, error) {
41-
has, err := hasCilium()
42-
if err != nil {
41+
store := nodecap.NewFileNodeCapabilities(nodeCapabilitiesFile)
42+
if err := store.Load(); err != nil {
4343
return false, err
4444
}
45-
if has {
45+
switch store.Get(nodecap.NodeCapabilityHasCiliumChainer) {
46+
case True:
47+
fmt.Printf("has prev cilium chainer\n")
4648
return true, nil
49+
case False:
50+
fmt.Printf("no prev cilium chainer\n")
51+
return false, nil
52+
}
53+
54+
_, err := netlink.LinkByName("cilium_net")
55+
if err == nil {
56+
fmt.Printf("link cilium_net exist\n")
57+
return true, nil
58+
}
59+
if !errors.As(err, &netlink.LinkNotFoundError{}) {
60+
return false, err
4761
}
4862

4963
return require, nil

cmd/terway-cli/cni_linux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,7 @@ func Test_isMounted(t *testing.T) {
274274
_, err := isMounted("/sys/fs/cgroup")
275275
assert.NoError(t, err)
276276
}
277+
278+
func Test_allowEBPFNetworkPolicy(t *testing.T) {
279+
280+
}

cmd/terway-cli/cni_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"os"
45
"testing"
56

7+
"github.com/AliyunContainerService/terway/pkg/utils/nodecap"
68
"github.com/Jeffail/gabs/v2"
79
"github.com/stretchr/testify/assert"
810
)
@@ -202,6 +204,8 @@ func TestVeth(t *testing.T) {
202204
_switchDataPathV2 = func() bool {
203205
return true
204206
}
207+
208+
_ = os.Remove(nodeCapabilitiesFile)
205209
out, err := mergeConfigList([][]byte{
206210
[]byte(`{
207211
"type":"terway",
@@ -225,6 +229,8 @@ func TestVethWithNoPolicy(t *testing.T) {
225229
_switchDataPathV2 = func() bool {
226230
return true
227231
}
232+
233+
_ = os.Remove(nodeCapabilitiesFile)
228234
out, err := mergeConfigList([][]byte{
229235
[]byte(`{
230236
"type":"terway",
@@ -249,6 +255,8 @@ func TestVethToDatapathV2(t *testing.T) {
249255
_switchDataPathV2 = func() bool {
250256
return true
251257
}
258+
259+
_ = os.Remove(nodeCapabilitiesFile)
252260
out, err := mergeConfigList([][]byte{
253261
[]byte(`{
254262
"type":"terway",
@@ -269,3 +277,34 @@ func TestVethToDatapathV2(t *testing.T) {
269277
assert.Equal(t, "datapathv2", g.Path("plugins.0.eniip_virtual_type").Data())
270278
assert.Equal(t, "cilium-cni", g.Path("plugins.1.type").Data())
271279
}
280+
281+
func TestVethNotAllowToSwitch(t *testing.T) {
282+
_switchDataPathV2 = func() bool {
283+
return true
284+
}
285+
286+
_ = os.Remove(nodeCapabilitiesFile)
287+
store := nodecap.NewFileNodeCapabilities(nodeCapabilitiesFile)
288+
store.Set(nodecap.NodeCapabilityHasCiliumChainer, False)
289+
err := store.Save()
290+
assert.NoError(t, err)
291+
292+
out, err := mergeConfigList([][]byte{
293+
[]byte(`{
294+
"type":"terway",
295+
"foo":"bar",
296+
"network_policy_provider": "ebpf"
297+
}`)}, &feature{
298+
EBPF: true,
299+
EDT: true,
300+
EnableNetworkPolicy: true,
301+
})
302+
assert.NoError(t, err)
303+
304+
g, err := gabs.ParseJSON([]byte(out))
305+
assert.NoError(t, err)
306+
307+
assert.Equal(t, "terway", g.Path("plugins.0.type").Data())
308+
assert.Equal(t, 1, len(g.Path("plugins").Children()))
309+
assert.Equal(t, "veth", g.Path("plugins.0.eniip_virtual_type").Data())
310+
}

0 commit comments

Comments
 (0)