This is a huge commit, it does following things:
1. refactor kubesphere dependency service client creation, we can disable dependency by config 2. dependencies can be configured by configuration file 3. refactor cmd package using cobra.Command, so we can use hypersphere to invoke command sepearately. Later we only need to build one image to contains all kubesphere core components. One command to rule them all! 4. live reloading configuration currently not implemented
This commit is contained in:
@@ -1,83 +1,79 @@
|
||||
package s2is3
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"k8s.io/klog"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
s3Region string
|
||||
s3Endpoint string
|
||||
s3DisableSSL bool
|
||||
s3ForcePathStyle bool
|
||||
s3AccessKeyID string
|
||||
s3SecretAccessKey string
|
||||
s3SessionToken string
|
||||
s3Bucket string
|
||||
)
|
||||
var (
|
||||
s2iS3 *s3.S3
|
||||
s2iS3Session *session.Session
|
||||
sessionInitMutex sync.Mutex
|
||||
clientInitMutex sync.Mutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&s3Region, "s2i-s3-region", "us-east-1", "region of s2i s3")
|
||||
flag.StringVar(&s3Endpoint, "s2i-s3-endpoint", "http://ks-minio.kubesphere-system.svc", "endpoint of s2i s3")
|
||||
flag.StringVar(&s3AccessKeyID, "s2i-s3-access-key-id", "AKIAIOSFODNN7EXAMPLE", "access key of s2i s3")
|
||||
flag.StringVar(&s3SecretAccessKey, "s2i-s3-secret-access-key", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "secret access key of s2i s3")
|
||||
flag.StringVar(&s3SessionToken, "s2i-s3-session-token", "", "session token of s2i s3")
|
||||
flag.StringVar(&s3Bucket, "s2i-s3-bucket", "s2i-binaries", "bucket name of s2i s3")
|
||||
flag.BoolVar(&s3DisableSSL, "s2i-s3-disable-SSL", true, "disable ssl")
|
||||
flag.BoolVar(&s3ForcePathStyle, "s2i-s3-force-path-style", true, "force path style")
|
||||
type S3Client struct {
|
||||
s3Client *s3.S3
|
||||
s3Session *session.Session
|
||||
bucket string
|
||||
}
|
||||
|
||||
func Client() *s3.S3 {
|
||||
if s2iS3 != nil {
|
||||
return s2iS3
|
||||
func NewS3Client(options *S3Options) (*S3Client, error) {
|
||||
cred := credentials.NewStaticCredentials(options.AccessKeyID, options.SecretAccessKey, options.SessionToken)
|
||||
|
||||
config := aws.Config{
|
||||
Region: aws.String(options.Region),
|
||||
Endpoint: aws.String(options.Endpoint),
|
||||
DisableSSL: aws.Bool(options.DisableSSL),
|
||||
S3ForcePathStyle: aws.Bool(options.ForcePathStyle),
|
||||
Credentials: cred,
|
||||
}
|
||||
clientInitMutex.Lock()
|
||||
defer clientInitMutex.Unlock()
|
||||
if s2iS3Session == nil {
|
||||
if sess := Session(); sess != nil {
|
||||
klog.Error("failed to connect to s2i s3")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
s2iS3 = s3.New(s2iS3Session)
|
||||
return s2iS3
|
||||
}
|
||||
func Session() *session.Session {
|
||||
if s2iS3Session != nil {
|
||||
return s2iS3Session
|
||||
}
|
||||
sessionInitMutex.Lock()
|
||||
defer sessionInitMutex.Unlock()
|
||||
creds := credentials.NewStaticCredentials(
|
||||
s3AccessKeyID, s3SecretAccessKey, s3SessionToken,
|
||||
)
|
||||
config := &aws.Config{
|
||||
Region: aws.String(s3Region),
|
||||
Endpoint: aws.String(s3Endpoint),
|
||||
DisableSSL: aws.Bool(s3DisableSSL),
|
||||
S3ForcePathStyle: aws.Bool(s3ForcePathStyle),
|
||||
Credentials: creds,
|
||||
}
|
||||
sess, err := session.NewSession(config)
|
||||
|
||||
s, err := session.NewSession(&config)
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connect to s2i s3: %+v", err)
|
||||
return nil
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
s2iS3Session = sess
|
||||
return s2iS3Session
|
||||
|
||||
var c S3Client
|
||||
|
||||
c.s3Client = s3.New(s)
|
||||
c.s3Session = s
|
||||
c.bucket = options.Bucket
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func Bucket() *string {
|
||||
return aws.String(s3Bucket)
|
||||
// NewS3ClientOrDie creates S3Client and panics if there is an error
|
||||
func NewS3ClientOrDie(options *S3Options) *S3Client {
|
||||
cred := credentials.NewStaticCredentials(options.AccessKeyID, options.SecretAccessKey, options.SessionToken)
|
||||
|
||||
config := aws.Config{
|
||||
Region: aws.String(options.Region),
|
||||
Endpoint: aws.String(options.Endpoint),
|
||||
DisableSSL: aws.Bool(options.DisableSSL),
|
||||
S3ForcePathStyle: aws.Bool(options.ForcePathStyle),
|
||||
Credentials: cred,
|
||||
}
|
||||
|
||||
s, err := session.NewSession(&config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client := s3.New(s)
|
||||
|
||||
return &S3Client{
|
||||
s3Client: client,
|
||||
s3Session: s,
|
||||
bucket: options.Bucket,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *S3Client) Client() *s3.S3 {
|
||||
|
||||
return s.s3Client
|
||||
}
|
||||
func (s *S3Client) Session() *session.Session {
|
||||
return s.s3Session
|
||||
}
|
||||
|
||||
func (s *S3Client) Bucket() *string {
|
||||
return aws.String(s.bucket)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user