Commit 9d5d84eb authored by Cyrill Troxler's avatar Cyrill Troxler
Browse files

Refactor all mounters to use the mounter interface

parent 093c5bf5
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -55,20 +55,21 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
		return nil, err
	}
	if !exists {
		if err := cs.s3.client.createBucket(volumeID); err != nil {
		if err = cs.s3.client.createBucket(volumeID); err != nil {
			glog.V(3).Infof("failed to create volume: %v", err)
			return nil, err
		}
	}
	mounter := cs.s3.cfg.Mounter
	if mounter == "" {
		mounter = req.GetParameters()[mounterKey]
	mounterType := cs.s3.cfg.Mounter
	if mounterType == "" {
		mounterType = req.GetParameters()[mounterKey]
	}
	switch mounter {
	case s3qlMounter:
		if err := s3qlCreate(volumeID, cs.s3.cfg); err != nil {
	mounter, err := newMounter(mounterType, volumeID, cs.s3.cfg)
	if err != nil {
		return nil, err
	}
	if err := mounter.Format(); err != nil {
		return nil, err
	}

	glog.V(4).Infof("create volume %s", volumeID)

pkg/s3/goofys.go

deleted100644 → 0
+0 −37
Original line number Diff line number Diff line
package s3

import (
	"fmt"
	"os"

	"context"

	goofys "github.com/kahing/goofys/api"
)

const defaultRegion = "us-east-1"

func goofysMount(bucket string, cfg *Config, targetPath string) error {
	goofysCfg := &goofys.Config{
		MountPoint: targetPath,
		Endpoint:   cfg.Endpoint,
		Region:     cfg.Region,
		DirMode:    0755,
		FileMode:   0644,
		MountOptions: map[string]string{
			"allow_other": "",
		},
	}
	if cfg.Endpoint != "" {
		cfg.Region = defaultRegion
	}
	os.Setenv("AWS_ACCESS_KEY_ID", cfg.AccessKeyID)
	os.Setenv("AWS_SECRET_ACCESS_KEY", cfg.SecretAccessKey)

	_, _, err := goofys.Mount(context.Background(), bucket, goofysCfg)

	if err != nil {
		return fmt.Errorf("Error mounting via goofys: %s", err)
	}
	return nil
}

pkg/s3/mounter.go

0 → 100644
+34 −0
Original line number Diff line number Diff line
package s3

import "fmt"

// Mounter interface which can be implemented
// by the different mounter types
type Mounter interface {
	Format() error
	Mount(targetPath string) error
}

const (
	mounterKey        = "mounter"
	s3fsMounterType   = "s3fs"
	goofysMounterType = "goofys"
	s3qlMounterType   = "s3ql"
)

// newMounter returns a new mounter depending on the mounterType parameter
func newMounter(mounterType string, bucket string, cfg *Config) (Mounter, error) {
	switch mounterType {
	case "":
	case s3fsMounterType:
		return newS3fsMounter(bucket, cfg)

	case goofysMounterType:
		return newGoofysMounter(bucket, cfg)

	case s3qlMounterType:
		return newS3qlMounter(bucket, cfg)

	}
	return nil, fmt.Errorf("Error mounting bucket %s, invalid mounter specified: %s", bucket, mounterType)
}
+63 −0
Original line number Diff line number Diff line
package s3

import (
	"fmt"
	"os"

	"context"

	goofysApi "github.com/kahing/goofys/api"
)

const defaultRegion = "us-east-1"

// Implements Mounter
type goofysMounter struct {
	bucket          string
	endpoint        string
	region          string
	accessKeyID     string
	secretAccessKey string
}

func newGoofysMounter(bucket string, cfg *Config) (Mounter, error) {
	region := cfg.Region
	// if endpoint is set we need a default region
	if region == "" && cfg.Endpoint != "" {
		region = defaultRegion
	}
	return &goofysMounter{
		bucket:          bucket,
		endpoint:        cfg.Endpoint,
		region:          region,
		accessKeyID:     cfg.AccessKeyID,
		secretAccessKey: cfg.SecretAccessKey,
	}, nil
}

func (goofys *goofysMounter) Format() error {
	return nil
}

func (goofys *goofysMounter) Mount(targetPath string) error {
	goofysCfg := &goofysApi.Config{
		MountPoint: targetPath,
		Endpoint:   goofys.endpoint,
		Region:     goofys.region,
		DirMode:    0755,
		FileMode:   0644,
		MountOptions: map[string]string{
			"allow_other": "",
		},
	}

	os.Setenv("AWS_ACCESS_KEY_ID", goofys.accessKeyID)
	os.Setenv("AWS_SECRET_ACCESS_KEY", goofys.secretAccessKey)

	_, _, err := goofysApi.Mount(context.Background(), goofys.bucket, goofysCfg)

	if err != nil {
		return fmt.Errorf("Error mounting via goofys: %s", err)
	}
	return nil
}
+64 −0
Original line number Diff line number Diff line
@@ -6,17 +6,38 @@ import (
	"os/exec"
)

func s3fsMount(bucket string, cfg *Config, targetPath string) error {
	if err := writes3fsPass(cfg); err != nil {
// Implements Mounter
type s3fsMounter struct {
	bucket        string
	url           string
	region        string
	pwFileContent string
}

func newS3fsMounter(bucket string, cfg *Config) (Mounter, error) {
	return &s3fsMounter{
		bucket:        bucket,
		url:           cfg.Endpoint,
		region:        cfg.Region,
		pwFileContent: cfg.AccessKeyID + ":" + cfg.SecretAccessKey,
	}, nil
}

func (s3fs *s3fsMounter) Format() error {
	return nil
}

func (s3fs *s3fsMounter) Mount(targetPath string) error {
	if err := writes3fsPass(s3fs.pwFileContent); err != nil {
		return err
	}
	args := []string{
		fmt.Sprintf("%s", bucket),
		fmt.Sprintf("%s", s3fs.bucket),
		fmt.Sprintf("%s", targetPath),
		"-o", "sigv2",
		"-o", "use_path_request_style",
		"-o", fmt.Sprintf("url=%s", cfg.Endpoint),
		"-o", fmt.Sprintf("endpoint=%s", cfg.Region),
		"-o", fmt.Sprintf("url=%s", s3fs.url),
		"-o", fmt.Sprintf("endpoint=%s", s3fs.region),
		"-o", "allow_other",
		"-o", "mp_umask=000",
	}
@@ -28,13 +49,13 @@ func s3fsMount(bucket string, cfg *Config, targetPath string) error {
	return nil
}

func writes3fsPass(cfg *Config) error {
func writes3fsPass(pwFileContent string) error {
	pwFileName := fmt.Sprintf("%s/.passwd-s3fs", os.Getenv("HOME"))
	pwFile, err := os.OpenFile(pwFileName, os.O_RDWR|os.O_CREATE, 0600)
	if err != nil {
		return err
	}
	_, err = pwFile.WriteString(cfg.AccessKeyID + ":" + cfg.SecretAccessKey)
	_, err = pwFile.WriteString(pwFileContent)
	if err != nil {
		return err
	}
Loading