Skip to content

Commit

Permalink
Merge pull request #4719 from AliyunContainerService/aliyun-vfs-impl
Browse files Browse the repository at this point in the history
Implement vfs with AlibabaCloud OSS
  • Loading branch information
k8s-ci-robot authored Apr 2, 2018
2 parents 8bbbc88 + d7c3f3b commit fcd0100
Show file tree
Hide file tree
Showing 29 changed files with 5,887 additions and 3 deletions.
13 changes: 12 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions util/pkg/vfs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ go_library(
"k8scontext.go",
"k8sfs.go",
"memfs.go",
"osscontext.go",
"ossfs.go",
"s3context.go",
"s3fs.go",
"sshfs.go",
Expand All @@ -28,6 +30,7 @@ go_library(
"//vendor/github.com/aws/aws-sdk-go/aws/session:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/s3:go_default_library",
"//vendor/github.com/denverdino/aliyungo/oss:go_default_library",
"//vendor/github.com/go-ini/ini:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/gophercloud/gophercloud:go_default_library",
Expand Down
33 changes: 33 additions & 0 deletions util/pkg/vfs/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync"
"time"

"github.com/denverdino/aliyungo/oss"
"github.com/golang/glog"
"github.com/gophercloud/gophercloud"
"golang.org/x/net/context"
Expand All @@ -46,6 +47,8 @@ type VFSContext struct {
gcsClient *storage.Service
// swiftClient is the openstack swift client
swiftClient *gophercloud.ServiceClient
// ossClient is the Aliyun Open Source Storage client
ossClient *oss.Client
}

var Context = VFSContext{
Expand Down Expand Up @@ -129,6 +132,10 @@ func (c *VFSContext) BuildVfsPath(p string) (Path, error) {
return c.buildOpenstackSwiftPath(p)
}

if strings.HasPrefix(p, "oss://") {
return c.buildOSSPath(p)
}

return nil, fmt.Errorf("unknown / unhandled path type: %q", p)
}

Expand Down Expand Up @@ -370,3 +377,29 @@ func (c *VFSContext) buildOpenstackSwiftPath(p string) (*SwiftPath, error) {

return NewSwiftPath(c.swiftClient, bucket, u.Path)
}

func (c *VFSContext) buildOSSPath(p string) (*OSSPath, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid aliyun oss path: %q", p)
}

if u.Scheme != "oss" {
return nil, fmt.Errorf("invalid aliyun oss path: %q", p)
}

bucket := strings.TrimSuffix(u.Host, "/")
if bucket == "" {
return nil, fmt.Errorf("invalid aliyun oss path: %q", p)
}

if c.ossClient == nil {
ossClient, err := NewAliOSSClient()
if err != nil {
return nil, err
}
c.ossClient = ossClient
}

return NewOSSPath(c.ossClient, bucket, u.Path)
}
78 changes: 78 additions & 0 deletions util/pkg/vfs/osscontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vfs

import (
"fmt"
"os"
"strings"

"github.com/denverdino/aliyungo/oss"
)

type aliyunOSSConfig struct {
region oss.Region
internal bool
accessKeyId string
accessKeySecret string
secure bool
}

func NewOSSPath(client *oss.Client, bucket string, key string) (*OSSPath, error) {
bucket = strings.TrimSuffix(bucket, "/")
key = strings.TrimPrefix(key, "/")

return &OSSPath{
client: client,
bucket: bucket,
key: key,
}, nil
}

func NewAliOSSClient() (*oss.Client, error) {
c := &aliyunOSSConfig{}
err := c.loadConfig()
if err != nil {
return nil, fmt.Errorf("error building aliyun oss client: %v", err)
}

return oss.NewOSSClient(c.region, c.internal, c.accessKeyId, c.accessKeySecret, c.secure), nil
}

func (c *aliyunOSSConfig) loadConfig() error {
c.region = oss.Region(os.Getenv("OSS_REGION"))
if c.region == "" {
// TODO: can we use default region?
return fmt.Errorf("OSS_REGION cannot be empty")
}
c.accessKeyId = os.Getenv("ALIYUN_ACCESS_KEY_ID")
if c.accessKeyId == "" {
return fmt.Errorf("ALIYUN_ACCESS_KEY_ID cannot be empty")
}
c.accessKeySecret = os.Getenv("ALIYUN_ACCESS_KEY_SECRET")
if c.accessKeySecret == "" {
return fmt.Errorf("ALIYUN_ACCESS_KEY_SECRET cannot be empty")
}
ossInternal := os.Getenv("ALIYUN_OSS_INTERNAL")
if ossInternal != "" {
c.internal = true
} else {
c.internal = false
}
c.secure = true
return nil
}
Loading

0 comments on commit fcd0100

Please sign in to comment.