2021-04-03 10:40:58 +00:00
|
|
|
package mounter
|
2018-07-21 11:35:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-03 10:40:58 +00:00
|
|
|
|
2021-08-24 10:33:26 +00:00
|
|
|
"github.com/yandex-cloud/k8s-csi-s3/pkg/s3"
|
2018-07-21 11:35:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements Mounter
|
|
|
|
type s3fsMounter struct {
|
2021-04-05 13:07:16 +00:00
|
|
|
meta *s3.FSMeta
|
2018-07-21 11:35:31 +00:00
|
|
|
url string
|
|
|
|
region string
|
|
|
|
pwFileContent string
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:08:48 +00:00
|
|
|
const (
|
|
|
|
s3fsCmd = "s3fs"
|
|
|
|
)
|
|
|
|
|
2021-04-05 13:07:16 +00:00
|
|
|
func newS3fsMounter(meta *s3.FSMeta, cfg *s3.Config) (Mounter, error) {
|
2018-07-21 11:35:31 +00:00
|
|
|
return &s3fsMounter{
|
2021-04-05 13:07:16 +00:00
|
|
|
meta: meta,
|
2018-07-21 11:35:31 +00:00
|
|
|
url: cfg.Endpoint,
|
|
|
|
region: cfg.Region,
|
|
|
|
pwFileContent: cfg.AccessKeyID + ":" + cfg.SecretAccessKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:43:51 +00:00
|
|
|
func (s3fs *s3fsMounter) Stage(stageTarget string) error {
|
2018-07-21 11:35:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:43:51 +00:00
|
|
|
func (s3fs *s3fsMounter) Unstage(stageTarget string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:12:41 +00:00
|
|
|
func (s3fs *s3fsMounter) Mount(source, target, volumeID string) error {
|
2018-07-21 11:35:31 +00:00
|
|
|
if err := writes3fsPass(s3fs.pwFileContent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args := []string{
|
2021-07-16 13:33:13 +00:00
|
|
|
fmt.Sprintf("%s:/%s", s3fs.meta.BucketName, s3fs.meta.Prefix),
|
2021-04-05 13:07:16 +00:00
|
|
|
target,
|
2018-07-21 11:35:31 +00:00
|
|
|
"-o", "use_path_request_style",
|
|
|
|
"-o", fmt.Sprintf("url=%s", s3fs.url),
|
|
|
|
"-o", "allow_other",
|
|
|
|
"-o", "mp_umask=000",
|
|
|
|
}
|
2021-09-07 10:37:04 +00:00
|
|
|
if s3fs.region != "" {
|
|
|
|
args = append(args, "-o", fmt.Sprintf("endpoint=%s", s3fs.region))
|
|
|
|
}
|
2021-07-16 13:12:57 +00:00
|
|
|
args = append(args, s3fs.meta.MountOptions...)
|
2018-07-26 20:43:51 +00:00
|
|
|
return fuseMount(target, s3fsCmd, args)
|
2018-07-21 11:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(pwFileContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pwFile.Close()
|
|
|
|
return nil
|
|
|
|
}
|