This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (77 loc) · 2.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"code.cloudfoundry.org/goshims/execshim"
"code.cloudfoundry.org/goshims/osshim"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/smb-csi-driver/identityserver"
"code.cloudfoundry.org/smb-csi-driver/nodeserver"
"flag"
"fmt"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
"os"
"strings"
)
type unaryInterceptor struct {
logger lager.Logger
}
func main() {
var endpoint = flag.String("endpoint", "", "")
var nodeId = flag.String("nodeid", "", "")
flag.Parse()
logger := lager.NewLogger("smb-csi-driver")
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
interceptor := unaryInterceptor{logger: logger}
logger.Info(fmt.Sprintf("node-id: %s", *nodeId))
proto, addr, err := ParseEndpoint(*endpoint)
if err != nil {
log.Fatal(err.Error())
}
if proto == "unix" {
addr = "/" + addr
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
log.Fatalf("Failed to remove %s, error: %s", addr, err.Error())
}
}
logger.Info(">>>>>")
logger.Info(proto)
logger.Info(addr)
logger.Info("<<<<<")
lis, err := net.Listen(proto, addr)
if err != nil {
logger.Fatal("failed to listen", err)
}
opts := []grpc.ServerOption{
grpc.UnaryInterceptor(interceptor.logGRPC),
}
grpcServer := grpc.NewServer(opts...)
csi.RegisterIdentityServer(grpcServer, identityserver.NewSmbIdentityServer())
csi.RegisterNodeServer(grpcServer, nodeserver.NewNodeServer(logger, &execshim.ExecShim{}, &osshim.OsShim{}, nodeserver.NewStore()))
err = grpcServer.Serve(lis)
if err != nil {
logger.Fatal("failed to serve", err, lager.Data{"listener": lis})
}
}
func ParseEndpoint(ep string) (string, string, error) {
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
s := strings.SplitN(ep, "://", 2)
if s[1] != "" {
return s[0], s[1], nil
}
}
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
}
func (l unaryInterceptor) logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
l.logger.Info("GRPC request", lager.Data{"method": info.FullMethod, "req": protosanitizer.StripSecrets(req).String()})
resp, err := handler(ctx, req)
if err != nil {
l.logger.Error("GRPC error", err)
} else {
l.logger.Info("GRPC response", lager.Data{"response": protosanitizer.StripSecrets(resp).String()})
}
return resp, err
}