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 <lionel.jouin@est.tech>
  • Loading branch information
LionelJouin authored and squeed committed Oct 15, 2024
1 parent a4fc6f9 commit fec2d62
Show file tree
Hide file tree
Showing 23 changed files with 348 additions and 54 deletions.
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 @@ -819,11 +819,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 @@ -1085,3 +1085,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 @@ -181,11 +181,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 @@ -295,3 +295,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 @@ -551,11 +551,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 @@ -663,3 +663,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 @@ -351,11 +351,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 @@ -491,3 +491,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 @@ -429,11 +429,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 @@ -570,3 +570,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
}
30 changes: 29 additions & 1 deletion plugins/main/macvlan/macvlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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 @@ -322,6 +322,13 @@ var _ = Describe("macvlan Operations", func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

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 Expand Up @@ -434,6 +441,13 @@ var _ = Describe("macvlan Operations", func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

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 Expand Up @@ -520,6 +534,13 @@ var _ = Describe("macvlan 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 Expand Up @@ -660,6 +681,13 @@ var _ = Describe("macvlan Operations", func() {
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

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
Loading

0 comments on commit fec2d62

Please sign in to comment.