pass marshaled cni configlist from client to server
authorMichael Cambria <mcambria@redhat.com>
Tue, 23 Apr 2019 15:48:23 +0000 (11:48 -0400)
committerMichael Cambria <mcambria@redhat.com>
Tue, 23 Apr 2019 15:48:23 +0000 (11:48 -0400)
api/api.proto
api/handler.go
client/main.go

index c785356..1d5ecaa 100644 (file)
@@ -61,7 +61,6 @@ message CNIcapArgs {
   }
 
   repeated PORTMAPPINGS portMappings = 1;
-  //PORTMAPPINGS portMappings = 1;
 
   message FOOMAP {
     string thing1 = 1;
@@ -69,7 +68,6 @@ message CNIcapArgs {
   }
 
   repeated FOOMAP fooMap = 2;
-  //FOOMAP fooMap = 2;
 }
 
 message ConfPath {
index 1792af6..c0e0834 100644 (file)
@@ -18,9 +18,6 @@ import (
 const (
        EnvCNIPath        = "CNI_PATH"
        EnvNetDir         = "NETCONFPATH"
-       //EnvCapabilityArgs = "CAP_ARGS"
-       //EnvCNIArgs        = "CNI_ARGS"
-       //EnvCNIIfname      = "CNI_IFNAME"
 
        DefaultNetDir = "/etc/cni/net.d"
 
@@ -183,21 +180,17 @@ func parseArgs(args string) ([][2]string, error) {
 
 func cniCommon(conf string, netns string, ifName string, args string, capabilityArgsValue string) (*libcni.NetworkConfigList, *libcni.RuntimeConf, *libcni.CNIConfig, error) {
 
+       var err error
+
        log.Printf("cniCommon Called")
 
-       // TODO
-       //netdir := os.Getenv(EnvNetDir)
-       netdir := "/home/mcambria/go/src/github.com/containernetworking"
-       if netdir == "" {
-               netdir = DefaultNetDir
-       }
-       netconf, err := libcni.LoadConfList(netdir, conf)
-       if err != nil {
+       netconf := libcni.NetworkConfigList{}
+       if err = json.Unmarshal([]byte(conf), &netconf); err != nil {
                return nil, nil, nil, err
        }
 
        capabilityArgs := CNIcapArgs{}
-       if err = json.Unmarshal([]byte(capabilityArgsValue), &capabilityArgs); err != nil {
+       if err := json.Unmarshal([]byte(capabilityArgsValue), &capabilityArgs); err != nil {
        //if err := proto.Unmarshal([]byte(capabilityArgsValue), &capabilityArgs); err != nil {
                return nil, nil, nil, err
        }
@@ -229,7 +222,7 @@ func cniCommon(conf string, netns string, ifName string, args string, capability
        s := sha512.Sum512([]byte(netns))
        containerID := fmt.Sprintf("cnitool-%x", s[:10])
 
-       // TODO
+       // TODO - remove hard coded cniBinPath 
        //cninet := libcni.NewCNIConfig(filepath.SplitList(os.Getenv(EnvCNIPath)), nil)
        cniBinPath := "/home/mcambria/go/src/github.com/mccv1r0/plugins/bin"
        cninet := libcni.NewCNIConfig(filepath.SplitList(cniBinPath), nil)
@@ -253,7 +246,7 @@ _, _ = f.Write([]byte(sout))
 sout = fmt.Sprintf("mcc: rt: %v of type %T \n", rt, rt)
 _, _ = f.Write([]byte(sout))
 
-       return netconf, rt, cninet, nil
+       return &netconf, rt, cninet, nil
 }
 
 /*
index a7dd6fb..cdb1ca6 100644 (file)
@@ -4,7 +4,7 @@ package main
 import (
        "log"
        "context"
-       //"encoding/json"
+       "encoding/json"
        "fmt"
        "os"
        "path/filepath"
@@ -13,6 +13,8 @@ import (
        //proto "github.com/golang/protobuf/proto"
        "google.golang.org/grpc"
        "google.golang.org/grpc/credentials"
+
+       "github.com/containernetworking/cni/libcni"
 )
 
 const (
@@ -190,6 +192,24 @@ func main() {
           os.Exit(1)
        }
 
+       // TODO - remove hard coded path and use EnvNetDir
+       //netdir := os.Getenv(EnvNetDir)
+       netdir := "/home/mcambria/go/src/github.com/containernetworking"
+       if netdir == "" {
+               netdir = DefaultNetDir
+       }
+       netconf, err := libcni.LoadConfList(netdir, os.Args[2])
+       if err != nil {
+          fmt.Fprintf(os.Stderr, "  failed to find config %s\n", os.Args[2])
+          return
+       }
+
+       confBytes, err := json.Marshal(&netconf)
+       if err != nil {
+          fmt.Fprintf(os.Stderr, "  failed to marshal config bytes\n")
+          return
+       }
+
        var capArgs string
        //var err error
        //capabilityArgs := api.CNIcapArgs{}
@@ -226,13 +246,13 @@ func main() {
 
        switch os.Args[1] {
        case CmdAdd:
-            gRPCsendAddTcp(os.Args[2], netns, ifName, args, capArgs)
+            gRPCsendAddTcp(string(confBytes), netns, ifName, args, capArgs)
             os.Exit(0)
        case CmdCheck:
-            gRPCsendCheckTcp(os.Args[2], netns, ifName, args, capArgs)
+            gRPCsendCheckTcp(string(confBytes), netns, ifName, args, capArgs)
             os.Exit(0)
        case CmdDel:
-            gRPCsendDelTcp(os.Args[2], netns, ifName, args, capArgs)
+            gRPCsendDelTcp(string(confBytes), netns, ifName, args, capArgs)
             os.Exit(0)
        }
 }
@@ -264,7 +284,7 @@ func gRPCsendAddTcp(conf string, netns string, ifName string, args string, capAr
        cni := api.NewCNIserverClient(conn)
 
        cniAddMsg := api.CNIaddMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,
@@ -307,7 +327,7 @@ func gRPCsendCheckTcp(conf string, netns string, ifName string, args string, cap
        cni := api.NewCNIserverClient(conn)
 
        cniCheckMsg := api.CNIcheckMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,
@@ -350,7 +370,7 @@ func gRPCsendDelTcp(conf string, netns string, ifName string, args string, capAr
        cni := api.NewCNIserverClient(conn)
 
        cniDelMsg := api.CNIdelMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,
@@ -394,7 +414,7 @@ func gRPCsendAddUnix(conf string, netns string, ifName string, args string, capA
        cni := api.NewCNIserverClient(conn)
 
        cniAddMsg := api.CNIaddMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,
@@ -438,7 +458,7 @@ func gRPCsendCheckUnix(conf string, netns string, ifName string, args string, ca
        cni := api.NewCNIserverClient(conn)
 
        cniCheckMsg := api.CNIcheckMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,
@@ -482,7 +502,7 @@ func gRPCsendDelUnix(conf string, netns string, ifName string, args string, capA
        cni := api.NewCNIserverClient(conn)
 
        cniDelMsg := api.CNIdelMsg{
-              Conf: os.Args[2],
+              Conf: conf,
               NetNS: netns,
               IfName: ifName,
               CniArgs: args,