This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
/
aliyun-oss.go
78 lines (69 loc) · 2.53 KB
/
aliyun-oss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Copyright 2019 BlackRock, Inc.
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 context
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/ocibuilder/ocibuilder/pkg/apis/ocibuilder/v1alpha1"
"github.com/ocibuilder/ocibuilder/pkg/common"
"github.com/ocibuilder/ocibuilder/pkg/util"
"k8s.io/client-go/kubernetes"
)
// AliyunOSSBuildContextReader implements BuildContextReader to fetch build context stored on Aliyun OSS
type AliyunOSSBuildContextReader struct {
// buildContext stored on OSS
buildContext *v1alpha1.AliyunOSSContext
// k8sClient is Kubernetes client
k8sClient kubernetes.Interface
}
// Read reads and stores build context from OSS
func (contextReader *AliyunOSSBuildContextReader) Read() (string, error) {
accessId, err := util.ReadCredentials(contextReader.k8sClient, contextReader.buildContext.AccessId)
if err != nil {
return "", err
}
accessSecret, err := util.ReadCredentials(contextReader.k8sClient, contextReader.buildContext.AccessSecret)
if err != nil {
return "", err
}
client, err := oss.New(contextReader.buildContext.Endpoint, accessId, accessSecret)
if err != nil {
return "", err
}
bucket, err := client.Bucket(contextReader.buildContext.Bucket.Name)
if err != nil {
return "", err
}
contextFilePath := fmt.Sprintf("%s/%s", common.ContextDirectory, common.ContextFile)
if err := os.MkdirAll(common.ContextDirectory, 0750); err != nil {
return "", err
}
if _, err := os.Create(contextFilePath); err != nil {
return "", err
}
if err := bucket.GetObjectToFile(contextReader.buildContext.Bucket.Key, contextFilePath); err != nil {
return "", err
}
if err := util.UntarFile(contextFilePath, common.ContextDirectoryUncompressed); err != nil {
return "", err
}
return common.ContextDirectoryUncompressed, nil
}
// NewAliyunOSSBuildContextReader returns a new Aliyun OSS build context reader
func NewAliyunOSSBuildContextReader(buildContext *v1alpha1.AliyunOSSContext, k8sClient kubernetes.Interface) *AliyunOSSBuildContextReader {
return &AliyunOSSBuildContextReader{
buildContext,
k8sClient,
}
}