Moved connection setup to its own, common function
authorMichael Cambria <mcambria@redhat.com>
Tue, 23 Apr 2019 19:27:12 +0000 (15:27 -0400)
committerMichael Cambria <mcambria@redhat.com>
Tue, 23 Apr 2019 19:27:12 +0000 (15:27 -0400)
api/handler.go
client/main.go

index e328f22..fffc75c 100644 (file)
@@ -6,7 +6,6 @@ import (
        "crypto/sha512"
        "encoding/json"
        "fmt"
-       "os"
        "path/filepath"
        "strings"
 
@@ -21,10 +20,6 @@ const (
        EnvNetDir         = "NETCONFPATH"
 
        DefaultNetDir = "/etc/cni/net.d"
-
-       CmdAdd   = "add"
-       CmdCheck = "check"
-       CmdDel   = "del"
 )
 
 // Server represents the gRPC server
@@ -218,16 +213,5 @@ func cniCommon(conf string, netns string, ifName string, args string, capability
                //CapabilityArgs: portMappings,
        }
 
-var f *os.File
-var sout string
-f, _ = os.OpenFile("/tmp/check.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
-
-sout = fmt.Sprintf("mcc: cninet: %v of type %T \n", cninet, cninet)
-_, _ = f.Write([]byte(sout))
-sout = fmt.Sprintf("mcc: netconf: %v of type %T \n", netconf, netconf)
-_, _ = 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
 }
index 6822a74..936127a 100644 (file)
@@ -115,113 +115,34 @@ func main() {
           os.Exit(1)
        }
 
+       conn, err := gRPCtcp()
+       //conn, err := gRPCunix()
+       if err != nil {
+               fmt.Fprintf(os.Stderr, "  failed to connect to server: %v\n", err)
+               return 
+       }
+
        switch os.Args[1] {
        case CmdAdd:
-            gRPCsendAddTcp(string(confBytes), netns, ifName, args, capabilityArgs)
-            os.Exit(0)
+            gRPCsendAdd(conn, string(confBytes), netns, ifName, args, capabilityArgs)
        case CmdCheck:
-            gRPCsendCheckTcp(string(confBytes), netns, ifName, args, capabilityArgs)
-            os.Exit(0)
+            gRPCsendCheck(conn, string(confBytes), netns, ifName, args, capabilityArgs)
        case CmdDel:
-            gRPCsendDelTcp(string(confBytes), netns, ifName, args, capabilityArgs)
-            os.Exit(0)
+            gRPCsendDel(conn, string(confBytes), netns, ifName, args, capabilityArgs)
        }
-}
-
-func gRPCsendAddTcp(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
-       var conn *grpc.ClientConn
 
-       // Create the client TLS credentials
-       creds, err := credentials.NewClientTLSFromFile("cert/server.crt", "")
-       if err != nil {
-               log.Fatalf("could not load tls cert: %s", err)
-               return err
-       }
-
-       // Setup the login/pass
-       auth := Authentication{
-               Login:    "john",
-               Password: "doe",
-       }
-
-       // Initiate a connection with the server
-       conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
-       if err != nil {
-               log.Fatalf("did not connect: %s", err)
-               return err
-       }
        defer conn.Close()
-
-       cni := api.NewCNIserverClient(conn)
-
-       cniAddMsg := api.CNIaddMsg{
-              Conf: conf,
-              NetNS: netns,
-              IfName: ifName,
-              CniArgs: args,
-              CapArgs: &capArgs,
-              }
-       resultAdd, err := cni.CNIadd(context.Background(), &cniAddMsg)
-       if err != nil {
-               log.Fatalf("error when calling CNIadd: %s", err)
-               return err
-       }
-       log.Printf("Response from TCP server: %s", resultAdd.StdOut)
-
-       return nil
-}
-
-func gRPCsendCheckTcp(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
-       var conn *grpc.ClientConn
-
-       // Create the client TLS credentials
-       creds, err := credentials.NewClientTLSFromFile("cert/server.crt", "")
-       if err != nil {
-               log.Fatalf("could not load tls cert: %s", err)
-               return err
-       }
-
-       // Setup the login/pass
-       auth := Authentication{
-               Login:    "john",
-               Password: "doe",
-       }
-
-       // Initiate a connection with the server
-       conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
-       if err != nil {
-               log.Fatalf("did not connect: %s", err)
-               return err
-       }
-       defer conn.Close()
-
-       cni := api.NewCNIserverClient(conn)
-
-       cniCheckMsg := api.CNIcheckMsg{
-              Conf: conf,
-              NetNS: netns,
-              IfName: ifName,
-              CniArgs: args,
-              CapArgs: &capArgs,
-              }
-       resultCheck, err := cni.CNIcheck(context.Background(), &cniCheckMsg)
-       if err != nil {
-               log.Fatalf("error when calling CNIcheck: %s", err)
-               return err
-       }
-       log.Printf("Response from TCP server: %s", resultCheck.Error)
-
-       return nil
+       os.Exit(0)
 }
 
-func gRPCsendDelTcp(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
+func gRPCtcp() (*grpc.ClientConn, error) {
        var conn *grpc.ClientConn
 
        // Create the client TLS credentials
        creds, err := credentials.NewClientTLSFromFile("cert/server.crt", "")
        if err != nil {
                log.Fatalf("could not load tls cert: %s", err)
-               return err
+               return nil, err
        }
 
        // Setup the login/pass
@@ -234,30 +155,13 @@ func gRPCsendDelTcp(conf string, netns string, ifName string, args string, capAr
        conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
        if err != nil {
                log.Fatalf("did not connect: %s", err)
-               return err
-       }
-       defer conn.Close()
-
-       cni := api.NewCNIserverClient(conn)
-
-       cniDelMsg := api.CNIdelMsg{
-              Conf: conf,
-              NetNS: netns,
-              IfName: ifName,
-              CniArgs: args,
-              CapArgs: &capArgs,
-              }
-       resultDel, err := cni.CNIdel(context.Background(), &cniDelMsg)
-       if err != nil {
-               log.Fatalf("error when calling CNIdel: %s", err)
-               return err
+               return nil, err
        }
-       log.Printf("Response from TCP server: %s", resultDel.Error)
 
-       return nil
+       return conn, nil
 }
 
-func gRPCsendAddUnix(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
+func gRPCunix() (*grpc.ClientConn, error) {
 
        var conn *grpc.ClientConn
 
@@ -265,7 +169,7 @@ func gRPCsendAddUnix(conf string, netns string, ifName string, args string, capA
        creds, err := credentials.NewClientTLSFromFile("cert/unixServer.crt", "")
        if err != nil {
                log.Fatalf("could not load tls cert: %s", err)
-               return err
+               return nil, err
        }
 
        // Setup the login/pass
@@ -278,9 +182,13 @@ func gRPCsendAddUnix(conf string, netns string, ifName string, args string, capA
        conn, err = grpc.Dial("unix:///tmp/grpc.sock", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
        if err != nil {
                log.Fatalf("did not connect: %s", err)
-               return err
+               return nil, err
        }
-       defer conn.Close()
+
+       return conn, nil
+}
+
+func gRPCsendAdd(conn *grpc.ClientConn, conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
 
        cni := api.NewCNIserverClient(conn)
 
@@ -296,35 +204,12 @@ func gRPCsendAddUnix(conf string, netns string, ifName string, args string, capA
                log.Fatalf("error when calling CNIadd: %s", err)
                return err
        }
-       log.Printf("Response from Unix server: %s", resultAdd.StdOut)
+       log.Printf("Response from TCP server: %s", resultAdd.StdOut)
 
        return nil
 }
 
-func gRPCsendCheckUnix(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
-
-       var conn *grpc.ClientConn
-
-       // Create the unix socket client TLS credentials
-       creds, err := credentials.NewClientTLSFromFile("cert/unixServer.crt", "")
-       if err != nil {
-               log.Fatalf("could not load tls cert: %s", err)
-               return err
-       }
-
-       // Setup the login/pass
-       auth := Authentication{
-               Login:    "john",
-               Password: "doe",
-       }
-
-       // Initiate a connection with the server
-       conn, err = grpc.Dial("unix:///tmp/grpc.sock", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
-       if err != nil {
-               log.Fatalf("did not connect: %s", err)
-               return err
-       }
-       defer conn.Close()
+func gRPCsendCheck(conn *grpc.ClientConn, conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
 
        cni := api.NewCNIserverClient(conn)
 
@@ -340,35 +225,12 @@ func gRPCsendCheckUnix(conf string, netns string, ifName string, args string, ca
                log.Fatalf("error when calling CNIcheck: %s", err)
                return err
        }
-       log.Printf("Response from Unix server: %s", resultCheck.Error)
+       log.Printf("Response from TCP server: %s", resultCheck.Error)
 
        return nil
 }
 
-func gRPCsendDelUnix(conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
-
-       var conn *grpc.ClientConn
-
-       // Create the unix socket client TLS credentials
-       creds, err := credentials.NewClientTLSFromFile("cert/unixServer.crt", "")
-       if err != nil {
-               log.Fatalf("could not load tls cert: %s", err)
-               return err
-       }
-
-       // Setup the login/pass
-       auth := Authentication{
-               Login:    "john",
-               Password: "doe",
-       }
-
-       // Initiate a connection with the server
-       conn, err = grpc.Dial("unix:///tmp/grpc.sock", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
-       if err != nil {
-               log.Fatalf("did not connect: %s", err)
-               return err
-       }
-       defer conn.Close()
+func gRPCsendDel(conn *grpc.ClientConn, conf string, netns string, ifName string, args string, capArgs api.CNIcapArgs) error {
 
        cni := api.NewCNIserverClient(conn)
 
@@ -384,7 +246,7 @@ func gRPCsendDelUnix(conf string, netns string, ifName string, args string, capA
                log.Fatalf("error when calling CNIdel: %s", err)
                return err
        }
-       log.Printf("Response from Unix server: %s", resultDel.Error)
+       log.Printf("Response from TCP server: %s", resultDel.Error)
 
        return nil
 }