-
Notifications
You must be signed in to change notification settings - Fork 236
/
nodeserver.go
520 lines (478 loc) · 18 KB
/
nodeserver.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oss
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
csicommon "github.com/kubernetes-csi/drivers/pkg/csi-common"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cloud/metadata"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cnfs/v1beta1"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
mountutils "k8s.io/mount-utils"
)
type nodeServer struct {
metadata metadata.MetadataProvider
*csicommon.DefaultNodeServer
nodeName string
clientset kubernetes.Interface
dynamicClient dynamic.Interface
sharedPathLock *utils.VolumeLocks
ossfsMounterFac *mounter.ContainerizedFuseMounterFactory
}
// Options contains options for target oss
type Options struct {
directAssigned bool
Bucket string `json:"bucket"`
URL string `json:"url"`
OtherOpts string `json:"otherOpts"`
AkID string `json:"akId"`
AkSecret string `json:"akSecret"`
Path string `json:"path"`
UseSharedPath bool `json:"useSharedPath"`
AuthType string `json:"authType"`
RoleName string `json:"roleName"`
RoleArn string `json:"roleArn"`
OidcProviderArn string `json:"oidcProviderArn"`
ServiceAccountName string `json:"serviceAccountName"`
SecretProviderClass string `json:"secretProviderClass"`
FuseType string `json:"fuseType"`
MetricsTop string `json:"metricsTop"`
ReadOnly bool `json:"readOnly"`
Encrypted string `json:"encrypted"`
KmsKeyId string `json:"kmsKeyId"`
}
const (
// OssfsCredentialFile is the path of oss ak credential file
OssfsCredentialFile = "/host/etc/passwd-ossfs"
// AkID is Ak ID
AkID = "akId"
// AkSecret is Ak Secret
AkSecret = "akSecret"
// OssFsType is the oss filesystem type
OssFsType = "ossfs"
// JindoFsType tag
JindoFsType = "jindofs"
// metricsPathPrefix
metricsPathPrefix = "/host/var/run/ossfs/"
// defaultMetricsTop
defaultMetricsTop = "10"
// fuseServieAccountName
fuseServieAccountName = "csi-fuse-ossfs"
)
const (
EncryptedTypeKms = "kms"
EncryptedTypeAes256 = "aes256"
)
func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{Capabilities: []*csi.NodeServiceCapability{
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
},
}}, nil
}
func validateNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) error {
valid, err := utils.CheckRequest(req.GetVolumeContext(), req.GetTargetPath())
if !valid {
return status.Errorf(codes.InvalidArgument, err.Error())
}
return nil
}
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
// logout oss paras
log.Infof("NodePublishVolume:: Starting Mount volume: %s mount with req: %+v", req.VolumeId, req)
mountPath := req.GetTargetPath()
if err := validateNodePublishVolumeRequest(req); err != nil {
return nil, err
}
var cnfsName string
opt := &Options{}
opt.UseSharedPath = true
opt.FuseType = OssFsType
for key, value := range req.VolumeContext {
key = strings.ToLower(key)
if key == "bucket" {
opt.Bucket = strings.TrimSpace(value)
} else if key == "url" {
opt.URL = strings.TrimSpace(value)
} else if key == "otheropts" {
opt.OtherOpts = strings.TrimSpace(value)
} else if key == "akid" {
opt.AkID = strings.TrimSpace(value)
} else if key == "aksecret" {
opt.AkSecret = strings.TrimSpace(value)
} else if key == "path" {
if v := strings.TrimSpace(value); v == "" {
opt.Path = "/"
} else {
opt.Path = v
}
} else if key == "usesharedpath" {
if useSharedPath, err := strconv.ParseBool(value); err == nil {
opt.UseSharedPath = useSharedPath
} else {
log.Warnf("invalid useSharedPath: %q", value)
}
} else if key == "authtype" {
opt.AuthType = strings.ToLower(strings.TrimSpace(value))
} else if key == "rolename" {
opt.RoleName = strings.TrimSpace(value)
} else if key == "rolearn" {
opt.RoleArn = strings.TrimSpace(value)
} else if key == "oidcproviderarn" {
opt.OidcProviderArn = strings.TrimSpace(value)
} else if key == "serviceaccountname" {
opt.ServiceAccountName = strings.TrimSpace(value)
} else if key == "secretproviderclass" {
opt.SecretProviderClass = strings.TrimSpace(value)
} else if key == "fusetype" {
opt.FuseType = strings.ToLower(strings.TrimSpace(value))
} else if key == "metricstop" {
opt.MetricsTop = strings.ToLower(strings.TrimSpace(value))
} else if key == "containernetworkfilesystem" {
cnfsName = value
} else if key == optDirectAssigned {
opt.directAssigned, _ = strconv.ParseBool(strings.TrimSpace(value))
} else if key == "encrypted" {
opt.Encrypted = strings.ToLower(strings.TrimSpace(value))
} else if key == "kmskeyid" {
opt.KmsKeyId = value
}
}
if req.Readonly {
opt.ReadOnly = true
} else {
switch req.VolumeCapability.AccessMode.GetMode() {
case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER, csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER:
opt.ReadOnly = false
default:
opt.ReadOnly = true
}
}
if len(opt.Bucket) == 0 {
cnfs, err := v1beta1.GetCnfsObject(ns.dynamicClient, cnfsName)
if err != nil {
return nil, err
}
if cnfs.Status.FsAttributes.EndPoint == nil {
return nil, errors.New("Cnfs " + cnfsName + " is not ready, endpoint is empty.")
}
opt.Bucket = cnfs.Status.FsAttributes.BucketName
opt.URL = cnfs.Status.FsAttributes.EndPoint.Internal
}
// Default oss path
if opt.Path == "" {
opt.Path = "/"
}
// support set ak by secret
if opt.AkID == "" || opt.AkSecret == "" {
if value, ok := req.Secrets[AkID]; ok {
opt.AkID = value
}
if value, ok := req.Secrets[AkSecret]; ok {
opt.AkSecret = value
}
}
// check parameters
if err := checkOssOptions(opt); err != nil {
log.Errorf("Check oss input error: %s", err.Error())
return nil, errors.New("Check oss input error: " + err.Error())
}
argStr := fmt.Sprintf("Bucket: %s, url: %s, , OtherOpts: %s, Path: %s, UseSharedPath: %s, authType: %s, encrypted: %s, kmsid: %s",
opt.Bucket, opt.URL, opt.OtherOpts, opt.Path, strconv.FormatBool(opt.UseSharedPath), opt.AuthType, opt.Encrypted, opt.KmsKeyId)
log.Infof("NodePublishVolume:: Starting Oss Mount: %s", argStr)
if opt.directAssigned {
return ns.publishDirectVolume(ctx, req, opt)
}
var ossMounter mountutils.Interface
switch opt.FuseType {
case JindoFsType:
ossMounter = mounter.NewConnectorMounter(mountutils.New(""), "/etc/jindofs-tool/jindo-fuse")
case OssFsType, "":
// When useSharedPath options is set to false,
// mount operations need to be atomic to ensure that no fuse pods are left behind in case of failure.
// Because kubelet will not call NodeUnpublishVolume when NodePublishVolume never succeeded.
var rrsaCfg *mounter.RrsaConfig
var err error
if opt.AuthType == mounter.AuthTypeRRSA {
rrsaCfg, err = getRRSAConfig(opt, ns.metadata)
if err != nil {
return nil, errors.New("Get RoleArn and OidcProviderArn for RRSA error: " + err.Error())
}
}
authCfg := &mounter.AuthConfig{AuthType: opt.AuthType, RrsaConfig: rrsaCfg, SecretProviderClassName: opt.SecretProviderClass}
ossMounter = ns.ossfsMounterFac.NewFuseMounter(ctx, req.VolumeId, authCfg, !opt.UseSharedPath)
default:
return nil, status.Errorf(codes.InvalidArgument, "unknown fuseType: %q", opt.FuseType)
}
notMnt, err := mountutils.IsNotMountPoint(ossMounter, mountPath)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(mountPath, os.ModePerm); err != nil {
log.Errorf("NodePublishVolume: mkdir %s: %v", mountPath, err)
return nil, status.Error(codes.Internal, err.Error())
}
notMnt = true
} else {
log.Errorf("NodePublishVolume: check mountpoint %s: %v", mountPath, err)
return nil, status.Error(codes.Internal, err.Error())
}
}
if !notMnt {
log.Infof("NodePublishVolume: %s already mounted", mountPath)
return &csi.NodePublishVolumeResponse{}, nil
}
var mountOptions []string
if req.VolumeCapability != nil && req.VolumeCapability.GetMount() != nil {
mountOptions = req.VolumeCapability.GetMount().MountFlags
}
regionID, _ := ns.metadata.Get(metadata.RegionID)
switch opt.AuthType {
case mounter.AuthTypeSTS:
if opt.FuseType == OssFsType {
mountOptions = append(mountOptions, GetRAMRoleOption())
} else if opt.FuseType == JindoFsType {
mountOptions = append(mountOptions, "fs.oss.provider.endpoint=ECS_ROLE")
}
case mounter.AuthTypeRRSA:
if opt.FuseType == OssFsType {
if regionID == "" {
mountOptions = append(mountOptions, "rrsa_endpoint=https://sts.aliyuncs.com")
} else {
mountOptions = append(mountOptions, fmt.Sprintf("rrsa_endpoint=https://sts-vpc.%s.aliyuncs.com", regionID))
}
} else if opt.FuseType == JindoFsType {
return nil, status.Errorf(codes.Internal, "jindo fs do not support RRSA")
}
case mounter.AuthTypeCSS:
if opt.FuseType == JindoFsType {
return nil, status.Errorf(codes.Internal, "jindo fs do not support CsiSecretStore")
}
default:
if opt.FuseType == OssFsType {
// ossfs fuse pod will mount the secret to access credentials
err := mounter.SetupOssfsCredentialSecret(ctx, ns.clientset, ns.nodeName, req.VolumeId, opt.Bucket, opt.AkID, opt.AkSecret)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to setup ossfs credential secret: %v", err)
}
} else if opt.FuseType == JindoFsType {
mountOptions = append(mountOptions, fmt.Sprintf("fs.oss.accessKeyId=%s,fs.oss.accessKeySecret=%s", opt.AkID, opt.AkSecret))
}
}
switch opt.Encrypted {
case EncryptedTypeAes256:
if opt.FuseType == OssFsType {
mountOptions = append(mountOptions, "use_sse")
}
case EncryptedTypeKms:
if opt.FuseType == OssFsType {
if opt.KmsKeyId == "" {
mountOptions = append(mountOptions, "use_sse=kmsid")
} else {
mountOptions = append(mountOptions, fmt.Sprintf("use_sse=kmsid:%s", opt.KmsKeyId))
}
}
}
if opt.ReadOnly {
mountOptions = append(mountOptions, "ro")
}
if regionID == "" {
log.Warnf("NodePublishVolume:: failed to get region id from both env and metadata, use original URL: %s", opt.URL)
} else if url, done := setNetworkType(opt.URL, regionID); done {
log.Warnf("NodePublishVolume:: setNetworkType: modified URL from %s to %s", opt.URL, url)
opt.URL = url
}
if url, done := setTransmissionProtocol(opt.URL); done {
log.Warnf("NodePublishVolume:: setTransmissionProtocol: modified URL from %s to %s", opt.URL, url)
opt.URL = url
}
if opt.UseSharedPath {
sharedPath := GetGlobalMountPath(req.GetVolumeId())
notMnt, err := ossMounter.IsLikelyNotMountPoint(sharedPath)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(sharedPath, os.ModePerm); err != nil {
log.Errorf("NodePublishVolume: mkdir %s: %v", sharedPath, err)
return nil, status.Error(codes.Internal, err.Error())
}
notMnt = true
} else if mountutils.IsCorruptedMnt(err) {
log.Warnf("Umount corrupted mountpoint %s", sharedPath)
err := mountutils.New("").Unmount(sharedPath)
if err != nil {
return nil, status.Errorf(codes.Internal, "umount corrupted mountpoint %s: %v", sharedPath, err)
}
notMnt = true
} else {
log.Errorf("NodePublishVolume: check mountpoint %s: %v", sharedPath, err)
return nil, status.Error(codes.Internal, err.Error())
}
}
if notMnt {
// serialize node publish operations on the same volume when using sharedpath
if lock := ns.sharedPathLock.TryAcquire(req.VolumeId); !lock {
log.Errorf("NodePublishVolume: aborted because failed to acquire volume %s lock", req.VolumeId)
return nil, status.Errorf(codes.Aborted, "NodePublishVolume operation on shared path of volume %s already exists", req.VolumeId)
}
defer ns.sharedPathLock.Release(req.VolumeId)
utils.WriteSharedMetricsInfo(metricsPathPrefix, req, OssFsType, "oss", opt.Bucket, sharedPath)
if opt.MetricsTop != "" {
mountOptions = append(mountOptions, fmt.Sprintf("metrics_top=%s", opt.MetricsTop))
}
if err := doMount(ossMounter, sharedPath, *opt, mountOptions); err != nil {
log.Errorf("NodePublishVolume: failed to mount")
return nil, err
}
} else {
log.Infof("NodePublishVolume: sharedpath %s already mounted", sharedPath)
}
log.Infof("NodePublishVolume:: Start mount operation from source [%s] to dest [%s]", sharedPath, mountPath)
if err := ossMounter.Mount(sharedPath, mountPath, "", []string{"bind"}); err != nil {
log.Errorf("Ossfs mount error: %v", err.Error())
return nil, errors.New("Create oss volume fail: " + err.Error())
}
} else {
if opt.FuseType == OssFsType {
metricsTop := defaultMetricsTop
if opt.MetricsTop != "" {
metricsTop = opt.MetricsTop
}
utils.WriteMetricsInfo(metricsPathPrefix, req, metricsTop, OssFsType, "oss", opt.Bucket)
}
if err := doMount(ossMounter, mountPath, *opt, mountOptions); err != nil {
return nil, err
}
}
log.Infof("NodePublishVolume: mounted oss successfully, volume %s, targetPath: %s", req.VolumeId, mountPath)
return &csi.NodePublishVolumeResponse{}, nil
}
// Check oss options
func checkOssOptions(opt *Options) error {
if opt.URL == "" || opt.Bucket == "" {
return errors.New("Oss parameters error: Url/Bucket empty")
}
if !strings.HasPrefix(opt.Path, "/") {
return errors.New("Oss path error: start with " + opt.Path + ", should start with /")
}
if opt.FuseType == JindoFsType {
return nil
}
switch opt.AuthType {
case mounter.AuthTypeSTS:
case mounter.AuthTypeRRSA:
if err := checkRRSAParams(opt); err != nil {
return err
}
case mounter.AuthTypeCSS:
if opt.SecretProviderClass == "" {
return errors.New("Oss parameters error: use CsiSecretStore but secretProviderClass is empty")
}
default:
// if not input ak from user, use the default ak value
if opt.AkID == "" || opt.AkSecret == "" {
ac := utils.GetEnvAK()
opt.AkID = ac.AccessKeyID
opt.AkSecret = ac.AccessKeySecret
}
if opt.AkID == "" || opt.AkSecret == "" {
return errors.New("Oss parameters error: AK and authType are both empty or invalid")
}
}
if opt.Encrypted != "" && opt.Encrypted != EncryptedTypeKms && opt.Encrypted != EncryptedTypeAes256 {
return errors.New("Oss encrypted error: invalid SSE encryted type")
}
return nil
}
func validateNodeUnpublishVolumeRequest(req *csi.NodeUnpublishVolumeRequest) error {
valid, err := utils.ValidatePath(req.GetTargetPath())
if !valid {
return status.Errorf(codes.InvalidArgument, err.Error())
}
return nil
}
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
log.Infof("NodeUnpublishVolume: Starting Umount OSS: %s mount with req: %+v", req.TargetPath, req)
mountPoint := req.TargetPath
err := validateNodeUnpublishVolumeRequest(req)
if err != nil {
return nil, err
}
if isDirectVolumePath(mountPoint) {
return ns.unPublishDirectVolume(ctx, req)
}
err = ns.cleanupMountPoint(ctx, req.VolumeId, req.TargetPath)
if err != nil {
log.Errorf("NodeUnpublishVolume: failed to unmount %q: %v", mountPoint, err)
return nil, status.Errorf(codes.Internal, "failed to unmount target %q: %v", mountPoint, err)
}
log.Infof("NodeUnpublishVolume: Umount OSS Successful: %s", mountPoint)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeStageVolume(
ctx context.Context,
req *csi.NodeStageVolumeRequest) (
*csi.NodeStageVolumeResponse, error) {
return &csi.NodeStageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnstageVolume(
ctx context.Context,
req *csi.NodeUnstageVolumeRequest) (
*csi.NodeUnstageVolumeResponse, error) {
log.Infof("NodeUnstageVolume: starting to unmount volume, volumeId: %s, target: %v", req.VolumeId, req.StagingTargetPath)
// unmount for sharedPath
mountpoint := GetGlobalMountPath(req.VolumeId)
err := ns.cleanupMountPoint(ctx, req.VolumeId, mountpoint)
if err != nil {
log.Errorf("NodeUnstageVolume: failed to unmount %q: %v", mountpoint, err)
return nil, status.Errorf(codes.Internal, "failed to unmount target %q: %v", mountpoint, err)
}
log.Infof("NodeUnstageVolume: umount OSS Successful: %s", mountpoint)
err = mounter.CleanupOssfsCredentialSecret(ctx, ns.clientset, ns.nodeName, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to cleanup ossfs credential secret: %v", err)
}
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (
*csi.NodeExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (ns *nodeServer) cleanupMountPoint(ctx context.Context, volumeId string, mountpoint string) error {
m := mountutils.New("")
var err error
forceUnmounter, ok := m.(mountutils.MounterForceUnmounter)
if ok {
err = mountutils.CleanupMountWithForce(mountpoint, forceUnmounter, true, time.Minute)
} else {
err = mountutils.CleanupMountPoint(mountpoint, m, true)
}
if err != nil {
return err
}
return ns.ossfsMounterFac.NewFuseMounter(ctx, volumeId, nil, false).Unmount(mountpoint)
}