forked from thecodeteam/csi-vsphere
/
service.go
100 lines (83 loc) · 2.12 KB
/
service.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
93
94
95
96
97
98
99
100
package service
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"sync"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/thecodeteam/gocsi"
"github.com/thecodeteam/gocsi/csp"
"github.com/thecodeteam/gofsutil"
"github.com/thecodeteam/csi-vsphere/pkg/vmware/vmdkops"
)
const (
// Name is the name of this CSI SP.
Name = "com.thecodeteam.vsphere"
// VendorVersion is the version of this CSP SP.
VendorVersion = "0.1.0"
// SupportedVersions is a list of the CSI versions this SP supports.
SupportedVersions = "0.0.0, 0.1.0"
)
// Service is a CSI SP and gocsi.IdempotencyProvider.
type Service interface {
csi.ControllerServer
csi.IdentityServer
csi.NodeServer
gocsi.IdempotencyProvider
Interceptors() []grpc.UnaryServerInterceptor
BeforeServe(context.Context, *csp.StoragePlugin, net.Listener) error
}
type service struct {
ops vmdkops.VmdkOps
defaultDatastore string
privMntDir string
}
// New returns a new Service.
func New() Service {
return &service{}
}
func (s *service) BeforeServe(
ctx context.Context,
sp *csp.StoragePlugin,
lis net.Listener) error {
vmdkops.EsxPort = 1019
if v, ok := gocsi.LookupEnv(ctx, EnvVarPort); ok {
i, err := strconv.Atoi(v)
if err != nil {
return err
}
vmdkops.EsxPort = i
}
if v, ok := gocsi.LookupEnv(ctx, EnvVarDatastore); ok {
s.defaultDatastore = v
}
s.ops = vmdkops.VmdkOps{Cmd: vmdkops.EsxVmdkCmd{Mtx: &sync.Mutex{}}}
privMntDir, _ := gocsi.LookupEnv(ctx, csp.EnvVarPrivateMountDir)
if privMntDir == "" {
var err error
if privMntDir, err = filepath.Abs(".csi-vsphere"); err != nil {
return err
}
}
if !filepath.IsAbs(privMntDir) {
return fmt.Errorf(
"private mount dir must be absolute: %s", privMntDir)
}
if err := os.MkdirAll(privMntDir, 0755); err != nil {
return err
}
if err := gofsutil.EvalSymlinks(ctx, &privMntDir); err != nil {
return err
}
s.privMntDir = privMntDir
log.WithFields(log.Fields{
"privateMountDir": s.privMntDir,
"esxPort": vmdkops.EsxPort,
}).Infof("configured %s", Name)
return nil
}