Skip to content

Commit

Permalink
Pass status along ipam update
Browse files Browse the repository at this point in the history
Signed-off-by: Lionel Jouin <[email protected]>
  • Loading branch information
LionelJouin committed Sep 20, 2024
1 parent fa737f8 commit 79b1e5e
Show file tree
Hide file tree
Showing 25 changed files with 359 additions and 58 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
go-version: ${{ env.GO_VERSION }}
- uses: ibiqlik/action-yamllint@v3
with:
format: auto
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- name: setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
go-version: ${{ env.GO_VERSION }}
- name: Build on all supported architectures
run: |
set -e
Expand All @@ -71,7 +71,7 @@ jobs:
- name: setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
go-version: ${{ env.GO_VERSION }}
- name: Set up Go for root
run: |
sudo ln -sf `which go` `sudo which go` || true
Expand Down Expand Up @@ -102,6 +102,6 @@ jobs:
- name: setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
go-version: ${{ env.GO_VERSION }}
- name: test
run: bash ./test_windows.sh
4 changes: 4 additions & 0 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ func ExecCheck(plugin string, netconf []byte) error {
func ExecDel(plugin string, netconf []byte) error {
return invoke.DelegateDel(context.TODO(), plugin, netconf, nil)
}

func ExecStatus(plugin string, netconf []byte) error {
return invoke.DelegateStatus(context.TODO(), plugin, netconf, nil)
}
8 changes: 8 additions & 0 deletions pkg/ipam/ipam_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func ConfigureIface(ifName string, res *current.Result) error {
route.Scope = netlink.Scope(*r.Scope)
}

if r.Table != nil {
route.Table = *r.Table
}

if r.Scope != nil {
route.Scope = netlink.Scope(*r.Scope)
}

if err = netlink.RouteAddEcmp(&route); err != nil {
return fmt.Errorf("failed to add route '%v via %v dev %v metric %d (Scope: %v, Table: %d)': %v", r.Dst, gw, ifName, r.Priority, route.Scope, route.Table, err)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/testutils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ func CmdDel(cniNetns, cniContainerID, cniIfname string, f func() error) error {
func CmdDelWithArgs(args *skel.CmdArgs, f func() error) error {
return CmdDel(args.Netns, args.ContainerID, args.IfName, f)
}

func CmdStatus(f func() error) error {
os.Setenv("CNI_COMMAND", "STATUS")
os.Setenv("CNI_PATH", os.Getenv("PATH"))
os.Setenv("CNI_NETNS_OVERRIDE", "1")
defer envCleanup()

return f()
}
9 changes: 8 additions & 1 deletion pkg/testutils/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// AllSpecVersions contains all CNI spec version numbers
var AllSpecVersions = [...]string{"0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0"}
var AllSpecVersions = [...]string{"0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0", "1.1.0"}

// SpecVersionHasIPVersion returns true if the given CNI specification version
// includes the "version" field in the IP address elements
Expand All @@ -39,6 +39,13 @@ func SpecVersionHasCHECK(ver string) bool {
return ok
}

// SpecVersionHasSTATUS returns true if the given CNI specification version
// supports the STATUS command
func SpecVersionHasSTATUS(ver string) bool {
ok, _ := version.GreaterThanOrEqualTo(ver, "1.1.0")
return ok
}

// SpecVersionHasChaining returns true if the given CNI specification version
// supports plugin chaining
func SpecVersionHasChaining(ver string) bool {
Expand Down
23 changes: 19 additions & 4 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,11 @@ func cmdDel(args *skel.CmdArgs) error {

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Status: cmdStatus,
/* FIXME GC */
/* FIXME Status */
}, version.All, bv.BuildString("bridge"))
}

Expand Down Expand Up @@ -1090,3 +1090,18 @@ func cmdCheck(args *skel.CmdArgs) error {
func uniqueID(containerID, cniIface string) string {
return containerID + "-" + cniIface
}

func cmdStatus(args *skel.CmdArgs) error {
conf := NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %w", err)
}

if conf.IPAM.Type != "" {
if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
return err
}
}

return nil
}
13 changes: 13 additions & 0 deletions plugins/main/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ type (

func newTesterByVersion(version string, testNS, targetNS ns.NetNS) cmdAddDelTester {
switch {
case strings.HasPrefix(version, "1.1."):
return &testerV10x{
testNS: testNS,
targetNS: targetNS,
}
case strings.HasPrefix(version, "1.0."):
return &testerV10x{
testNS: testNS,
Expand Down Expand Up @@ -1483,6 +1488,14 @@ func (tester *testerV01xOr02x) cmdAddTest(tc testCase, dataDir string) (types.Re
err := tester.testNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

// check that STATUS is
if testutils.SpecVersionHasSTATUS(tc.cniVersion) {
err := testutils.CmdStatus(func() error {
return cmdStatus(&skel.CmdArgs{StdinData: []byte(tc.netConfJSON(dataDir))})
})
Expect(err).NotTo(HaveOccurred())
}

r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error {
return cmdAdd(tester.args)
})
Expand Down
21 changes: 17 additions & 4 deletions plugins/main/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ func cmdDel(args *skel.CmdArgs) error {

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Status: cmdStatus,
/* FIXME GC */
/* FIXME Status */
}, version.All, bv.BuildString("dummy"))
}

Expand Down Expand Up @@ -294,3 +294,16 @@ func validateCniContainerInterface(intf current.Interface) error {

return nil
}

func cmdStatus(args *skel.CmdArgs) error {
conf := types.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %w", err)
}

if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
return err
}

return nil
}
9 changes: 8 additions & 1 deletion plugins/main/dummy/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type (

func newTesterByVersion(version string) tester {
switch {
case strings.HasPrefix(version, "1.0."):
case strings.HasPrefix(version, "1."):
return &testerV10x{}
case strings.HasPrefix(version, "0.4."):
return &testerV04x{}
Expand Down Expand Up @@ -261,6 +261,13 @@ var _ = Describe("dummy Operations", func() {
defer GinkgoRecover()

var err error
if testutils.SpecVersionHasSTATUS(ver) {
err = testutils.CmdStatus(func() error {
return cmdStatus(args)
})
Expect(err).NotTo(HaveOccurred())
}

result, _, err = testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expand Down
25 changes: 21 additions & 4 deletions plugins/main/host-device/host-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,11 @@ func getLink(devname, hwaddr, kernelpath, pciaddr string, auxDev string) (netlin

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Status: cmdStatus,
/* FIXME GC */
/* FIXME Status */
}, version.All, bv.BuildString("host-device"))
}

Expand Down Expand Up @@ -630,3 +630,20 @@ func validateCniContainerInterface(intf current.Interface) error {

return nil
}

func cmdStatus(args *skel.CmdArgs) error {
conf := NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %w", err)
}

if conf.IPAM.Type != "" {
if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
return err
}
}

// TODO: Check if host device exists.

return nil
}
11 changes: 10 additions & 1 deletion plugins/main/host-device/host-device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ type (

func newTesterByVersion(version string) tester {
switch {
case strings.HasPrefix(version, "1.0."):
case strings.HasPrefix(version, "1."):
return &testerV10x{}
case strings.HasPrefix(version, "0.4."):
return &testerV04x{}
Expand Down Expand Up @@ -362,6 +362,15 @@ var _ = Describe("base functionality", func() {
"type": "host-device",
"device": %q
}`, ver, ifname)

// if v1.1 or greater, call CmdStatus
if testutils.SpecVersionHasSTATUS(ver) {
err := testutils.CmdStatus(func() error {
return cmdStatus(&skel.CmdArgs{StdinData: []byte(conf)})
})
Expect(err).NotTo(HaveOccurred())
}

args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNS.Path(),
Expand Down
24 changes: 20 additions & 4 deletions plugins/main/ipvlan/ipvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ func cmdDel(args *skel.CmdArgs) error {

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Status: cmdStatus,
/* FIXME GC */
/* FIXME Status */
}, version.All, bv.BuildString("ipvlan"))
}

Expand Down Expand Up @@ -490,3 +490,19 @@ func validateCniContainerInterface(intf current.Interface, modeExpected string)

return nil
}

func cmdStatus(args *skel.CmdArgs) error {
conf := NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %w", err)
}
if conf.IPAM.Type != "" {
if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
return err
}
}

// TODO: Check if master interface exists.

return nil
}
9 changes: 8 additions & 1 deletion plugins/main/ipvlan/ipvlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func ipvlanAddCheckDelTest(conf, masterName string, originalNS, targetNS ns.NetN
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

if testutils.SpecVersionHasSTATUS(cniVersion) {
err = testutils.CmdStatus(func() error {
return cmdStatus(args)
})
Expect(err).NotTo(HaveOccurred())
}

result, _, err = testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expand Down Expand Up @@ -214,7 +221,7 @@ type (

func newTesterByVersion(version string) tester {
switch {
case strings.HasPrefix(version, "1.0."):
case strings.HasPrefix(version, "1."):
return &testerV10x{}
case strings.HasPrefix(version, "0.4.") || strings.HasPrefix(version, "0.3."):
return &testerV04x{}
Expand Down
25 changes: 21 additions & 4 deletions plugins/main/macvlan/macvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ func cmdDel(args *skel.CmdArgs) error {

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Add: cmdAdd,
Check: cmdCheck,
Del: cmdDel,
Status: cmdStatus,
/* FIXME GC */
/* FIXME Status */
}, version.All, bv.BuildString("macvlan"))
}

Expand Down Expand Up @@ -571,3 +571,20 @@ func validateCniContainerInterface(intf current.Interface, modeExpected string)

return nil
}

func cmdStatus(args *skel.CmdArgs) error {
conf := NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %w", err)
}

if conf.IPAM.Type != "" {
if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
return err
}
}

// TODO: Check if master interface exists.

return nil
}
Loading

0 comments on commit 79b1e5e

Please sign in to comment.