diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/ovn4nfvk8s-cni/app/helper_linux.go | 23 | ||||
-rw-r--r-- | cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go | 5 | ||||
-rw-r--r-- | cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go | 126 |
3 files changed, 14 insertions, 140 deletions
diff --git a/cmd/ovn4nfvk8s-cni/app/helper_linux.go b/cmd/ovn4nfvk8s-cni/app/helper_linux.go index 676901e..2dba628 100644 --- a/cmd/ovn4nfvk8s-cni/app/helper_linux.go +++ b/cmd/ovn4nfvk8s-cni/app/helper_linux.go @@ -6,7 +6,7 @@ import ( "fmt" "net" "os/exec" - "regexp" + "strconv" "strings" "github.com/sirupsen/logrus" @@ -37,7 +37,7 @@ func renameLink(curName, newName string) error { return nil } -func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, gatewayIP, defaultGateway string, mtu int) (*current.Interface, *current.Interface, error) { +func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, gatewayIP, defaultGateway string, idx, mtu int) (*current.Interface, *current.Interface, error) { hostIface := ¤t.Interface{} contIface := ¤t.Interface{} @@ -97,9 +97,7 @@ func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, } // rename the host end of veth pair - re := regexp.MustCompile("(\\d+)\\D*\\z") - index := re.FindAllString(ifName, -1) - hostIface.Name = containerID[:14] + index[0] + hostIface.Name = containerID[:14] + strconv.Itoa(idx) if err := renameLink(oldHostVethName, hostIface.Name); err != nil { return nil, nil, fmt.Errorf("failed to rename %s to %s: %v", oldHostVethName, hostIface.Name, err) } @@ -108,21 +106,24 @@ func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, } // ConfigureInterface sets up the container interface -var ConfigureInterface = func(args *skel.CmdArgs, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, mtu int) ([]*current.Interface, error) { +var ConfigureInterface = func(args *skel.CmdArgs, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, idx, mtu int) ([]*current.Interface, error) { netns, err := ns.GetNS(args.Netns) if err != nil { return nil, fmt.Errorf("failed to open netns %q: %v", args.Netns, err) } defer netns.Close() - hostIface, contIface, err := setupInterface(netns, args.ContainerID, interfaceName, macAddress, ipAddress, gatewayIP, defaultGateway, mtu) - if err != nil { - return nil, err - } + var ifaceID string - if interfaceName != "" { + if interfaceName != "*" { ifaceID = fmt.Sprintf("%s_%s_%s", namespace, podName, interfaceName) } else { ifaceID = fmt.Sprintf("%s_%s", namespace, podName) + interfaceName = args.IfName + defaultGateway = "true" + } + hostIface, contIface, err := setupInterface(netns, args.ContainerID, interfaceName, macAddress, ipAddress, gatewayIP, defaultGateway, idx, mtu) + if err != nil { + return nil, err } ovsArgs := []string{ diff --git a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go index 2585fcf..c176700 100644 --- a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go +++ b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go @@ -142,8 +142,7 @@ func addMultipleInterfaces(args *skel.CmdArgs, ovnAnnotation, namespace, podName logrus.Errorf("addMultipleInterfaces: interface can't be null") return nil } - logrus.Debugf("addMultipleInterfaces: ipAddress %v %v", ipAddress, interfaceName) - interfacesArray, err = app.ConfigureInterface(args, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway, config.Default.MTU) + interfacesArray, err = app.ConfigureInterface(args, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway, index, config.Default.MTU) if err != nil { logrus.Errorf("Failed to configure interface in pod: %v", err) return nil @@ -318,7 +317,7 @@ func cmdDel(args *skel.CmdArgs) error { } func main() { - logrus.Infof("ovn4nfvk8s-cni CNI Invoked by Multus") + logrus.Infof("ovn4nfvk8s-cni invoked") c := cli.NewApp() c.Name = "ovn4nfvk8s-cni" c.Usage = "a CNI plugin to set up or tear down a additional interfaces with OVN" diff --git a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go deleted file mode 100644 index 16296e3..0000000 --- a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go +++ /dev/null @@ -1,126 +0,0 @@ -// +build linux - -package main - -import ( - "encoding/json" - - "github.com/containernetworking/cni/pkg/skel" - "github.com/containernetworking/cni/pkg/types" - "github.com/containernetworking/cni/pkg/types/current" - "net" - "ovn4nfv-k8s-plugin/cmd/ovn4nfvk8s-cni/app" - "reflect" - "testing" -) - -func TestAddMultipleInterfaces(t *testing.T) { - oldConfigureInterface := app.ConfigureInterface - // as we are exiting, revert ConfigureInterface back at end of function - defer func() { app.ConfigureInterface = oldConfigureInterface }() - app.ConfigureInterface = func(args *skel.CmdArgs, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, mtu int) ([]*current.Interface, error) { - return []*current.Interface{ - { - Name: "pod", - Mac: "0a:00:00:00:00:0c", - Sandbox: "102103104", - }}, nil - } - oldConfigureRoute := app.ConfigureRoute - defer func() { app.ConfigureRoute = oldConfigureRoute }() - app.ConfigureRoute = func(args *skel.CmdArgs, dst, gw, dev string) error { - return nil - } - - args := &skel.CmdArgs{"102103104", "default", "eth0", "", "", nil} - ovnAnnotation := "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\"}] " - result := addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result == nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - resultSave := result - ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"defaultGateway\":\"true\",\"interface\":\"net0\"}] " - result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result == nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\"}] " - result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result != nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - ovnAnnotation = "[{\"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\"}] " - result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result != nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\"}, {\"ip_address\":\"172.16.25.2/24\", \"mac_address\":\"0a:00:00:00:00:0d\", \"gateway_ip\": \"172.16.25.1\",\"interface\":\"net1\"}]" - result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result == nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\", \"defaultGateway\":\"true\"}, {\"ip_address\":\"172.16.25.2/24\", \"mac_address\":\"0a:00:00:00:00:0d\", \"gateway_ip\": \"172.16.25.1\",\"interface\":\"net1\"}]" - result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod") - if result == nil { - t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation) - } - // Test add route feature - ovnRoutesAnnotation := "[{ \"dst\": \"172.16.29.0/24\", \"gw\": \"172.16.24.1\", \"dev\": \"eth0\" }]" - result = addRoutes(args, ovnRoutesAnnotation, resultSave) - if result == nil { - t.Errorf("Failed addRoutes %+v", ovnRoutesAnnotation) - } - - ovnRoutesAnnotation = "[{ \"dst\": \"172.16.30.0/24\", \"gw\": \"172.16.25.1\", \"dev\": \"eth0\"}, { \"dst\": \"172.16.31.0/24\", \"gw\": \"172.16.26.1\", \"dev\": \"eth1\" }]" - result = addRoutes(args, ovnRoutesAnnotation, resultSave) - if result == nil { - t.Errorf("Failed addRoutes %+v", ovnRoutesAnnotation) - } - newResult, err := current.NewResultFromResult(result) - if err != nil { - t.Errorf("Failed addMultipleInterfaces %+v", newResult) - } - addr1, addrNet1, _ := net.ParseCIDR("172.16.24.2/24") - addr2, addrNet2, _ := net.ParseCIDR("172.16.29.0/24") - addr3, addrNet3, _ := net.ParseCIDR("172.16.30.0/24") - addr4, addrNet4, _ := net.ParseCIDR("172.16.31.0/24") - expectedResult := ¤t.Result{ - CNIVersion: "0.3.1", - Interfaces: []*current.Interface{ - { - Name: "pod", - Mac: "0a:00:00:00:00:0c", - Sandbox: "102103104", - }, - }, - IPs: []*current.IPConfig{ - { - Version: "4", - Interface: current.Int(1), - Address: net.IPNet{IP: addr1, Mask: addrNet1.Mask}, - Gateway: net.ParseIP("172.16.24.1"), - }, - }, - Routes: []*types.Route{ - { - Dst: net.IPNet{IP: addr2, Mask: addrNet2.Mask}, - GW: net.ParseIP("172.16.24.1"), - }, - { - Dst: net.IPNet{IP: addr3, Mask: addrNet3.Mask}, - GW: net.ParseIP("172.16.25.1"), - }, - { - Dst: net.IPNet{IP: addr4, Mask: addrNet4.Mask}, - GW: net.ParseIP("172.16.26.1"), - }, - }, - DNS: types.DNS{}, - } - jsonBytes1, err := json.Marshal(newResult.Routes) - jsonBytes2, err := json.Marshal(expectedResult.Routes) - if !reflect.DeepEqual(jsonBytes1, jsonBytes2) { - t.Errorf("Routes are not correct") - } - -} |