Skip to content

Commit 273cbc1

Browse files
committed
Support fsetid static attach
1 parent 65fe00b commit 273cbc1

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

pkg/bmcpfs/bmcpfs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
// network types of CPFS mount targets
4343
networkTypeVPC = "vpc"
4444
networkTypeVSC = "vsc"
45+
46+
volumeHandleDelimiter = "+"
4547
)
4648

4749
type Driver struct {

pkg/bmcpfs/controllerserver.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type controllerServer struct {
4343
vscManager *internal.PrimaryVscManagerWithCache
4444
attachDetacher internal.CPFSAttachDetacher
4545
nasClient *nasclient.Client
46+
skipDetach bool
4647
}
4748

4849
func newControllerServer(region string) (*controllerServer, error) {
@@ -51,6 +52,11 @@ func newControllerServer(region string) (*controllerServer, error) {
5152
return nil, err
5253
}
5354

55+
skipDetach := false
56+
if skipDetachVal, _ := strconv.ParseBool(os.Getenv("SKIP_BMCPFS_DETACH")); skipDetachVal {
57+
skipDetach = skipDetachVal
58+
}
59+
5460
nasClient, err := cloud.NewNasClientV2(region)
5561
if err != nil {
5662
return nil, err
@@ -59,6 +65,7 @@ func newControllerServer(region string) (*controllerServer, error) {
5965
vscManager: internal.NewPrimaryVscManagerWithCache(efloClient),
6066
attachDetacher: internal.NewCPFSAttachDetacher(nasClient),
6167
nasClient: nasClient,
68+
skipDetach: skipDetach,
6269
}, nil
6370
}
6471

@@ -85,11 +92,12 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
8592
}, nil
8693
}
8794

95+
cpfsID, _ := parseVolumeHandle(req.VolumeId)
8896
// Get VscMountTarget of filesystem
8997
mt := req.VolumeContext[_vscMountTarget]
9098
if mt == "" {
9199
var err error
92-
mt, err = getMountTarget(cs.nasClient, req.VolumeId, networkTypeVSC)
100+
mt, err = getMountTarget(cs.nasClient, cpfsID, networkTypeVSC)
93101
if err != nil {
94102
return nil, status.Errorf(codes.Internal, "failed to get VscMountTarget: %v", err)
95103
}
@@ -107,7 +115,7 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
107115
klog.Info("Use VSC MountTarget for lingjun node", "nodeId", req.NodeId, "vscId", vscId)
108116

109117
// Attach CPFS to VSC
110-
err = cs.attachDetacher.Attach(ctx, req.VolumeId, vscId)
118+
err = cs.attachDetacher.Attach(ctx, cpfsID, vscId)
111119
if err != nil {
112120
if autoSwitch, _ := strconv.ParseBool(req.VolumeContext[_mpAutoSwitch]); autoSwitch && internal.IsAttachNotSupportedError(err) {
113121
if req.VolumeContext[_vpcMountTarget] == "" {
@@ -138,10 +146,9 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
138146
func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (
139147
*csi.ControllerUnpublishVolumeResponse, error,
140148
) {
141-
if !strings.HasPrefix(req.NodeId, LingjunNodeIDPrefix) {
149+
if !strings.HasPrefix(req.NodeId, LingjunNodeIDPrefix) || cs.skipDetach {
142150
return &csi.ControllerUnpublishVolumeResponse{}, nil
143151
}
144-
145152
// Create Primary vsc for Lingjun node
146153
lingjunInstanceId := strings.TrimPrefix(req.NodeId, LingjunNodeIDPrefix)
147154
if LingjunNodeIDPrefix == "" {
@@ -155,6 +162,7 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
155162
klog.InfoS("ControllerUnpublishVolume: skip detaching cpfs from vsc as vsc not found", "node", req.NodeId)
156163
return &csi.ControllerUnpublishVolumeResponse{}, nil
157164
}
165+
// If `req.VolumeId` is a combination of `cpfsID` and `fsetID`, Detach will trigger an error.
158166
err = cs.attachDetacher.Detach(ctx, req.VolumeId, vsc.VscID)
159167
if err != nil {
160168
return nil, status.Error(codes.Internal, err.Error())
@@ -209,6 +217,14 @@ func newEfloClient(region string) (*efloclient.Client, error) {
209217
return efloclient.NewClient(config)
210218
}
211219

220+
func parseVolumeHandle(volumeHandle string) (string, string) {
221+
parts := strings.Split(volumeHandle, volumeHandleDelimiter)
222+
if len(parts) == 2 {
223+
return parts[0], parts[1]
224+
}
225+
return parts[0], ""
226+
}
227+
212228
func getMountTarget(client *nasclient.Client, fsId, networkType string) (string, error) {
213229
resp, err := client.DescribeFileSystems(&nasclient.DescribeFileSystemsRequest{
214230
FileSystemId: &fsId,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package bmcpfs
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
// TestParseVolumeHandle tests the parseVolumeHandle function with various inputs
26+
func TestParseVolumeHandle(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
volumeHandle string
30+
expectedFsId string
31+
expectedFsetId string
32+
}{
33+
{
34+
name: "single part",
35+
volumeHandle: "fs-12345",
36+
expectedFsId: "fs-12345",
37+
expectedFsetId: "",
38+
},
39+
{
40+
name: "two parts",
41+
volumeHandle: "fs-12345+fset-67890",
42+
expectedFsId: "fs-12345",
43+
expectedFsetId: "fset-67890",
44+
},
45+
{
46+
name: "multiple delimiters",
47+
volumeHandle: "fs-12345+fset-67890+extra",
48+
expectedFsId: "fs-12345",
49+
expectedFsetId: "",
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
fsId, fsetId := parseVolumeHandle(tt.volumeHandle)
56+
assert.Equal(t, tt.expectedFsId, fsId)
57+
assert.Equal(t, tt.expectedFsetId, fsetId)
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)