-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backend storage local, mysql, oss, s3 (#894)
- Loading branch information
Showing
11 changed files
with
300 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package storages | ||
|
||
import ( | ||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
// LocalStorage is an implementation of backend.Backend which uses local filesystem as storage. | ||
type LocalStorage struct { | ||
// path is the directory to store the files. If empty, use the default storage path, which depends on | ||
// the object it's used to store. | ||
path string | ||
} | ||
|
||
func NewLocalStorage(config *v1.BackendLocalConfig) *LocalStorage { | ||
return &LocalStorage{path: config.Path} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package storages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
func TestNewLocalStorage(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
config *v1.BackendLocalConfig | ||
storage *LocalStorage | ||
}{ | ||
{ | ||
name: "new local storage successfully", | ||
config: &v1.BackendLocalConfig{Path: "etc"}, | ||
storage: &LocalStorage{path: "etc"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
storage := NewLocalStorage(tc.config) | ||
assert.Equal(t, tc.storage, storage) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package storages | ||
|
||
import ( | ||
"strconv" | ||
|
||
gomysql "github.com/go-sql-driver/mysql" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
// MysqlStorage is an implementation of backend.Backend which uses mysql as storage. | ||
type MysqlStorage struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewMysqlStorage(config *v1.BackendMysqlConfig) (*MysqlStorage, error) { | ||
c := gomysql.NewConfig() | ||
c.User = config.User | ||
c.Passwd = config.Password | ||
c.Addr = config.Host + ":" + strconv.Itoa(config.Port) | ||
c.DBName = config.DBName | ||
c.Net = "tcp" | ||
c.ParseTime = true | ||
c.InterpolateParams = true | ||
c.Params = map[string]string{ | ||
"charset": "utf8", | ||
"loc": "Asia/Shanghai", | ||
} | ||
db, err := gorm.Open(mysql.Open(c.FormatDSN()), &gorm.Config{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MysqlStorage{db: db}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package storages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
func TestNewMysqlStorage(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
success bool | ||
config *v1.BackendMysqlConfig | ||
}{ | ||
{ | ||
name: "new mysql storage successfully", | ||
success: true, | ||
config: &v1.BackendMysqlConfig{ | ||
DBName: "kusion", | ||
User: "kk", | ||
Host: "127.0.0.1", | ||
Port: 3306, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockey.PatchConvey("mock gorm db", t, func() { | ||
mockey.Mock(gorm.Open).Return(&gorm.DB{}, nil).Build() | ||
_, err := NewMysqlStorage(tc.config) | ||
assert.Equal(t, tc.success, err == nil) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package storages | ||
|
||
import ( | ||
"github.com/aliyun/aliyun-oss-go-sdk/oss" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
// OssStorage is an implementation of backend.Backend which uses oss as storage. | ||
type OssStorage struct { | ||
bucket *oss.Bucket | ||
|
||
// prefix will be added to the object storage key, so that all the files are stored under the prefix. | ||
prefix string | ||
} | ||
|
||
func NewOssStorage(config *v1.BackendOssConfig) (*OssStorage, error) { | ||
client, err := oss.New(config.Endpoint, config.AccessKeyID, config.AccessKeySecret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bucket, err := client.Bucket(config.Bucket) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &OssStorage{bucket: bucket, prefix: config.Prefix}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package storages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aliyun/aliyun-oss-go-sdk/oss" | ||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
func TestNewOssStorage(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
success bool | ||
config *v1.BackendOssConfig | ||
}{ | ||
{ | ||
name: "new oss storage successfully", | ||
success: true, | ||
config: &v1.BackendOssConfig{ | ||
GenericBackendObjectStorageConfig: &v1.GenericBackendObjectStorageConfig{ | ||
Endpoint: "http://oss-cn-hangzhou.aliyuncs.com", | ||
AccessKeyID: "fake-access-key-id", | ||
AccessKeySecret: "fake-access-key-secret", | ||
Bucket: "kusion", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockey.PatchConvey("mock oss client", t, func() { | ||
mockey.Mock(oss.New).Return(&oss.Client{}, nil).Build() | ||
_, err := NewOssStorage(tc.config) | ||
assert.Equal(t, tc.success, err == nil) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package storages | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
// S3Storage is an implementation of backend.Backend which uses s3 as storage. | ||
type S3Storage struct { | ||
sess *session.Session | ||
bucket string | ||
|
||
// prefix will be added to the object storage key, so that all the files are stored under the prefix. | ||
prefix string | ||
} | ||
|
||
func NewS3Storage(config *v1.BackendS3Config) (*S3Storage, error) { | ||
c := &aws.Config{ | ||
Credentials: credentials.NewStaticCredentials(config.AccessKeyID, config.AccessKeySecret, ""), | ||
Region: aws.String(config.Region), | ||
DisableSSL: aws.Bool(true), | ||
S3ForcePathStyle: aws.Bool(false), | ||
} | ||
if config.Endpoint != "" { | ||
c.Endpoint = aws.String(config.Endpoint) | ||
} | ||
sess, err := session.NewSession(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &S3Storage{ | ||
sess: sess, | ||
bucket: config.Bucket, | ||
prefix: config.Prefix, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package storages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/core/v1" | ||
) | ||
|
||
func TestNewS3Storage(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
success bool | ||
config *v1.BackendS3Config | ||
}{ | ||
{ | ||
name: "new S3 storage successfully", | ||
success: true, | ||
config: &v1.BackendS3Config{ | ||
GenericBackendObjectStorageConfig: &v1.GenericBackendObjectStorageConfig{ | ||
AccessKeyID: "fake-access-key-id", | ||
AccessKeySecret: "fake-access-key-secret", | ||
Bucket: "kusion", | ||
}, | ||
Region: "us-east-1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockey.PatchConvey("mock s3 session", t, func() { | ||
mockey.Mock(session.NewSession).Return(&session.Session{}, nil).Build() | ||
_, err := NewS3Storage(tc.config) | ||
assert.Equal(t, tc.success, err == nil) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.