Use unix domain socket by default

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
Mrunal Patel 2016-07-22 16:44:27 -04:00
parent d8ae7178e2
commit 0766dfecfe
2 changed files with 27 additions and 5 deletions

View file

@ -3,7 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"net"
"os" "os"
"time"
pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -12,9 +14,22 @@ import (
) )
const ( const (
address = "localhost:49999" unixDomainSocket = "/var/run/ocid.sock"
// TODO: Make configurable
timeout = 10 * time.Second
) )
func getClientConnection() (*grpc.ClientConn, error) {
conn, err := grpc.Dial(unixDomainSocket, grpc.WithInsecure(), grpc.WithTimeout(timeout),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}))
if err != nil {
return nil, fmt.Errorf("Failed to connect: %v", err)
}
return conn, nil
}
// Version sends a VersionRequest to the server, and parses the returned VersionResponse. // Version sends a VersionRequest to the server, and parses the returned VersionResponse.
func Version(client pb.RuntimeServiceClient, version string) error { func Version(client pb.RuntimeServiceClient, version string) error {
r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version}) r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version})
@ -54,7 +69,7 @@ var pullImageCommand = cli.Command{
Usage: "pull an image", Usage: "pull an image",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
// Set up a connection to the server. // Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure()) conn, err := getClientConnection()
if err != nil { if err != nil {
return fmt.Errorf("Failed to connect: %v", err) return fmt.Errorf("Failed to connect: %v", err)
} }
@ -74,7 +89,7 @@ var runtimeVersionCommand = cli.Command{
Usage: "get runtime version information", Usage: "get runtime version information",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
// Set up a connection to the server. // Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure()) conn, err := getClientConnection()
if err != nil { if err != nil {
return fmt.Errorf("Failed to connect: %v", err) return fmt.Errorf("Failed to connect: %v", err)
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"log" "log"
"net" "net"
"os"
"github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime" "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
"github.com/mrunalp/ocid/server" "github.com/mrunalp/ocid/server"
@ -10,11 +11,17 @@ import (
) )
const ( const (
port = ":49999" unixDomainSocket = "/var/run/ocid.sock"
) )
func main() { func main() {
lis, err := net.Listen("tcp", port) // Remove the socket if it already exists
if _, err := os.Stat(unixDomainSocket); err == nil {
if err := os.Remove(unixDomainSocket); err != nil {
log.Fatal(err)
}
}
lis, err := net.Listen("unix", unixDomainSocket)
if err != nil { if err != nil {
log.Fatalf("failed to listen: %v", err) log.Fatalf("failed to listen: %v", err)
} }