Add support for Provider network interface 50/67150/3
authorRitu Sood <ritu.sood@intel.com>
Thu, 28 Feb 2019 20:17:07 +0000 (12:17 -0800)
committerRitu Sood <ritu.sood@intel.com>
Tue, 5 Mar 2019 08:22:55 +0000 (00:22 -0800)
To support provider networks added a new
netType "provider". Static IP address
required for provider network interface.

Change-Id: I40f508ead2eefc352b735c6d4773d20fa821769a
Signed-off-by: Ritu Sood <ritu.sood@intel.com>
cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go
internal/pkg/ovn/ovn_test.go
internal/pkg/ovn/pods.go

index 923363b..dda42ad 100644 (file)
@@ -128,7 +128,7 @@ func addMultipleInterfaces(args *skel.CmdArgs, ovnAnnotation, namespace, podName
                gatewayIP := ovnNet["gateway_ip"]
                defaultGateway := ovnNet["defaultGateway"]
 
-               if ipAddress == "" || macAddress == "" || gatewayIP == "" {
+               if ipAddress == "" || macAddress == "" {
                        logrus.Errorf("failed in pod annotation key extract")
                        return nil
                }
index 2e558a6..00aba8b 100644 (file)
@@ -56,14 +56,14 @@ var _ = Describe("Add logical Port", func() {
                                "ovn-nbctl --timeout=15 --wait=sb -- --may-exist lsp-add " + netName + " " + portName + " -- lsp-set-addresses " + portName + " dynamic -- set logical_switch_port " + portName + " external-ids:namespace= external-ids:logical_switch=" + netName + " external-ids:pod=true",
                        })
 
-                       fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
-                               Cmd:    "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
-                               Output: gwCIDR,
-                       })
                        fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
                                Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " dynamic_addresses",
                                Output: macIPAddress,
                        })
+                       fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
+                               Cmd:    "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
+                               Output: gwCIDR,
+                       })
 
                        fexec := &fakeexec.FakeExec{
                                CommandScript: fakeCmds,
@@ -105,7 +105,76 @@ var _ = Describe("Add logical Port", func() {
                        )
 
                        ovnController.addLogicalPort(&okPod)
-                       _, _ = ovnController.kube.GetAnnotationsOnPod("", "ok")
+                       Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))
+
+                       return nil
+               }
+
+               err := app.Run([]string{app.Name})
+               Expect(err).NotTo(HaveOccurred())
+       })
+
+       It("tests Pod provider", func() {
+               app.Action = func(ctx *cli.Context) error {
+                       const (
+                               gwIP         string = "10.1.1.1"
+                               gwCIDR       string = gwIP + "/24"
+                               netName      string = "ovn-prot-net"
+                               portName     string = "_ok_net0"
+                               macIPAddress string = "0a:00:00:00:00:01 192.168.1.3/24"
+                       )
+                       fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
+                               Cmd:    "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
+                               Output: netName,
+                       })
+
+                       fakeCmds = ovntest.AddFakeCmdsNoOutputNoError(fakeCmds, []string{
+                               "ovn-nbctl --timeout=15 --may-exist lsp-add " + netName + " " + portName + " -- lsp-set-addresses " + portName + " " + macIPAddress + " -- --if-exists clear logical_switch_port " + portName + " dynamic_addresses",
+                       })
+
+                       fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
+                               Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " addresses",
+                               Output: macIPAddress,
+                       })
+
+                       fexec := &fakeexec.FakeExec{
+                               CommandScript: fakeCmds,
+                               LookPathFunc: func(file string) (string, error) {
+                                       return fmt.Sprintf("/fake-bin/%s", file), nil
+                               },
+                       }
+                       err := util.SetExec(fexec)
+                       Expect(err).NotTo(HaveOccurred())
+
+                       _, err = config.InitConfig(ctx, fexec, nil)
+                       Expect(err).NotTo(HaveOccurred())
+
+                       fakeClient := &fake.Clientset{}
+                       var fakeWatchFactory factory.WatchFactory
+
+                       ovnController := NewOvnController(fakeClient, &fakeWatchFactory)
+                       var (
+                               okPod = v1.Pod{
+                                       TypeMeta: metav1.TypeMeta{
+                                               Kind:       "Pod",
+                                               APIVersion: "v1",
+                                       },
+                                       ObjectMeta: metav1.ObjectMeta{
+                                               Name:        "ok",
+                                               Annotations: map[string]string{"ovnNetwork": "[{ \"name\": \"ovn-prot-net\", \"interface\": \"net0\", \"netType\": \"provider\", \"ipAddress\": \"192.168.1.3/24\", \"macAddress\": \"0a:00:00:00:00:01\" }]"},
+                                       },
+                                       Spec: v1.PodSpec{
+                                               Containers: []v1.Container{
+                                                       {
+                                                               Name: "by-name",
+                                                       },
+                                                       {},
+                                               },
+                                       },
+                               }
+                       )
+                       ovnController.addLogicalPort(&okPod)
+                       Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))
 
                        return nil
                }
@@ -113,4 +182,5 @@ var _ = Describe("Add logical Port", func() {
                err := app.Run([]string{app.Name})
                Expect(err).NotTo(HaveOccurred())
        })
+
 })
index cc3d459..770f4e5 100644 (file)
@@ -80,7 +80,7 @@ func (oc *Controller) deleteLogicalPort(pod *kapi.Pod) {
        return
 }
 
-func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipAddress, macAddress, interfaceName string) (annotation string) {
+func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipAddress, macAddress, interfaceName, netType string) (annotation string) {
        var out, stderr string
        var err error
        var isStaticIP bool
@@ -136,11 +136,6 @@ func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipA
                }
        }
        oc.logicalPortCache[portName] = logicalSwitch
-       gatewayIP, mask, err := oc.getGatewayFromSwitch(logicalSwitch)
-       if err != nil {
-               logrus.Errorf("Error obtaining gateway address for switch %s: %s", logicalSwitch, err)
-               return
-       }
 
        count := 30
        for count > 0 {
@@ -178,7 +173,18 @@ func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipA
                logrus.Errorf("Error while obtaining addresses for %s", portName)
                return
        }
-       annotation = fmt.Sprintf(`{\"ip_address\":\"%s/%s\", \"mac_address\":\"%s\", \"gateway_ip\": \"%s\"}`, addresses[1], mask, addresses[0], gatewayIP)
+
+       if netType == "virtual" {
+               gatewayIP, mask, err := oc.getGatewayFromSwitch(logicalSwitch)
+               if err != nil {
+                       logrus.Errorf("Error obtaining gateway address for switch %s: %s", logicalSwitch, err)
+                       return
+               }
+               annotation = fmt.Sprintf(`{\"ip_address\":\"%s/%s\", \"mac_address\":\"%s\", \"gateway_ip\": \"%s\"}`, addresses[1], mask, addresses[0], gatewayIP)
+       } else {
+               annotation = fmt.Sprintf(`{\"ip_address\":\"%s\", \"mac_address\":\"%s\", \"gateway_ip\": \"%s\"}`, addresses[1], addresses[0], "")
+       }
+
        return annotation
 }
 
@@ -203,7 +209,7 @@ func findLogicalSwitch(name string) bool {
 
 func (oc *Controller) addLogicalPort(pod *kapi.Pod) {
        var logicalSwitch string
-       var ipAddress, macAddress, interfaceName, defaultGateway string
+       var ipAddress, macAddress, interfaceName, defaultGateway, netType string
 
        annotation := pod.Annotations["ovnNetwork"]
 
@@ -217,10 +223,15 @@ func (oc *Controller) addLogicalPort(pod *kapi.Pod) {
                ovnString = "["
                for _, net := range ovnNetObjs {
                        logicalSwitch = net["name"].(string)
+                       if !findLogicalSwitch(logicalSwitch) {
+                               logrus.Errorf("Logical Switch not found")
+                               return
+                       }
                        if _, ok := net["interface"]; ok {
                                interfaceName = net["interface"].(string)
                        } else {
-                               interfaceName = ""
+                               logrus.Errorf("Interface name must be provided")
+                               return
                        }
                        if _, ok := net["ipAddress"]; ok {
                                ipAddress = net["ipAddress"].(string)
@@ -237,14 +248,24 @@ func (oc *Controller) addLogicalPort(pod *kapi.Pod) {
                        } else {
                                defaultGateway = "false"
                        }
-                       if !findLogicalSwitch(logicalSwitch) {
+                       if _, ok := net["netType"]; ok {
+                               netType = net["netType"].(string)
+                       } else {
+                               netType = "virtual"
+                       }
+                       if netType != "provider" && netType != "virtual" {
+                               logrus.Errorf("netType is not supported")
                                return
                        }
-                       if interfaceName == "" {
-                               logrus.Errorf("Interface name must be provided")
+                       if netType == "provider" && ipAddress == "" {
+                               logrus.Errorf("ipAddress must be provided for netType Provider")
+                               return
+                       }
+                       if netType == "provider" && defaultGateway == "true" {
+                               logrus.Errorf("defaultGateway not supported for provider network - Use ovnNetworkRoutes to add routes")
                                return
                        }
-                       outStr = oc.addLogicalPortWithSwitch(pod, logicalSwitch, ipAddress, macAddress, interfaceName)
+                       outStr = oc.addLogicalPortWithSwitch(pod, logicalSwitch, ipAddress, macAddress, interfaceName, netType)
                        if outStr == "" {
                                return
                        }