Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use subdir mount subpath when StorageClassShareMount is disabled #1266

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/scripts/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ def test_deployment_dynamic_patch_pv():
pv_name = volume_id
pv = client.CoreV1Api().read_persistent_volume(name=pv_name)
pv.spec.mount_options = ["subdir={}".format(subdir), "verbose"]
pv.spec.mount_options.append("subdir={}".format(subdir))
LOG.info(f"Patch PV {pv_name}: add subdir={subdir} and verbose in mountOptions")
client.CoreV1Api().patch_persistent_volume(pv_name, pv)

Expand Down Expand Up @@ -1143,7 +1142,7 @@ def test_deployment_dynamic_patch_pv():
LOG.info("Check subdir {}".format(subdir))
result = check_mount_point(subdir + "/{}/out.txt".format(volume_id))
if not result:
raise Exception("mount Point of /{}/out.txt are not ready within 5 min.".format(subdir))
raise Exception("mount Point of {}/{}/out.txt are not ready within 5 min.".format(subdir,volume_id))

# check target
LOG.info("Check target path is ok..")
Expand Down Expand Up @@ -1749,7 +1748,6 @@ def test_deployment_dynamic_patch_pv_with_webhook():
pv_name = volume_id
pv = client.CoreV1Api().read_persistent_volume(name=pv_name)
pv.spec.mount_options = ["subdir={}".format(subdir), "verbose"]
pv.spec.mount_options.append("subdir={}".format(subdir))
LOG.info(f"Patch PV {pv_name}: add subdir={subdir} and verbose in mountOptions")
client.CoreV1Api().patch_persistent_volume(pv_name, pv)

Expand Down
14 changes: 1 addition & 13 deletions pkg/dashboard/services/pods/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import (
"path"

"github.com/gin-gonic/gin"
"github.com/juicedata/juicefs-csi-driver/pkg/common"
"github.com/juicedata/juicefs-csi-driver/pkg/config"
"github.com/juicedata/juicefs-csi-driver/pkg/dashboard/utils"
"github.com/juicedata/juicefs-csi-driver/pkg/util"
"github.com/juicedata/juicefs-csi-driver/pkg/util/resource"
"golang.org/x/net/websocket"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -162,16 +160,6 @@ func (s *podService) WarmupPod(c *gin.Context, namespace, name, container string
klog.Error("Failed to get mount pod: ", err)
return
}
rootPath := ""
volumeId := mountpod.Labels[common.PodUniqueIdLabelKey]
var pv corev1.PersistentVolume
if err := s.client.Get(ctx, types.NamespacedName{Name: volumeId}, &pv); err == nil {
if pv.Spec.CSI != nil && pv.Spec.CSI.VolumeAttributes != nil {
if subPath, ok := pv.Spec.CSI.VolumeAttributes["subPath"]; ok {
rootPath = subPath
}
}
}

mntPath, _, err := util.GetMountPathOfPod(*mountpod)
if err != nil || mntPath == "" {
Expand All @@ -189,7 +177,7 @@ func (s *podService) WarmupPod(c *gin.Context, namespace, name, container string
cmds = append(cmds, "--io-retries="+ioRetries)
cmds = append(cmds, "--max-failure="+maxFailure)
}
cmds = append(cmds, path.Join(mntPath, rootPath, customSubPath))
cmds = append(cmds, path.Join(mntPath, customSubPath))
if err := resource.ExecInPod(
ctx,
s.k8sClient, s.kubeconfig, terminal, namespace, name, container,
Expand Down
3 changes: 3 additions & 0 deletions pkg/juicefs/juicefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (fs *jfs) GetBasePath() string {
// CreateVol creates the directory needed
func (fs *jfs) CreateVol(ctx context.Context, volumeID, subPath string) (string, error) {
log := util.GenLog(ctx, jfsLog, "CreateVol")
if !config.StorageClassShareMount && !config.ByProcess {
return fs.MountPath, nil
}
volPath := filepath.Join(fs.MountPath, subPath)
log.V(1).Info("checking volPath exists", "volPath", volPath, "fs", fs)
var exists bool
Expand Down
8 changes: 7 additions & 1 deletion pkg/juicefs/juicefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ func Test_jfs_CreateVol(t *testing.T) {
}
got, err := j.CreateVol(context.TODO(), "", "subPath")
So(err, ShouldBeNil)
So(got, ShouldEqual, "/mountPath/subPath")
So(got, ShouldEqual, "/mountPath")
})
Convey("test exist err", func() {
patch1 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return false, errors.New("test")
})
defer patch1.Reset()
patch2 := ApplyGlobalVar(&config.StorageClassShareMount, true)
defer patch2.Reset()

j := jfs{
MountPath: "/mountPath",
Expand All @@ -87,6 +89,8 @@ func Test_jfs_CreateVol(t *testing.T) {
return errors.New("test")
})
defer patch2.Reset()
patch3 := ApplyGlobalVar(&config.StorageClassShareMount, true)
defer patch3.Reset()

j := jfs{
MountPath: "/mountPath",
Expand All @@ -108,6 +112,8 @@ func Test_jfs_CreateVol(t *testing.T) {
return mocks.FakeFileInfoIno1{}, errors.New("test")
})
defer patch3.Reset()
patch4 := ApplyGlobalVar(&config.StorageClassShareMount, true)
defer patch4.Reset()

j := jfs{
MountPath: "/mountPath",
Expand Down
3 changes: 0 additions & 3 deletions pkg/juicefs/mount/builder/cci-serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ func (r *CCIBuilder) NewMountSidecar() *corev1.Pod {
pod.Spec.Volumes = append(pod.Spec.Volumes, cacheVolumes...)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, cacheVolumeMounts...)

// overwrite subdir
r.overwriteSubdirWithSubPath()

// command
mountCmd := r.genMountCommand()
initCmd := r.genInitCommand()
Expand Down
42 changes: 20 additions & 22 deletions pkg/juicefs/mount/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,26 @@ func (r *BaseBuilder) genCommonJuicePod(cnGen func() corev1.Container) *corev1.P
// genMountCommand generates mount command
func (r *BaseBuilder) genMountCommand() string {
cmd := ""
options := r.jfsSetting.Options
options := []string{}
subdir := r.jfsSetting.SubPath
if !config.StorageClassShareMount {
for _, option := range r.jfsSetting.Options {
if strings.HasPrefix(option, "subdir=") {
s := strings.Split(option, "=")
if len(s) != 2 {
continue
}
subdir = path.Join(s[1], r.jfsSetting.SubPath)
continue
}
options = append(options, option)
}
if subdir != "" {
options = append(options, fmt.Sprintf("subdir=%s", subdir))
}
} else {
options = r.jfsSetting.Options
}
if r.jfsSetting.IsCe {
mountArgs := []string{"exec", config.CeMountPath, "${metaurl}", security.EscapeBashStr(r.jfsSetting.MountPath)}
if !util.ContainsPrefix(options, "metrics=") {
Expand Down Expand Up @@ -206,27 +225,6 @@ func (r *BaseBuilder) getQuotaPath() string {
return targetPath
}

func (r *BaseBuilder) overwriteSubdirWithSubPath() {
if r.jfsSetting.SubPath != "" {
options := make([]string, 0)
subdir := r.jfsSetting.SubPath
for _, option := range r.jfsSetting.Options {
if strings.HasPrefix(option, "subdir=") {
s := strings.Split(option, "=")
if len(s) != 2 {
continue
}
if s[0] == "subdir" {
subdir = path.Join(s[1], r.jfsSetting.SubPath)
}
continue
}
options = append(options, option)
}
r.jfsSetting.Options = append(options, fmt.Sprintf("subdir=%s", subdir))
}
}

// genJobCommand generates job command
func (r *BaseBuilder) getJobCommand() string {
var cmd string
Expand Down
80 changes: 0 additions & 80 deletions pkg/juicefs/mount/builder/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,92 +18,12 @@ package builder

import (
"reflect"
"strings"
"testing"

"github.com/juicedata/juicefs-csi-driver/pkg/common"
"github.com/juicedata/juicefs-csi-driver/pkg/config"
)

func TestBaseBuilder_overwriteSubdirWithSubPath(t *testing.T) {
type fields struct {
jfsSetting *config.JfsSetting
}
tests := []struct {
name string
fields fields
wantSubdir string
}{
{
name: "test1",
fields: fields{
jfsSetting: &config.JfsSetting{
Options: []string{
"subdir=abc",
},
SubPath: "def",
},
},
wantSubdir: "abc/def",
},
{
name: "test2",
fields: fields{
jfsSetting: &config.JfsSetting{
Options: []string{},
SubPath: "def",
},
},
wantSubdir: "def",
},
{
name: "test3",
fields: fields{
jfsSetting: &config.JfsSetting{
Options: []string{},
SubPath: "",
},
},
wantSubdir: "",
},
{
name: "test4",
fields: fields{
jfsSetting: &config.JfsSetting{
Options: []string{
"subdir=abc",
},
SubPath: "",
},
},
wantSubdir: "abc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &BaseBuilder{
jfsSetting: tt.fields.jfsSetting,
}
r.overwriteSubdirWithSubPath()
var subdir string
for _, option := range r.jfsSetting.Options {
if strings.HasPrefix(option, "subdir=") {
s := strings.Split(option, "=")
if len(s) != 2 {
t.Error("overwriteSubdirWithSubPath() error")
}
if s[0] == "subdir" {
subdir = s[1]
}
}
}

if subdir != tt.wantSubdir {
t.Errorf("overwriteSubdirWithSubPath() got=%s, want=%s", subdir, tt.wantSubdir)
}
})
}
}
func TestGenMetadata(t *testing.T) {
tests := []struct {
name string
Expand Down
3 changes: 0 additions & 3 deletions pkg/juicefs/mount/builder/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ func (r *ContainerBuilder) NewMountSidecar() *corev1.Pod {
)}},
}

// overwrite subdir
r.overwriteSubdirWithSubPath()

mountCmd := r.genMountCommand()
cmd := mountCmd
initCmd := r.genInitCommand()
Expand Down
13 changes: 13 additions & 0 deletions pkg/juicefs/mount/builder/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ func TestNewMountPod(t *testing.T) {
func TestPodMount_getCommand(t *testing.T) {
type args struct {
mountPath string
subPath string
options []string
}
tests := []struct {
Expand Down Expand Up @@ -455,6 +456,17 @@ func TestPodMount_getCommand(t *testing.T) {
},
want: "exec /sbin/mount.juicefs test /jfs/test-volume -o foreground,no-update,debug",
},
{
name: "test-subpath",
isCe: false,
source: "test",
args: args{
mountPath: "/jfs/test-volume",
options: []string{"subdir=test"},
subPath: "/jfs/test-volume/subpath",
},
want: "exec /sbin/mount.juicefs test /jfs/test-volume -o foreground,no-update,subdir=test/jfs/test-volume/subpath",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -464,6 +476,7 @@ func TestPodMount_getCommand(t *testing.T) {
Source: tt.source,
IsCe: tt.isCe,
MountPath: tt.args.mountPath,
SubPath: tt.args.subPath,
Options: tt.args.options,
Attr: &config.PodAttr{},
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/juicefs/mount/builder/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ func (r *ServerlessBuilder) NewMountSidecar() *corev1.Pod {
pod.Spec.Volumes = append(pod.Spec.Volumes, cacheVolumes...)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, cacheVolumeMounts...)

// overwrite subdir
r.overwriteSubdirWithSubPath()

// command
mountCmd := r.genMountCommand()
initCmd := r.genInitCommand()
Expand Down
3 changes: 0 additions & 3 deletions pkg/juicefs/mount/builder/vci-serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ func (r *VCIBuilder) NewMountSidecar() *corev1.Pod {
pod.Spec.Volumes = append(pod.Spec.Volumes, cacheVolumes...)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, cacheVolumeMounts...)

// overwrite subdir
r.overwriteSubdirWithSubPath()

// command
mountCmd := r.genMountCommand()
initCmd := r.genInitCommand()
Expand Down
Loading