2018-07-14 08:48:22 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2018-07-27 10:56:28 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-07-14 08:48:22 +00:00
|
|
|
"fmt"
|
2018-07-27 10:56:28 +00:00
|
|
|
"io"
|
2018-07-14 08:48:22 +00:00
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/minio/minio-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-07-27 10:56:28 +00:00
|
|
|
metadataName = ".metadata.json"
|
2018-07-27 19:37:32 +00:00
|
|
|
fsPrefix = "csi-fs"
|
2018-07-14 08:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type s3Client struct {
|
2018-07-16 18:24:54 +00:00
|
|
|
cfg *Config
|
2018-07-14 08:48:22 +00:00
|
|
|
minio *minio.Client
|
|
|
|
}
|
|
|
|
|
2018-07-27 10:56:28 +00:00
|
|
|
type bucket struct {
|
|
|
|
Name string
|
2018-07-29 08:00:53 +00:00
|
|
|
Mounter string
|
2018-07-27 19:37:32 +00:00
|
|
|
FSPath string
|
2018-07-27 10:56:28 +00:00
|
|
|
CapacityBytes int64
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:24:54 +00:00
|
|
|
func newS3Client(cfg *Config) (*s3Client, error) {
|
2018-07-14 08:48:22 +00:00
|
|
|
var client = &s3Client{}
|
|
|
|
|
2018-07-16 18:24:54 +00:00
|
|
|
client.cfg = cfg
|
|
|
|
u, err := url.Parse(client.cfg.Endpoint)
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ssl := u.Scheme == "https"
|
|
|
|
endpoint := u.Hostname()
|
|
|
|
if u.Port() != "" {
|
|
|
|
endpoint = u.Hostname() + ":" + u.Port()
|
|
|
|
}
|
2019-06-30 15:32:59 +00:00
|
|
|
minioClient, err := minio.NewWithRegion(endpoint, client.cfg.AccessKeyID, client.cfg.SecretAccessKey, ssl, client.cfg.Region)
|
2018-07-14 08:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.minio = minioClient
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:19:02 +00:00
|
|
|
func newS3ClientFromSecrets(secrets map[string]string) (*s3Client, error) {
|
|
|
|
return newS3Client(&Config{
|
|
|
|
AccessKeyID: secrets["accessKeyID"],
|
|
|
|
SecretAccessKey: secrets["secretAccessKey"],
|
|
|
|
Region: secrets["region"],
|
|
|
|
Endpoint: secrets["endpoint"],
|
|
|
|
// Mounter is set in the volume preferences, not secrets
|
|
|
|
Mounter: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-14 08:48:22 +00:00
|
|
|
func (client *s3Client) bucketExists(bucketName string) (bool, error) {
|
|
|
|
return client.minio.BucketExists(bucketName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *s3Client) createBucket(bucketName string) error {
|
2018-07-16 18:24:54 +00:00
|
|
|
return client.minio.MakeBucket(bucketName, client.cfg.Region)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 19:37:32 +00:00
|
|
|
func (client *s3Client) createPrefix(bucketName string, prefix string) error {
|
|
|
|
_, err := client.minio.PutObject(bucketName, prefix+"/", bytes.NewReader([]byte("")), 0, minio.PutObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-14 08:48:22 +00:00
|
|
|
func (client *s3Client) removeBucket(bucketName string) error {
|
|
|
|
if err := client.emptyBucket(bucketName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.minio.RemoveBucket(bucketName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *s3Client) emptyBucket(bucketName string) error {
|
|
|
|
objectsCh := make(chan string)
|
|
|
|
var listErr error
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(objectsCh)
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
for object := range client.minio.ListObjects(bucketName, "", true, doneCh) {
|
|
|
|
if object.Err != nil {
|
|
|
|
listErr = object.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
objectsCh <- object.Key
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if listErr != nil {
|
|
|
|
glog.Error("Error listing objects", listErr)
|
|
|
|
return listErr
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
errorCh := client.minio.RemoveObjects(bucketName, objectsCh)
|
|
|
|
for e := range errorCh {
|
|
|
|
glog.Errorf("Failed to remove object %s, error: %s", e.ObjectName, e.Err)
|
|
|
|
}
|
|
|
|
if len(errorCh) != 0 {
|
|
|
|
return fmt.Errorf("Failed to remove all objects of bucket %s", bucketName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:01:54 +00:00
|
|
|
// ensure our prefix is also removed
|
|
|
|
return client.minio.RemoveObject(bucketName, fsPrefix)
|
2018-07-14 08:48:22 +00:00
|
|
|
}
|
2018-07-27 10:56:28 +00:00
|
|
|
|
|
|
|
func (client *s3Client) setBucket(bucket *bucket) error {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(b).Encode(bucket)
|
|
|
|
opts := minio.PutObjectOptions{ContentType: "application/json"}
|
|
|
|
_, err := client.minio.PutObject(bucket.Name, metadataName, b, int64(b.Len()), opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *s3Client) getBucket(bucketName string) (*bucket, error) {
|
|
|
|
opts := minio.GetObjectOptions{}
|
|
|
|
obj, err := client.minio.GetObject(bucketName, metadataName, opts)
|
|
|
|
if err != nil {
|
|
|
|
return &bucket{}, err
|
|
|
|
}
|
|
|
|
objInfo, err := obj.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return &bucket{}, err
|
|
|
|
}
|
|
|
|
b := make([]byte, objInfo.Size)
|
|
|
|
_, err = obj.Read(b)
|
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return &bucket{}, err
|
|
|
|
}
|
|
|
|
var meta bucket
|
|
|
|
err = json.Unmarshal(b, &meta)
|
|
|
|
return &meta, err
|
|
|
|
}
|