Skip to content

Commit

Permalink
datapath: enable networkpolicy equal to datapathv2
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Dec 3, 2024
1 parent 9e5b7a1 commit 11bac74
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 26 deletions.
58 changes: 32 additions & 26 deletions cmd/terway-cli/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,54 +213,60 @@ func mergeConfigList(configs [][]byte, f *feature) (string, error) {
if !ebpfSupport {
_ = plugin.Delete("eniip_virtual_type")
} else {
requireIPvlan := false

switch strings.ToLower(virtualType) {
case dataPathVeth, dataPathDefault:
datapath = dataPathVeth

// only for terway-eniip
if ebpfSupport && networkPolicyProvider == NetworkPolicyProviderEBPF {
allow, err := allowEBPFNetworkPolicy(f.EnableNetworkPolicy)
if err != nil {
return "", err
}
if allow {
requireEBPFChainer = true
datapath = dataPathV2
}
}
case dataPathIPvlan:
requireIPvlan = true
datapath = dataPathIPvlan

fallthrough
case dataPathV2:
requireEBPFChainer = true

if requireIPvlan && !_switchDataPathV2() {
fmt.Printf("keep ipvlan mode %v %v\n", requireIPvlan, !_switchDataPathV2())
_, err = plugin.Set(dataPathIPvlan, "eniip_virtual_type")
if err != nil {
return "", err
}
} else {
fmt.Printf("datapathv2 enabled\n")
_, err = plugin.Set(dataPathV2, "eniip_virtual_type")
if err != nil {
return "", err
}

if _switchDataPathV2() {
datapath = dataPathV2
}
case dataPathV2:
datapath = dataPathV2
}

if edtSupport {
_, err = plugin.Set("edt", "bandwidth_mode")
} else {
_, err = plugin.Set("tc", "bandwidth_mode")
switch datapath {
case dataPathVeth:
requireEBPFChainer = false
edtSupport = false
_, err = plugin.Set(dataPathVeth, "eniip_virtual_type")
if err != nil {
return "", err
}

Check warning on line 246 in cmd/terway-cli/cni.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-cli/cni.go#L245-L246

Added lines #L245 - L246 were not covered by tests
case dataPathIPvlan:
requireEBPFChainer = true
_, err = plugin.Set(dataPathIPvlan, "eniip_virtual_type")
if err != nil {
return "", err

Check warning on line 251 in cmd/terway-cli/cni.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-cli/cni.go#L251

Added line #L251 was not covered by tests
}
case dataPathV2:
requireEBPFChainer = true
_, err = plugin.Set(dataPathV2, "eniip_virtual_type")
if err != nil {
return "", err
}
default:
return "", fmt.Errorf("invalid datapath %s", datapath)

Check warning on line 260 in cmd/terway-cli/cni.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-cli/cni.go#L259-L260

Added lines #L259 - L260 were not covered by tests
}

if edtSupport {
_, err = plugin.Set("edt", "bandwidth_mode")
} else {
_, err = plugin.Set("tc", "bandwidth_mode")
}
if err != nil {
return "", err

Check warning on line 269 in cmd/terway-cli/cni.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-cli/cni.go#L269

Added line #L269 was not covered by tests
}
}
}
Expand Down
73 changes: 73 additions & 0 deletions cmd/terway-cli/cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Test_mergeConfigList_ipvl(t *testing.T) {
assert.Equal(t, "terway", g.Path("plugins.0.type").Data())
assert.Equal(t, "bar", g.Path("plugins.0.foo").Data())
assert.Equal(t, "cilium-cni", g.Path("plugins.2.type").Data())
assert.Equal(t, "ipvlan", g.Path("plugins.0.eniip_virtual_type").Data())
}

func Test_mergeConfigList_ipvl_exist(t *testing.T) {
Expand Down Expand Up @@ -196,3 +197,75 @@ func Test_mergeConfigList_datapathv2(t *testing.T) {
assert.Equal(t, "datapathv2", g.Path("plugins.1.datapath").Data())
assert.Equal(t, "portmap", g.Path("plugins.2.type").Data())
}

func TestVeth(t *testing.T) {
_switchDataPathV2 = func() bool {
return true
}
out, err := mergeConfigList([][]byte{
[]byte(`{
"type":"terway",
"foo":"bar"
}`)}, &feature{
EBPF: true,
EDT: true,
EnableNetworkPolicy: true,
})
assert.NoError(t, err)

g, err := gabs.ParseJSON([]byte(out))
assert.NoError(t, err)

assert.Equal(t, "terway", g.Path("plugins.0.type").Data())
assert.Equal(t, "veth", g.Path("plugins.0.eniip_virtual_type").Data())
assert.Equal(t, 1, len(g.Path("plugins").Children()))
}

func TestVethWithNoPolicy(t *testing.T) {
_switchDataPathV2 = func() bool {
return true
}
out, err := mergeConfigList([][]byte{
[]byte(`{
"type":"terway",
"foo":"bar",
"network_policy_provider": "ebpf"
}`)}, &feature{
EBPF: true,
EDT: true,
EnableNetworkPolicy: false,
})
assert.NoError(t, err)

g, err := gabs.ParseJSON([]byte(out))
assert.NoError(t, err)

assert.Equal(t, "terway", g.Path("plugins.0.type").Data())
assert.Equal(t, "veth", g.Path("plugins.0.eniip_virtual_type").Data())
assert.Equal(t, 1, len(g.Path("plugins").Children()))
}

func TestVethToDatapathV2(t *testing.T) {
_switchDataPathV2 = func() bool {
return true
}
out, err := mergeConfigList([][]byte{
[]byte(`{
"type":"terway",
"foo":"bar",
"network_policy_provider": "ebpf"
}`)}, &feature{
EBPF: true,
EDT: true,
EnableNetworkPolicy: true,
})
assert.NoError(t, err)

g, err := gabs.ParseJSON([]byte(out))
assert.NoError(t, err)

assert.Equal(t, "terway", g.Path("plugins.0.type").Data())
assert.Equal(t, 2, len(g.Path("plugins").Children()))
assert.Equal(t, "datapathv2", g.Path("plugins.0.eniip_virtual_type").Data())
assert.Equal(t, "cilium-cni", g.Path("plugins.1.type").Data())
}

0 comments on commit 11bac74

Please sign in to comment.