Change format of the network file 21/87721/1
authorRitu Sood <ritu.sood@intel.com>
Tue, 14 May 2019 16:36:48 +0000 (16:36 +0000)
committerRitu Sood <ritu.sood@intel.com>
Tue, 14 May 2019 16:44:47 +0000 (16:44 +0000)
Format of network file is different
between KUD and Plugin. Change the
format to match the format in KUD

Change-Id: I360741b23e91e60d790a50a7eeb213576b0508b3
Signed-off-by: Ritu Sood <ritu.sood@intel.com>
Issue-ID: MULTICLOUD-304

src/k8splugin/mock_files/mock_yamls/ovn4nfvk8s.yaml
src/k8splugin/plugins/network/plugin.go
src/k8splugin/plugins/network/plugin_test.go
src/k8splugin/plugins/network/v1/types.go
src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go
src/k8splugin/plugins/ovn4nfvk8s-network/plugin_test.go

index 1a26275..31351de 100644 (file)
@@ -1,15 +1,9 @@
 apiVersion: v1
-kind: OnapNetwork
+kind: Network
 metadata:
   name: ovn-priv-net
 spec:
-  config: '{
-         "cnitype": "ovn4nfvk8s",
-         "name": "mynet",
-         "subnet": "172.16.33.0/24",
-         "gateway": "172.16.33.1",
-         "routes": [{
-                 "dst": "172.16.29.1/24",
-                 "gw": "100.64.1.1"
-         }]
-  }'
\ No newline at end of file
+  cnitype: "ovn4nfvk8s"
+  name:    "ovn-priv-net"
+  subnet:  "172.16.33.0/24"
+  gateway: "172.16.33.1/24"
index fb16318..74ac347 100644 (file)
@@ -42,12 +42,7 @@ func Create(data *utils.ResourceData, client kubernetes.Interface) (string, erro
                return "", pkgerrors.Wrap(err, "Decode network object error")
        }
 
-       config, err := network.DecodeConfig()
-       if err != nil {
-               return "", pkgerrors.Wrap(err, "Fail to decode network's configuration")
-       }
-
-       cniType := config["cnitype"].(string)
+       cniType := network.Spec.CniType
        typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
        if !ok {
                return "", pkgerrors.New("No plugin for resource " + cniType + " found")
index 5bf649a..e8e113b 100644 (file)
@@ -71,7 +71,7 @@ func TestCreateNetwork(t *testing.T) {
                        input: &utils.ResourceData{
                                YamlFilePath: "../../mock_files/mock_yamls/service.yaml",
                        },
-                       expectedError: "Fail to decode network's configuration: Invalid configuration value",
+                       expectedError: "No plugin for resource",
                },
                {
                        label: "Fail to create a network",
index b4efa39..96484ef 100644 (file)
@@ -14,9 +14,7 @@ limitations under the License.
 package v1
 
 import (
-       "encoding/json"
 
-       pkgerrors "github.com/pkg/errors"
        metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/runtime"
        "k8s.io/apimachinery/pkg/runtime/schema"
@@ -33,7 +31,11 @@ type OnapNetwork struct {
 
 // OnapNetworkSpec is the spec for OnapNetwork resource
 type OnapNetworkSpec struct {
-       Config string `json:"config"`
+        CniType     string `json:"cnitype"`
+        Name        string `json:"name"`
+        Subnet      string `json:"subnet"`
+        Gateway     string `json:"gateway"`
+
 }
 
 // DeepCopyObject returns a generically typed copy of an object
@@ -50,18 +52,3 @@ func (in OnapNetwork) DeepCopyObject() runtime.Object {
 func (in OnapNetwork) GetObjectKind() schema.ObjectKind {
        return &in.TypeMeta
 }
-
-// DecodeConfig content
-func (in OnapNetwork) DecodeConfig() (map[string]interface{}, error) {
-       var raw map[string]interface{}
-
-       if in.Spec.Config == "" {
-               return nil, pkgerrors.New("Invalid configuration value")
-       }
-
-       if err := json.Unmarshal([]byte(in.Spec.Config), &raw); err != nil {
-               return nil, pkgerrors.Wrap(err, "JSON unmarshalling error")
-       }
-
-       return raw, nil
-}
index 959586b..32bca8f 100644 (file)
@@ -76,25 +76,21 @@ func init() {
 
 // CreateNetwork in OVN controller
 func CreateNetwork(network *v1.OnapNetwork) (string, error) {
-       config, err := network.DecodeConfig()
-       if err != nil {
-               return "", err
-       }
 
-       name := config["name"].(string)
-       if name == "" {
-               return "", pkgerrors.New("Empty Name value")
-       }
+        name := network.Spec.Name
+        if name == "" {
+                return "", pkgerrors.New("Invalid Network Name")
+        }
 
-       subnet := config["subnet"].(string)
-       if subnet == "" {
-               return "", pkgerrors.New("Empty Subnet value")
-       }
+        subnet := network.Spec.Subnet
+        if subnet == "" {
+                return "", pkgerrors.New("Invalid Subnet Address")
+        }
 
-       gatewayIPMask := config["gateway"].(string)
-       if gatewayIPMask == "" {
-               return "", pkgerrors.New("Empty Gateway IP Mask")
-       }
+        gatewayIPMask := network.Spec.Gateway
+        if gatewayIPMask == "" {
+                return "", pkgerrors.New("Invalid Gateway Address")
+        }
 
        routerMac, stderr, err := ovnCmd.Run(getAuthStr(), "--if-exist", "-v", "get", "logical_router_port", "rtos-"+name, "mac")
        if err != nil {
index ce848a7..6a1054e 100644 (file)
@@ -43,7 +43,7 @@ func TestCreateOVN4NFVK8SNetwork(t *testing.T) {
                {
                        label:         "Fail to decode a network",
                        input:         &v1.OnapNetwork{},
-                       expectedError: "Invalid configuration value",
+                       expectedError: "Invalid Network Name",
                },
                {
                        label: "Fail to create a network",
@@ -52,7 +52,7 @@ func TestCreateOVN4NFVK8SNetwork(t *testing.T) {
                                        Name: "test",
                                },
                                Spec: v1.OnapNetworkSpec{
-                                       Config: "{\"cnitype\": \"ovn4nfvk8s\",\"name\": \"mynet\",\"subnet\": \"172.16.33.0/24\",\"gateway\": \"172.16.33.1\",\"routes\": [{\"dst\": \"172.16.29.1/24\",\"gw\": \"100.64.1.1\"}]}",
+                                       CniType: "ovn4nfvk8s", Name: "mynet", Subnet: "172.16.33.0/24", Gateway: "172.16.33.1/24",
                                },
                        },
                        expectedError: "Failed to get logical router",
@@ -67,7 +67,7 @@ func TestCreateOVN4NFVK8SNetwork(t *testing.T) {
                                        Name: "test",
                                },
                                Spec: v1.OnapNetworkSpec{
-                                       Config: "{\"cnitype\": \"ovn4nfvk8s\",\"name\": \"mynet\",\"subnet\": \"172.16.33.0/24\",\"gateway\": \"172.16.33.1\",\"routes\": [{\"dst\": \"172.16.29.1/24\",\"gw\": \"100.64.1.1\"}]}",
+                                       CniType: "ovn4nfvk8s", Name: "mynet", Subnet: "172.16.33.0/24", Gateway: "172.16.33.1/24",
                                },
                        },
                        expectedResult: "mynet",