diff --git a/pkg/server/storage/minio.go b/pkg/server/storage/minio.go new file mode 100644 index 000000000..d7a58eb18 --- /dev/null +++ b/pkg/server/storage/minio.go @@ -0,0 +1,33 @@ +package storage + +import "strings" + +type Minio struct { + Storage +} + +func (m *Minio) Type() string { + return string(MinioType) +} + +func (m *Minio) PodEnvVars() map[string]string { + s3 := m.Storage.(*S3) + endpoint := strings.Split(trimEndpoint(s3.Endpoint), ":") + return map[string]string{ + "S3_HOST": endpoint[0], + "S3_PORT": endpoint[1], + "MINIO_BUCKET": s3.Bucket, + } +} + +func trimEndpoint(endpoint string) string { + for _, s := range []string{"http://", "https://"} { + endpoint = strings.TrimPrefix(endpoint, s) + } + return endpoint +} + +func newMinio(conf *Config) Storage { + s3 := newS3(conf) + return &Minio{Storage: s3} +} diff --git a/pkg/server/storage/minio_test.go b/pkg/server/storage/minio_test.go new file mode 100644 index 000000000..1f942408a --- /dev/null +++ b/pkg/server/storage/minio_test.go @@ -0,0 +1,55 @@ +package storage + +import ( + "fmt" + "testing" +) + +func TestMinioType(t *testing.T) { + minio := newMinio(&Config{}) + if tmp := minio.Type(); tmp != "minio" { + t.Errorf("expected minio, got %s", tmp) + } +} + +func TestMinioPodEnvVars(t *testing.T) { + expectedHost := "url" + expectedPort := "9000" + expectedBucket := "test" + minio := newMinio(&Config{ + AwsEndpoint: fmt.Sprintf("http://%s:%s", expectedHost, expectedPort), + AwsBucket: expectedBucket, + }) + ev := minio.PodEnvVars() + + var testCases = []struct { + env string + expected string + }{ + {"S3_HOST", expectedHost}, + {"S3_PORT", expectedPort}, + {"MINIO_BUCKET", expectedBucket}, + } + + for _, tc := range testCases { + if tmp := ev[tc.env]; tmp != tc.expected { + t.Errorf("expected %s, got %s", tc.expected, tc.env) + } + } +} + +func TestTrimEndpoint(t *testing.T) { + var testCases = []struct { + input string + expected string + }{ + {"http://foo.com", "foo.com"}, + {"https://teresa.io", "teresa.io"}, + } + + for _, tc := range testCases { + if actual := trimEndpoint(tc.input); actual != tc.expected { + t.Errorf("expected %s, got %s", tc.expected, actual) + } + } +} diff --git a/pkg/server/storage/storage.go b/pkg/server/storage/storage.go index 2c75997a0..0401ba67f 100644 --- a/pkg/server/storage/storage.go +++ b/pkg/server/storage/storage.go @@ -7,8 +7,9 @@ import ( type storageType string const ( - S3Type storageType = "s3" - FakeType storageType = "fake" + S3Type storageType = "s3" + MinioType storageType = "minio" + FakeType storageType = "fake" ) type Config struct { @@ -31,8 +32,12 @@ type Storage interface { } func New(conf *Config) (Storage, error) { - if conf.Type != S3Type { + switch conf.Type { + case S3Type: + return newS3(conf), nil + case MinioType: + return newMinio(conf), nil + default: return nil, ErrInvalidStorageType } - return newS3(conf), nil }