aboutsummaryrefslogtreecommitdiff
path: root/pkg/driver/driver.go
blob: 84052ef76f27c2f699a241f6d5cdbf6cb2c64ea9 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package driver

import (
	"fmt"
	"os"
	"os/signal"
	"path/filepath"
	"syscall"

	"github.com/container-storage-interface/spec/lib/go/csi"
	"github.com/seaweedfs/seaweedfs-csi-driver/pkg/datalocality"
	"github.com/seaweedfs/seaweedfs-csi-driver/pkg/mountmanager"
	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/pb"
	"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
	"github.com/seaweedfs/seaweedfs/weed/security"
	"github.com/seaweedfs/seaweedfs/weed/util"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

var (
	version = "1.0.0"
)

type SeaweedFsDriver struct {
	name    string
	nodeID  string
	version string

	endpoint        string
	mountEndpoint   string
	volumeSocketDir string // directory for volume sockets, derived from mountEndpoint

	vcap  []*csi.VolumeCapability_AccessMode
	cscap []*csi.ControllerServiceCapability

	filers            []pb.ServerAddress
	filerIndex        int
	grpcDialOption    grpc.DialOption
	ConcurrentWriters int
	CacheCapacityMB   int
	CacheDir          string
	UidMap            string
	GidMap            string
	signature         int32
	DataCenter        string
	DataLocality      datalocality.DataLocality

	RunNode       bool
	RunController bool
}

func NewSeaweedFsDriver(name, filer, nodeID, endpoint, mountEndpoint string, enableAttacher bool) *SeaweedFsDriver {

	glog.Infof("Driver: %v version: %v", name, version)

	util.LoadConfiguration("security", false)

	// Derive volumeSocketDir from mountEndpoint
	volumeSocketDir := mountmanager.DefaultSocketDir
	if mountEndpoint != "" {
		_, address, err := mountmanager.ParseEndpoint(mountEndpoint)
		if err != nil {
			glog.Warningf("invalid mount endpoint %q, using default socket directory %q: %v", mountEndpoint, volumeSocketDir, err)
		} else if address != "" {
			volumeSocketDir = filepath.Dir(address)
		}
	}

	n := &SeaweedFsDriver{
		endpoint:        endpoint,
		mountEndpoint:   mountEndpoint,
		volumeSocketDir: volumeSocketDir,
		nodeID:          nodeID,
		name:            name,
		version:         version,
		filers:          pb.ServerAddresses(filer).ToAddresses(),
		grpcDialOption:  security.LoadClientTLS(util.GetViper(), "grpc.client"),
		signature:       util.RandomInt32(),
	}

	n.AddVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{
		csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
		csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
		csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER,
		csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER,
	})
	n.AddControllerServiceCapabilities([]csi.ControllerServiceCapability_RPC_Type{
		csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
		csi.ControllerServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER,
		csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
	})

	// we need this just only for csi-attach, but we do nothing for attach/detach
	if enableAttacher {
		n.AddControllerServiceCapabilities([]csi.ControllerServiceCapability_RPC_Type{
			csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
		})
	}

	return n
}

func (n *SeaweedFsDriver) Run() {
	glog.Info("starting")

	var controller *ControllerServer
	if n.RunController {
		controller = NewControllerServer(n)
	}

	var node *NodeServer
	if n.RunNode {
		node = NewNodeServer(n)
	}

	s := NewNonBlockingGRPCServer()
	s.Start(n.endpoint,
		NewIdentityServer(n),
		controller,
		node)

	stopChan := make(chan os.Signal, 1)
	signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
	<-stopChan

	glog.Infof("stopping")

	s.Stop()
	s.Wait()

	if node != nil {
		glog.Infof("node cleanup")
		node.NodeCleanup()
	}

	glog.Infof("stopped")
}

func (n *SeaweedFsDriver) AddVolumeCapabilityAccessModes(vc []csi.VolumeCapability_AccessMode_Mode) {
	for _, c := range vc {
		glog.Infof("Enabling volume access mode: %v", c.String())
		n.vcap = append(n.vcap, &csi.VolumeCapability_AccessMode{Mode: c})
	}
}

func (n *SeaweedFsDriver) AddControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) {
	for _, c := range cl {
		glog.Infof("Enabling controller service capability: %v", c.String())
		n.cscap = append(n.cscap, NewControllerServiceCapability(c))
	}
}

func (d *SeaweedFsDriver) ValidateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
	if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
		return nil
	}

	for _, cap := range d.cscap {
		if c == cap.GetRpc().GetType() {
			return nil
		}
	}
	return status.Error(codes.InvalidArgument, fmt.Sprintf("%s", c))
}

var _ = filer_pb.FilerClient(&SeaweedFsDriver{})

func (d *SeaweedFsDriver) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {

	return util.Retry("filer grpc", func() error {

		i := d.filerIndex
		n := len(d.filers)
		var err error
		for x := 0; x < n; x++ {

			err = pb.WithGrpcClient(streamingMode, d.signature, func(grpcConnection *grpc.ClientConn) error {
				client := filer_pb.NewSeaweedFilerClient(grpcConnection)
				return fn(client)
			}, d.filers[i].ToGrpcAddress(), false, d.grpcDialOption)

			if err != nil {
				glog.V(0).Infof("WithFilerClient %d %v: %v", x, d.filers[i], err)
			} else {
				d.filerIndex = i
				return nil
			}

			i++
			if i >= n {
				i = 0
			}

		}
		return err
	})

}
func (d *SeaweedFsDriver) AdjustedUrl(location *filer_pb.Location) string {
	return location.Url
}
func (d *SeaweedFsDriver) GetDataCenter() string {
	return d.DataCenter
}