diff options
| author | 泽淼 周 <zhouzemiao@ihuman.com> | 2025-09-27 18:28:34 +0800 |
|---|---|---|
| committer | Chris Lu <chrislusf@users.noreply.github.com> | 2025-12-06 18:53:22 -0800 |
| commit | da9e11f28bab3c90107c3a730ddbfb57d18475ca (patch) | |
| tree | 10b98b83c5b551bf7cccf71edb4b41c6fb40228d /pkg/driver/mounter.go | |
| parent | 1a36b03ed79a12e9b9db5954167ce825222f3d83 (diff) | |
| download | seaweedfs-csi-driver-da9e11f28bab3c90107c3a730ddbfb57d18475ca.tar.xz seaweedfs-csi-driver-da9e11f28bab3c90107c3a730ddbfb57d18475ca.zip | |
Optimization: Reduce unnecessary logic of seaweedfs-mount
Diffstat (limited to 'pkg/driver/mounter.go')
| -rw-r--r-- | pkg/driver/mounter.go | 144 |
1 files changed, 125 insertions, 19 deletions
diff --git a/pkg/driver/mounter.go b/pkg/driver/mounter.go index 6beee55..d86d82e 100644 --- a/pkg/driver/mounter.go +++ b/pkg/driver/mounter.go @@ -2,7 +2,12 @@ package driver import ( "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "github.com/seaweedfs/seaweedfs-csi-driver/pkg/datalocality" "github.com/seaweedfs/seaweedfs-csi-driver/pkg/mountmanager" "github.com/seaweedfs/seaweedfs/weed/glog" ) @@ -58,29 +63,29 @@ func (m *mountServiceMounter) Mount(target string) (Unmounter, error) { filers[i] = string(address) } - req := &mountmanager.MountRequest{ - VolumeID: m.volumeID, - TargetPath: target, - ReadOnly: m.readOnly, - Filers: filers, - CacheDir: m.driver.CacheDir, - CacheCapacityMB: m.driver.CacheCapacityMB, - ConcurrentWriters: m.driver.ConcurrentWriters, - UidMap: m.driver.UidMap, - GidMap: m.driver.GidMap, - DataCenter: m.driver.DataCenter, - DataLocality: m.driver.DataLocality.String(), - VolumeContext: m.volContext, - } - - resp, err := m.client.Mount(req) + cacheBase := m.driver.CacheDir + if cacheBase == "" { + cacheBase = os.TempDir() + } + cacheDir := filepath.Join(cacheBase, m.volumeID) + localSocket := mountmanager.LocalSocketPath(m.volumeID) + + args, err := m.buildMountArgs(target, cacheDir, localSocket, filers) if err != nil { return nil, err } - expectedSocket := mountmanager.LocalSocketPath(m.volumeID) - if resp.LocalSocket != "" && resp.LocalSocket != expectedSocket { - glog.Warningf("mount service returned socket %s for volume %s (expected %s)", resp.LocalSocket, m.volumeID, expectedSocket) + req := &mountmanager.MountRequest{ + VolumeID: m.volumeID, + TargetPath: target, + CacheDir: cacheDir, + MountArgs: args, + LocalSocket: localSocket, + } + + _, err = m.client.Mount(req) + if err != nil { + return nil, err } return &mountServiceUnmounter{ @@ -93,3 +98,104 @@ func (u *mountServiceUnmounter) Unmount() error { _, err := u.client.Unmount(&mountmanager.UnmountRequest{VolumeID: u.volumeID}) return err } + +func (m *mountServiceMounter) buildMountArgs(targetPath, cacheDir, localSocket string, filers []string) ([]string, error) { + volumeContext := m.volContext + if volumeContext == nil { + volumeContext = map[string]string{} + } + + path := volumeContext["path"] + if path == "" { + path = fmt.Sprintf("/buckets/%s", m.volumeID) + } + + collection := volumeContext["collection"] + if collection == "" { + collection = m.volumeID + } + + args := []string{ + "-logtostderr=true", + "mount", + "-dirAutoCreate=true", + "-umask=000", + fmt.Sprintf("-dir=%s", targetPath), + fmt.Sprintf("-localSocket=%s", localSocket), + fmt.Sprintf("-cacheDir=%s", cacheDir), + } + + if m.readOnly { + args = append(args, "-readOnly") + } + + argsMap := map[string]string{ + "collection": collection, + "filer": strings.Join(filers, ","), + "filer.path": path, + "cacheCapacityMB": strconv.Itoa(m.driver.CacheCapacityMB), + "concurrentWriters": strconv.Itoa(m.driver.ConcurrentWriters), + "map.uid": m.driver.UidMap, + "map.gid": m.driver.GidMap, + "disk": "", + "dataCenter": "", + "replication": "", + "ttl": "", + "chunkSizeLimitMB": "", + "volumeServerAccess": "", + "readRetryTime": "", + } + + dataLocality := m.driver.DataLocality + if contextLocality, ok := volumeContext["dataLocality"]; ok && contextLocality != "" { + if dl, ok := datalocality.FromString(contextLocality); ok { + dataLocality = dl + } else { + return nil, fmt.Errorf("invalid volumeContext dataLocality: %s", contextLocality) + } + } + + dataCenter := m.driver.DataCenter + if err := CheckDataLocality(&dataLocality, &dataCenter); err != nil { + return nil, err + } + + switch dataLocality { + case datalocality.Write_preferLocalDc: + argsMap["dataCenter"] = dataCenter + } + + parameterArgMap := map[string]string{ + "uidMap": "map.uid", + "gidMap": "map.gid", + "filerPath": "filer.path", + "diskType": "disk", + } + + ignoredArgs := map[string]struct{}{"dataLocality": {}} + + for key, value := range volumeContext { + if _, ignored := ignoredArgs[key]; ignored { + continue + } + if mapped, ok := parameterArgMap[key]; ok { + key = mapped + } + if _, ok := argsMap[key]; !ok { + glog.Warningf("VolumeContext '%s' ignored", key) + continue + } + if value != "" { + argsMap[key] = value + } + } + + for key, value := range argsMap { + if value == "" { + continue + } + args = append(args, fmt.Sprintf("-%s=%s", key, value)) + } + + return args, nil +} |
