-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
object_store.go
126 lines (107 loc) · 3.66 KB
/
object_store.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2018 The Kubeflow 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
//
// https://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 storage
import (
"bytes"
"path"
"regexp"
"github.com/kubeflow/pipelines/backend/src/common/util"
minio "github.com/minio/minio-go/v6"
"sigs.k8s.io/yaml"
)
const (
multipartDefaultSize = -1
)
// Interface for object store.
type ObjectStoreInterface interface {
AddFile(template []byte, filePath string) error
DeleteFile(filePath string) error
GetFile(filePath string) ([]byte, error)
AddAsYamlFile(o interface{}, filePath string) error
GetFromYamlFile(o interface{}, filePath string) error
GetPipelineKey(pipelineId string) string
}
// Managing pipeline using Minio.
type MinioObjectStore struct {
minioClient MinioClientInterface
bucketName string
baseFolder string
disableMultipart bool
}
// GetPipelineKey adds the configured base folder to pipeline id.
func (m *MinioObjectStore) GetPipelineKey(pipelineID string) string {
return path.Join(m.baseFolder, pipelineID)
}
func (m *MinioObjectStore) AddFile(file []byte, filePath string) error {
var parts int64
if m.disableMultipart {
parts = int64(len(file))
} else {
parts = multipartDefaultSize
}
_, err := m.minioClient.PutObject(
m.bucketName, filePath, bytes.NewReader(file),
parts, minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return util.NewInternalServerError(err, "Failed to store file %v", filePath)
}
return nil
}
func (m *MinioObjectStore) DeleteFile(filePath string) error {
err := m.minioClient.DeleteObject(m.bucketName, filePath)
if err != nil {
return util.NewInternalServerError(err, "Failed to delete file %v", filePath)
}
return nil
}
func (m *MinioObjectStore) GetFile(filePath string) ([]byte, error) {
reader, err := m.minioClient.GetObject(m.bucketName, filePath, minio.GetObjectOptions{})
if err != nil {
return nil, util.NewInternalServerError(err, "Failed to get file %v", filePath)
}
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
bytes := buf.Bytes()
// Remove single part signature if exists
if m.disableMultipart {
re := regexp.MustCompile(`\w+;chunk-signature=\w+`)
bytes = []byte(re.ReplaceAllString(string(bytes), ""))
}
return bytes, nil
}
func (m *MinioObjectStore) AddAsYamlFile(o interface{}, filePath string) error {
bytes, err := yaml.Marshal(o)
if err != nil {
return util.NewInternalServerError(err, "Failed to marshal file %v: %v", filePath, err.Error())
}
err = m.AddFile(bytes, filePath)
if err != nil {
return util.Wrap(err, "Failed to add a yaml file")
}
return nil
}
func (m *MinioObjectStore) GetFromYamlFile(o interface{}, filePath string) error {
bytes, err := m.GetFile(filePath)
if err != nil {
return util.Wrap(err, "Failed to read from a yaml file")
}
err = yaml.Unmarshal(bytes, o)
if err != nil {
return util.NewInternalServerError(err, "Failed to unmarshal file %v: %v", filePath, err.Error())
}
return nil
}
func NewMinioObjectStore(minioClient MinioClientInterface, bucketName string, baseFolder string, disableMultipart bool) *MinioObjectStore {
return &MinioObjectStore{minioClient: minioClient, bucketName: bucketName, baseFolder: baseFolder, disableMultipart: disableMultipart}
}