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

Suport to Minio as slug storage backend #236

Merged
merged 3 commits into from
Aug 3, 2017
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: 3 additions & 0 deletions pkg/server/deploy/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func newPodSpec(name, image string, a *app.App, envVars map[string]string, fileS
for _, e := range a.EnvVars {
ps.Env[e.Key] = e.Value
}
for k, v := range fileStorage.PodEnvVars() {
ps.Env[k] = v
}
return ps
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/server/storage/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (f *fake) Type() string {
return string(FakeType)
}

func (f *fake) PodEnvVars() map[string]string {
return make(map[string]string)
}

func NewFake() Storage {
return &fake{
Key: "key",
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/storage/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ func TestFakeUploadFile(t *testing.T) {
t.Errorf("expected no error, got %v", err)
}
}

func TestFakePodEnvVars(t *testing.T) {
fake := NewFake()
ev := fake.PodEnvVars()
if len(ev) != 0 {
t.Errorf("expected 0, got %d", len(ev))
}
}
33 changes: 33 additions & 0 deletions pkg/server/storage/minio.go
Original file line number Diff line number Diff line change
@@ -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}
}
55 changes: 55 additions & 0 deletions pkg/server/storage/minio_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
4 changes: 4 additions & 0 deletions pkg/server/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (s *S3) Type() string {
return string(S3Type)
}

func (s *S3) PodEnvVars() map[string]string {
return make(map[string]string)
}

func newS3(conf *Config) Storage {
st := &S3{
Key: conf.AwsKey,
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ func TestS3UploadFile(t *testing.T) {
t.Errorf("expected no error, got %v", err)
}
}

func TestS3PodEnvVars(t *testing.T) {
s3 := newS3(&Config{})
ev := s3.PodEnvVars()
if len(ev) != 0 {
t.Errorf("expected 0, got %d", len(ev))
}
}
14 changes: 10 additions & 4 deletions pkg/server/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,11 +28,16 @@ type Storage interface {
AccessData() map[string][]byte
UploadFile(path string, file io.ReadSeeker) error
Type() string
PodEnvVars() map[string]string
}

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
}