Skip to content

Commit 201e17d

Browse files
authored
Merge pull request #1515 from iltyty/nas-filesystem-id
Support specifying NAS filesystem ID
2 parents e146a80 + 1b80115 commit 201e17d

11 files changed

+162
-65
lines changed

pkg/nas/cloud/nas_client_v2.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ func (c *NasClientV2) DescribeAccesspoint(filesystemId, accessPointId string) (*
156156
})
157157
}
158158

159+
func (c *NasClientV2) DescribeFileSystems(filesystemID string) (*sdk.DescribeFileSystemsResponse, error) {
160+
c.limiter.Take()
161+
return c.client.DescribeFileSystems(&sdk.DescribeFileSystemsRequest{
162+
FileSystemId: &filesystemID,
163+
})
164+
}
165+
159166
func IsAccessPointNotFoundError(err error) bool {
160167
if err == nil {
161168
return false

pkg/nas/filesystem_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343

4444
const (
4545
ProtocolType = "protocolType"
46-
FileSystemType = "fileSystemType"
4746
EncryptType = "encryptType"
4847
SnapshotID = "snapshotID"
4948
StorageType = "storageType"
@@ -164,7 +163,8 @@ func (cs *filesystemController) CreateVolume(ctx context.Context, req *csi.Creat
164163
if err != nil {
165164
return nil, status.Errorf(codes.Internal, "init nas client: %v", err)
166165
}
167-
fileSystemID := ""
166+
var fileSystemID string
167+
fileSystemType := nasVol.FileSystemType
168168
// if the pvc mapped fileSystem is already create, skip creating a filesystem
169169
if value, ok := cs.pvcFileSystemIDMap.Load(pvName); ok && value != "" {
170170
klog.Warningf("CreateVolume: Nfs Volume(%s)'s filesystem %s has Created Already, try to create mountTarget", pvName, value)
@@ -193,6 +193,7 @@ func (cs *filesystemController) CreateVolume(ctx context.Context, req *csi.Creat
193193
return nil, status.Error(codes.Internal, errMsg)
194194
}
195195
fileSystemID = createFileSystemsResponse.FileSystemId
196+
fileSystemType = createFileSystemsRequest.FileSystemType
196197
cs.pvcFileSystemIDMap.Store(pvName, fileSystemID)
197198
klog.Infof("CreateVolume: Volume: %s, Successful Create Nas filesystem with ID: %s, with requestID: %s", pvName, fileSystemID, createFileSystemsResponse.RequestId)
198199

@@ -295,7 +296,8 @@ func (cs *filesystemController) CreateVolume(ctx context.Context, req *csi.Creat
295296
time.Sleep(time.Duration(2) * time.Second)
296297
}
297298

298-
volumeContext["fileSystemId"] = fileSystemID
299+
volumeContext[filesystemIDKey] = fileSystemID
300+
volumeContext[filesystemTypeKey] = fileSystemType
299301
volumeContext["server"] = mountTargetDomain
300302
volumeContext["path"] = filepath.Join("/")
301303
if nasVol.FileSystemType == "extreme" {
@@ -322,7 +324,7 @@ func (cs *filesystemController) getNasVolumeOptions(req *csi.CreateVolumeRequest
322324
nasVolArgs := &nasVolumeArgs{}
323325
volOptions := req.GetParameters()
324326

325-
if nasVolArgs.FileSystemType, ok = volOptions[FileSystemType]; !ok {
327+
if nasVolArgs.FileSystemType, ok = volOptions[filesystemTypeKey]; !ok {
326328
nasVolArgs.ProtocolType = "standard"
327329
} else if nasVolArgs.FileSystemType != "standard" && nasVolArgs.FileSystemType != "extreme" {
328330
return nil, fmt.Errorf("Required parameter [parameter.fileSystemType] must be [standard, extreme]")

pkg/nas/filesystem_controller_test.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ func TestCreateVolume(t *testing.T) {
138138
CapacityBytes: 0,
139139
VolumeId: "",
140140
VolumeContext: map[string]string{
141-
"deleteVolume": "false",
142-
"fileSystemId": "file-system-id",
143-
"path": "/",
144-
"server": "",
141+
"deleteVolume": "false",
142+
"fileSystemId": "file-system-id",
143+
"fileSystemType": "",
144+
"path": "/",
145+
"server": "",
145146
},
146147
ContentSource: nil,
147148
AccessibleTopology: nil,
@@ -172,10 +173,11 @@ func TestCreateVolume(t *testing.T) {
172173
CapacityBytes: 0,
173174
VolumeId: "",
174175
VolumeContext: map[string]string{
175-
"deleteVolume": "false",
176-
"fileSystemId": "file-system-id",
177-
"path": "/",
178-
"server": "",
176+
"deleteVolume": "false",
177+
"fileSystemId": "file-system-id",
178+
"fileSystemType": "",
179+
"path": "/",
180+
"server": "",
179181
},
180182
ContentSource: nil,
181183
AccessibleTopology: nil,
@@ -192,10 +194,11 @@ func TestCreateVolume(t *testing.T) {
192194
CapacityBytes: 0,
193195
VolumeId: "",
194196
VolumeContext: map[string]string{
195-
"deleteVolume": "false",
196-
"fileSystemId": "file-system-id",
197-
"path": "/",
198-
"server": "",
197+
"deleteVolume": "false",
198+
"fileSystemId": "file-system-id",
199+
"fileSystemType": "",
200+
"path": "/",
201+
"server": "",
199202
},
200203
ContentSource: nil,
201204
AccessibleTopology: nil,
@@ -212,11 +215,12 @@ func TestCreateVolume(t *testing.T) {
212215
CapacityBytes: 100 * 1024 * 1024 * 1024,
213216
VolumeId: "",
214217
VolumeContext: map[string]string{
215-
"deleteVolume": "false",
216-
"fileSystemId": "file-system-id",
217-
"path": "/share",
218-
"server": "test.mount.target.domain",
219-
"vers": "3",
218+
"deleteVolume": "false",
219+
"fileSystemId": "file-system-id",
220+
"fileSystemType": "extreme",
221+
"path": "/share",
222+
"server": "test.mount.target.domain",
223+
"vers": "3",
220224
},
221225
ContentSource: nil,
222226
AccessibleTopology: nil,
@@ -279,9 +283,9 @@ func createExtremeFileSystemRequest() *csi.CreateVolumeRequest {
279283
RequiredBytes: 100 * 1024 * 1024 * 1024,
280284
},
281285
Parameters: map[string]string{
282-
FileSystemType: "extreme",
283-
VpcID: "vpc-id",
284-
VSwitchID: "vswitch-id",
286+
filesystemTypeKey: "extreme",
287+
VpcID: "vpc-id",
288+
VSwitchID: "vswitch-id",
285289
},
286290
}
287291
}
@@ -857,7 +861,7 @@ func TestGetNasVolumeOptions(t *testing.T) {
857861
name: "Invalid file system type",
858862
args: &csi.CreateVolumeRequest{
859863
Parameters: map[string]string{
860-
FileSystemType: "InvalidFileSystemType",
864+
filesystemTypeKey: "InvalidFileSystemType",
861865
},
862866
},
863867
expected: nil,
@@ -870,7 +874,7 @@ func TestGetNasVolumeOptions(t *testing.T) {
870874
RequiredBytes: 1024,
871875
},
872876
Parameters: map[string]string{
873-
FileSystemType: "extreme",
877+
filesystemTypeKey: "extreme",
874878
},
875879
},
876880
expected: nil,
@@ -911,8 +915,8 @@ func TestGetNasVolumeOptions(t *testing.T) {
911915
RequiredBytes: 100 * 1024 * 1024 * 1024,
912916
},
913917
Parameters: map[string]string{
914-
FileSystemType: "extreme",
915-
StorageType: "InvalidStorageType",
918+
filesystemTypeKey: "extreme",
919+
StorageType: "InvalidStorageType",
916920
},
917921
},
918922
expected: nil,
@@ -925,8 +929,8 @@ func TestGetNasVolumeOptions(t *testing.T) {
925929
RequiredBytes: 100 * 1024 * 1024 * 1024,
926930
},
927931
Parameters: map[string]string{
928-
FileSystemType: "extreme",
929-
EncryptType: "InvalidEncryptType",
932+
filesystemTypeKey: "extreme",
933+
EncryptType: "InvalidEncryptType",
930934
},
931935
},
932936
expected: nil,

pkg/nas/interfaces/nas_client_v2_interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ type NasClientV2Interface interface {
1010
CreateAccesspoint(req *sdk.CreateAccessPointRequest) (*sdk.CreateAccessPointResponse, error)
1111
DeleteAccesspoint(filesystemId, accessPointId string) error
1212
DescribeAccesspoint(filesystemId, accessPointId string) (*sdk.DescribeAccessPointResponse, error)
13+
DescribeFileSystems(filesystemID string) (*sdk.DescribeFileSystemsResponse, error)
1314
}

pkg/nas/interfaces/nas_client_v2_mock.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ func (n *MockNasClientV2Interface) DescribeAccesspoint(filesystemId, accessPoint
5252
FileSystemId: &filesystemId,
5353
})
5454
}
55+
56+
func (n *MockNasClientV2Interface) DescribeFileSystems(filesystemID string) (*sdk.DescribeFileSystemsResponse, error) {
57+
return n.client.DescribeFileSystems(&sdk.DescribeFileSystemsRequest{
58+
FileSystemId: &filesystemID,
59+
})
60+
}

pkg/nas/interfaces/nas_v2_interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type NasV2Interface interface {
88
CreateDir(request *nas.CreateDirRequest) (*nas.CreateDirResponse, error)
99
DeleteAccessPoint(request *nas.DeleteAccessPointRequest) (*nas.DeleteAccessPointResponse, error)
1010
DescribeAccessPoint(request *nas.DescribeAccessPointRequest) (*nas.DescribeAccessPointResponse, error)
11+
DescribeFileSystems(request *nas.DescribeFileSystemsRequest) (*nas.DescribeFileSystemsResponse, error)
1112
GetRecycleBinAttribute(request *nas.GetRecycleBinAttributeRequest) (*nas.GetRecycleBinAttributeResponse, error)
1213
SetDirQuota(request *nas.SetDirQuotaRequest) (*nas.SetDirQuotaResponse, error)
1314
}

pkg/nas/interfaces/nas_v2_mock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/nas/nodeserver.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ const (
138138
cnfsAlwaysFallbackEventTmpl = "CNFS automatically switched from %s to %s."
139139
cnfsIfConnectFailedFallbackEventTmpl = "Due to network issues, CNFS automatically switched from %s to %s."
140140
cnfsIfMountTargetUnhealthyFallbackEventTmpl = "Due to mount target inactive, CNFS automatically switched from %s to %s."
141-
cpfsServerSuffix = "cpfs.aliyuncs.com"
142141
)
143142

144143
func validateNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) error {
@@ -207,12 +206,14 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
207206
var cnfsName string
208207
for key, value := range req.VolumeContext {
209208
switch strings.ToLower(key) {
209+
case "filesystemtype":
210+
opt.FSType = value
210211
case "useclient": // only VK will fetch this parameter from CNFS and add it to VolumeContext
211212
opt.ClientType = strings.ToLower(value)
212213
if opt.ClientType == EFCClient {
213214
opt.MountProtocol = MountProtocolEFC
214215
}
215-
if strings.HasSuffix(req.VolumeContext["server"], cpfsServerSuffix) {
216+
if isCPFS(req.VolumeContext[filesystemTypeKey], req.VolumeContext["server"]) {
216217
opt.FSType = "cpfs"
217218
} else {
218219
opt.FSType = "standard"
@@ -334,7 +335,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
334335
opt.Vers = "4.0"
335336
}
336337

337-
if strings.Contains(opt.Server, "extreme.nas.aliyuncs.com") {
338+
if isExtrameNAS(opt.FSType, opt.Server) {
338339
if opt.Vers != "3" {
339340
return nil, errors.New("Extreme nas only support nfs v3 " + opt.Server)
340341
}
@@ -438,12 +439,12 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
438439
}
439440
if opt.MountProtocol == "efc" {
440441
if strings.Contains(opt.Server, ".nas.aliyuncs.com") {
441-
fsID := GetFsIDByNasServer(opt.Server)
442+
fsID := getNASIDFromMapOrServer(req.VolumeContext, opt.Server)
442443
if len(fsID) != 0 {
443444
utils.WriteMetricsInfo(metricsPathPrefix, req, "10", "efc", "nas", fsID)
444445
}
445446
} else {
446-
fsID := GetFsIDByCpfsServer(opt.Server)
447+
fsID := getCPFSIDFromMapOrServer(req.VolumeContext, opt.Server)
447448
if len(fsID) != 0 {
448449
utils.WriteMetricsInfo(metricsPathPrefix, req, "10", "efc", "cpfs", fsID)
449450
}

pkg/nas/sharepath_controller.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,44 @@ package nas
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"path/filepath"
2223

2324
"github.com/container-storage-interface/spec/lib/go/csi"
25+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cloud/metadata"
2426
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/common"
27+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/interfaces"
2528
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/internal"
2629
"google.golang.org/grpc/codes"
2730
"google.golang.org/grpc/status"
2831
corev1 "k8s.io/api/core/v1"
2932
"k8s.io/klog/v2"
3033
)
3134

32-
type sharepathController struct{}
35+
type sharepathController struct {
36+
nasClient interfaces.NasClientV2Interface
37+
}
3338

34-
func newSharepathController(_ *internal.ControllerConfig) (internal.Controller, error) {
35-
return &sharepathController{}, nil
39+
func newSharepathController(config *internal.ControllerConfig) (internal.Controller, error) {
40+
region, err := config.Metadata.Get(metadata.RegionID)
41+
if err != nil {
42+
return nil, fmt.Errorf("failed to get region ID: %w", err)
43+
}
44+
nasClient, err := config.NasClientFactory.V2(region)
45+
if err != nil {
46+
return nil, err
47+
}
48+
return &sharepathController{
49+
nasClient: nasClient,
50+
}, nil
3651
}
3752

3853
func (cs *sharepathController) VolumeAs() string {
3954
return "sharepath"
4055
}
4156

4257
func (cs *sharepathController) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
58+
var server, path string
4359
parameters := req.Parameters
4460
reclaimPolicy, ok := parameters[common.CsiAlibabaCloudPrefix+"/"+"reclaimPolicy"]
4561
if ok && reclaimPolicy != string(corev1.PersistentVolumeReclaimRetain) {
@@ -48,7 +64,7 @@ func (cs *sharepathController) CreateVolume(ctx context.Context, req *csi.Create
4864
volumeContext := map[string]string{}
4965
// using cnfs or not
5066
if cnfsName := parameters["containerNetworkFileSystem"]; cnfsName != "" {
51-
path := parameters["path"]
67+
path = parameters["path"]
5268
if path == "" {
5369
path = "/"
5470
} else {
@@ -57,7 +73,7 @@ func (cs *sharepathController) CreateVolume(ctx context.Context, req *csi.Create
5773
volumeContext["containerNetworkFileSystem"] = cnfsName
5874
volumeContext["path"] = path
5975
} else {
60-
server, path := muxServerSelector.SelectNfsServer(parameters["server"])
76+
server, path = muxServerSelector.SelectNfsServer(parameters["server"])
6177
if server == "" {
6278
return nil, status.Error(codes.InvalidArgument, "invalid nas server")
6379
}
@@ -68,6 +84,9 @@ func (cs *sharepathController) CreateVolume(ctx context.Context, req *csi.Create
6884
volumeContext["path"] = path
6985
}
7086

87+
volumeContext[filesystemIDKey] = getNASIDFromMapOrServer(parameters, server)
88+
volumeContext[filesystemTypeKey] = getFilesystemTypeFromAPIOrServer(volumeContext[filesystemIDKey], server, cs.nasClient)
89+
7190
// fill volumeContext
7291
if mountType := parameters["mountType"]; mountType != "" {
7392
volumeContext["mountType"] = mountType

pkg/nas/subpath_controller.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func (cs *subpathController) CreateVolume(ctx context.Context, req *csi.CreateVo
104104
} else {
105105
var server string
106106
server, path = muxServerSelector.SelectNfsServer(parameters["server"])
107-
filesystemId, _, _ = strings.Cut(server, "-")
107+
filesystemId = getNASIDFromMapOrServer(parameters, server)
108108
if server == "" || filesystemId == "" {
109109
return nil, status.Error(codes.InvalidArgument, "invalid nas server")
110110
}
111-
filesystemType = cloud.GetFilesystemTypeByMountTargetDomain(server)
111+
filesystemType = getFilesystemTypeFromAPIOrServer(filesystemId, server, cs.nasClient)
112112
// set volumeContext
113113
if protocol := parameters["mountProtocol"]; protocol != "" {
114114
volumeContext["mountProtocol"] = protocol
@@ -118,6 +118,8 @@ func (cs *subpathController) CreateVolume(ctx context.Context, req *csi.CreateVo
118118
// fill volumeContext
119119
path = filepath.Join(path, req.Name)
120120
volumeContext["path"] = path
121+
volumeContext[filesystemIDKey] = filesystemId
122+
volumeContext[filesystemTypeKey] = filesystemType
121123
if mountType := parameters["mountType"]; mountType != "" {
122124
if mountType == "losetup" {
123125
volumeContext["loopImageSize"] = strconv.FormatInt(capacity, 10)
@@ -191,8 +193,8 @@ func (cs *subpathController) DeleteVolume(ctx context.Context, req *csi.DeleteVo
191193
recycleBinEnabled, _ = strconv.ParseBool(cnfs.Status.FsAttributes.EnableTrashCan)
192194
} else {
193195
server := attributes["server"]
194-
filesystemId, _, _ = strings.Cut(server, "-")
195-
filesystemType := cloud.GetFilesystemTypeByMountTargetDomain(server)
196+
filesystemId = getNASIDFromMapOrServer(attributes, server)
197+
filesystemType := getFilesystemTypeFromAPIOrServer(filesystemId, server, cs.nasClient)
196198
if filesystemType == cloud.FilesystemTypeStandard {
197199
var err error
198200
recycleBinEnabled, err = cs.isRecycleBinEnabled(filesystemId)
@@ -274,7 +276,7 @@ func (cs *subpathController) ControllerExpandVolume(ctx context.Context, req *cs
274276
filesystemId = cnfs.Status.FsAttributes.FilesystemID
275277
} else {
276278
server := attributes["server"]
277-
filesystemId, _, _ = strings.Cut(server, "-")
279+
filesystemId = getNASIDFromMapOrServer(pv.Spec.CSI.VolumeAttributes, server)
278280
}
279281
if filesystemId == "" {
280282
return nil, status.Error(codes.InvalidArgument, "empty filesystemId")

0 commit comments

Comments
 (0)