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

Mount using K8S Util Mounter #36

Merged
merged 1 commit into from
May 3, 2020
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: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM golang:1.13.6-alpine3.11 AS build-gcsfuse
ARG gcsfuse_version
ARG global_ldflags

RUN apk add --update --no-cache fuse fuse-dev git upx
RUN apk add --update --no-cache fuse fuse-dev git

WORKDIR ${GOPATH}

Expand Down Expand Up @@ -67,4 +67,5 @@ ENTRYPOINT ["/usr/local/bin/driver"]

# Copy the binaries
COPY --from=compress-gcfsuse /tmp/bin/* /usr/local/bin/
COPY --from=build-gcsfuse /tmp/gcsfuse/sbin/* /sbin/
COPY --from=compress-csi-gcs /tmp/bin/* /usr/local/bin/
4 changes: 4 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"k8s.io/klog"

"github.com/ofek/csi-gcs/pkg/util"

"k8s.io/utils/mount"
)

type GCSDriver struct {
Expand All @@ -19,6 +21,7 @@ type GCSDriver struct {
mountPoint string
version string
server *grpc.Server
mounter mount.Interface
}

func NewGCSDriver(name, node, endpoint string, version string) (*GCSDriver, error) {
Expand All @@ -28,6 +31,7 @@ func NewGCSDriver(name, node, endpoint string, version string) (*GCSDriver, erro
endpoint: endpoint,
mountPoint: BucketMountPath,
version: version,
mounter: mount.New(""),
}, nil
}

Expand Down
192 changes: 0 additions & 192 deletions pkg/driver/mounter.go

This file was deleted.

67 changes: 51 additions & 16 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package driver

import (
"context"
"fmt"
"os"
"strconv"
"strings"

"cloud.google.com/go/storage"
"github.com/container-storage-interface/spec/lib/go/csi"
Expand All @@ -11,6 +14,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
"k8s.io/utils/mount"

"github.com/ofek/csi-gcs/pkg/flags"
"github.com/ofek/csi-gcs/pkg/util"
Expand Down Expand Up @@ -59,7 +63,6 @@ func (driver *GCSDriver) NodePublishVolume(ctx context.Context, req *csi.NodePub
if err != nil {
return nil, err
}
defer util.CleanupKey(keyFile, KeyStoragePath)

// Creates a client.
client, err := storage.NewClient(ctx, option.WithCredentialsFile(keyFile))
Expand All @@ -78,20 +81,37 @@ func (driver *GCSDriver) NodePublishVolume(ctx context.Context, req *csi.NodePub
return nil, status.Errorf(codes.NotFound, "Bucket %s does not exist", options[flags.FLAG_BUCKET])
}

notMnt, err := CheckMount(req.TargetPath)
notMnt, err := driver.mounter.IsLikelyNotMountPoint(req.TargetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
if os.IsNotExist(err) {
if err := os.MkdirAll(req.TargetPath, 0750); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
notMnt = true
} else {
return nil, status.Error(codes.Internal, err.Error())
}
}

if !notMnt {
return &csi.NodePublishVolumeResponse{}, nil
}

mounter, err := NewGcsFuseMounter(options[flags.FLAG_BUCKET], keyFile, flags.ExtraFlags(options))
if err != nil {
return nil, err
mountOptions := []string{fmt.Sprintf("key_file=%s", keyFile), "allow_other"}
mountOptions = append(mountOptions, flags.ExtraFlags(options)...)
if req.GetReadonly() {
mountOptions = append(mountOptions, "ro")
}
if err := mounter.Mount(req.TargetPath); err != nil {
return nil, err
err = driver.mounter.Mount(options[flags.FLAG_BUCKET], req.TargetPath, "gcsfuse", mountOptions)

if err != nil {
if os.IsPermission(err) {
return nil, status.Error(codes.PermissionDenied, err.Error())
}
if strings.Contains(err.Error(), "invalid argument") {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}

return &csi.NodePublishVolumeResponse{}, nil
Expand All @@ -108,15 +128,22 @@ func (driver *GCSDriver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeU
return nil, status.Error(codes.InvalidArgument, "Target path missing in request")
}

notMount, err := CheckMount(req.GetTargetPath())
if err != nil || notMount {
return &csi.NodeUnpublishVolumeResponse{}, nil
notMnt, err := driver.mounter.IsLikelyNotMountPoint(req.TargetPath)

if err != nil {
if os.IsNotExist(err) {
return nil, status.Error(codes.NotFound, "Targetpath not found")
}
return nil, status.Error(codes.Internal, err.Error())
}
if notMnt {
return nil, status.Error(codes.NotFound, "Volume not mounted")
}

if err := FuseUnmount(req.GetTargetPath()); err != nil {
err = mount.CleanupMountPoint(req.GetTargetPath(), driver.mounter, false)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
klog.V(4).Infof("bucket %s has been unmounted.", req.GetVolumeId())

return &csi.NodeUnpublishVolumeResponse{}, nil
}
Expand Down Expand Up @@ -170,9 +197,17 @@ func (driver *GCSDriver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpa
return nil, status.Error(codes.InvalidArgument, "Volume path missing in request")
}

notMount, err := CheckMount(req.GetVolumePath())
if err != nil || notMount {
return nil, status.Error(codes.NotFound, "Volume is not mounted")
notMnt, err := driver.mounter.IsLikelyNotMountPoint(req.GetVolumePath())

if err != nil {
if os.IsNotExist(err) {
return nil, status.Error(codes.NotFound, "Targetpath not found")
} else {
return nil, status.Error(codes.Internal, err.Error())
}
}
if notMnt {
return nil, status.Error(codes.NotFound, "Volume not mounted")
}

return &csi.NodeExpandVolumeResponse{}, nil
Expand Down
Loading