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

check corrupted mount before mkdir target #726

Merged
merged 2 commits into from
Aug 28, 2023
Merged
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
3 changes: 1 addition & 2 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package driver

import (
"context"
"os"
"path"
"reflect"
"strconv"
Expand Down Expand Up @@ -95,7 +94,7 @@ func (d *nodeService) NodePublishVolume(ctx context.Context, req *csi.NodePublis
}

klog.V(5).Infof("NodePublishVolume: creating dir %s", target)
if err := os.MkdirAll(target, os.FileMode(0755)); err != nil {
if err := d.juicefs.CreateTarget(ctx, target); err != nil {
return nil, status.Errorf(codes.Internal, "Could not create dir %q: %v", target, err)
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
"k8s.io/client-go/kubernetes/fake"
k8sexec "k8s.io/utils/exec"
"k8s.io/utils/mount"

"github.com/juicedata/juicefs-csi-driver/pkg/juicefs"
"github.com/juicedata/juicefs-csi-driver/pkg/juicefs/mocks"
Expand Down Expand Up @@ -73,6 +75,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs.EXPECT().BindTarget(context.TODO(), bindSource, targetPath).Return(nil)
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, []string{"ro"}).Return(mockJfs, nil)
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -119,6 +122,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs.EXPECT().BindTarget(context.TODO(), bindSource, targetPath).Return(nil)
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, mountOptions).Return(mockJfs, nil)
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -164,6 +168,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs.EXPECT().BindTarget(context.TODO(), bindSource, targetPath).Return(nil)
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, mountOptions).Return(mockJfs, nil)
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -213,6 +218,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs := mocks.NewMockJfs(mockCtl)
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, []string{"ro"}).Return(mockJfs, errors.New("test"))
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -254,6 +260,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs.EXPECT().CreateVol(context.TODO(), volumeId, subPath).Return(bindSource, errors.New("test"))
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, []string{"ro"}).Return(mockJfs, nil)
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -296,6 +303,7 @@ func TestNodePublishVolume(t *testing.T) {
mockJfs.EXPECT().BindTarget(context.TODO(), bindSource, targetPath).Return(errors.New("test"))
mockJuicefs := mocks.NewMockInterface(mockCtl)
mockJuicefs.EXPECT().JfsMount(context.TODO(), volumeId, targetPath, secret, volumeCtx, []string{"ro"}).Return(mockJfs, nil)
mockJuicefs.EXPECT().CreateTarget(context.TODO(), targetPath).Return(nil)

juicefsDriver := &nodeService{
juicefs: mockJuicefs,
Expand Down Expand Up @@ -328,11 +336,22 @@ func TestNodePublishVolume(t *testing.T) {
return errors.New("test")
})
defer patch1.Reset()
patch2 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return false, nil
})
defer patch2.Reset()

client := &k8s.K8sClient{Interface: fake.NewSimpleClientset()}
mounter := &mount.SafeFormatAndMount{
Interface: mount.New(""),
Exec: k8sexec.New(),
}
jfs := juicefs.NewJfsProvider(mounter, client)

juicefsDriver := &nodeService{
juicefs: nil,
juicefs: jfs,
nodeID: "fake_node_id",
k8sClient: &k8s.K8sClient{Interface: fake.NewSimpleClientset()},
k8sClient: client,
}

req := &csi.NodePublishVolumeRequest{
Expand Down
22 changes: 22 additions & 0 deletions pkg/juicefs/juicefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"k8s.io/klog"
k8sexec "k8s.io/utils/exec"
"k8s.io/utils/mount"
k8sMount "k8s.io/utils/mount"

"github.com/juicedata/juicefs-csi-driver/pkg/config"
podmount "github.com/juicedata/juicefs-csi-driver/pkg/juicefs/mount"
Expand All @@ -65,6 +66,7 @@ type Interface interface {
SetQuota(ctx context.Context, secrets map[string]string, jfsSetting *config.JfsSetting, quotaPath string, capacity int64) (string, error)
Settings(ctx context.Context, volumeID string, secrets, volCtx map[string]string, options []string) (*config.JfsSetting, error)
GetSubPath(ctx context.Context, volumeID string) (string, error)
CreateTarget(ctx context.Context, target string) error
}

type juicefs struct {
Expand Down Expand Up @@ -607,6 +609,26 @@ func (j *juicefs) JfsUnmount(ctx context.Context, volumeId, mountPath string) er
return nil
}

func (j *juicefs) CreateTarget(ctx context.Context, target string) error {
var corruptedMnt bool

for {
err := util.DoWithTimeout(ctx, defaultCheckTimeout, func() (err error) {
_, err = k8sMount.PathExists(target)
return
})
if err == nil {
return os.MkdirAll(target, os.FileMode(0755))
} else if corruptedMnt = k8sMount.IsCorruptedMnt(err); corruptedMnt {
// if target is a corrupted mount, umount it
util.UmountPath(ctx, target)
continue
} else {
return err
}
}
}

func (j *juicefs) JfsCleanupMountPoint(ctx context.Context, mountPath string) error {
klog.V(5).Infof("JfsCleanupMountPoint: clean up mount point: %q", mountPath)
return util.DoWithTimeout(ctx, 2*defaultCheckTimeout, func() (err error) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/juicefs/mocks/mock_juicefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/sanity/fake_juicefs_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type fakeJfsProvider struct {
fs map[string]fakeJfs
}

func (j *fakeJfsProvider) CreateTarget(ctx context.Context, target string) error {
return nil
}

func (j *fakeJfsProvider) Settings(ctx context.Context, volumeID string, secrets, volCtx map[string]string, options []string) (*config.JfsSetting, error) {
return new(config.JfsSetting), nil
}
Expand Down
Loading