-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
gcs.go
306 lines (286 loc) · 8.58 KB
/
gcs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package gcs
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/argoproj/pkg/file"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2/google"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
waitutil "github.com/argoproj/argo-workflows/v3/util/wait"
"github.com/argoproj/argo-workflows/v3/workflow/artifacts/common"
)
// ArtifactDriver is a driver for GCS
type ArtifactDriver struct {
ServiceAccountKey string
}
var (
_ common.ArtifactDriver = &ArtifactDriver{}
defaultRetry = wait.Backoff{Duration: time.Second * 2, Factor: 2.0, Steps: 5, Jitter: 0.1}
)
// from https://github.com/googleapis/google-cloud-go/blob/master/storage/go110.go
func isTransientGCSErr(err error) bool {
if err == io.ErrUnexpectedEOF {
return true
}
switch e := err.(type) {
case *googleapi.Error:
// Retry on 429 and 5xx, according to
// https://cloud.google.com/storage/docs/exponential-backoff.
return e.Code == 429 || (e.Code >= 500 && e.Code < 600)
case *url.Error:
// Retry socket-level errors ECONNREFUSED and ENETUNREACH (from syscall).
// Unfortunately the error type is unexported, so we resort to string
// matching.
retriable := []string{"connection refused", "connection reset"}
for _, s := range retriable {
if strings.Contains(e.Error(), s) {
return true
}
}
case interface{ Temporary() bool }:
if e.Temporary() {
return true
}
}
if e, ok := err.(interface{ Unwrap() error }); ok {
return isTransientGCSErr(e.Unwrap())
}
return false
}
func (g *ArtifactDriver) newGCSClient() (*storage.Client, error) {
if g.ServiceAccountKey != "" {
return newGCSClientWithCredential(g.ServiceAccountKey)
}
// Assume it uses Workload Identity
return newGCSClientDefault()
}
func newGCSClientWithCredential(serviceAccountJSON string) (*storage.Client, error) {
ctx := context.Background()
creds, err := google.CredentialsFromJSON(ctx, []byte(serviceAccountJSON), storage.ScopeReadWrite)
if err != nil {
return nil, fmt.Errorf("GCS client CredentialsFromJSON: %v", err)
}
client, err := storage.NewClient(ctx, option.WithCredentials(creds))
if err != nil {
return nil, fmt.Errorf("GCS storage.NewClient with credential: %v", err)
}
return client, nil
}
func newGCSClientDefault() (*storage.Client, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("GCS storage.NewClient: %v", err)
}
return client, nil
}
// Load function downloads objects from GCS
func (g *ArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
err := waitutil.Backoff(defaultRetry,
func() (bool, error) {
log.Infof("GCS Load path: %s, key: %s", path, inputArtifact.GCS.Key)
gcsClient, err := g.newGCSClient()
if err != nil {
log.Warnf("Failed to create new GCS client: %v", err)
return isTransientGCSErr(err), err
}
defer gcsClient.Close()
err = downloadObjects(gcsClient, inputArtifact.GCS.Bucket, inputArtifact.GCS.Key, path)
if err != nil {
log.Warnf("Failed to download objects from GCS: %v", err)
return isTransientGCSErr(err), err
}
return true, nil
})
return err
}
// download all the objects of a key from the bucket
func downloadObjects(client *storage.Client, bucket, key, path string) error {
objNames, err := listByPrefix(client, bucket, key, "")
if err != nil {
return err
}
if len(objNames) < 1 {
msg := fmt.Sprintf("no results for key: %s", key)
return errors.New(errors.CodeNotFound, msg)
}
for _, objName := range objNames {
err = downloadObject(client, bucket, key, objName, path)
if err != nil {
return err
}
}
return nil
}
// download an object from the bucket
func downloadObject(client *storage.Client, bucket, key, objName, path string) error {
objPrefix := filepath.Clean(key)
if os.PathSeparator == '\\' {
objPrefix = strings.ReplaceAll(objPrefix, "\\", "/")
}
relObjPath := strings.TrimPrefix(objName, objPrefix)
localPath := filepath.Join(path, relObjPath)
objectDir, _ := filepath.Split(localPath)
if objectDir != "" {
if err := os.MkdirAll(objectDir, 0o700); err != nil {
return fmt.Errorf("mkdir %s: %v", objectDir, err)
}
}
ctx := context.Background()
rc, err := client.Bucket(bucket).Object(objName).NewReader(ctx)
if err != nil {
if err == storage.ErrObjectNotExist {
return errors.New(errors.CodeNotFound, err.Error())
}
return fmt.Errorf("new bucket reader: %v", err)
}
defer rc.Close()
out, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("os create %s: %v", localPath, err)
}
defer func() {
if err := out.Close(); err != nil {
log.Fatalf("Error closing file[%s]: %v", localPath, err)
}
}()
_, err = io.Copy(out, rc)
if err != nil {
return fmt.Errorf("io copy: %v", err)
}
return nil
}
// list all the object names of the prefix in the bucket
func listByPrefix(client *storage.Client, bucket, prefix, delim string) ([]string, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
it := client.Bucket(bucket).Objects(ctx, &storage.Query{
Prefix: prefix,
Delimiter: delim,
})
results := []string{}
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
results = append(results, attrs.Name)
}
return results, nil
}
// Save an artifact to GCS compliant storage, e.g., uploading a local file to GCS bucket
func (g *ArtifactDriver) Save(path string, outputArtifact *wfv1.Artifact) error {
err := waitutil.Backoff(defaultRetry,
func() (bool, error) {
log.Infof("GCS Save path: %s, key: %s", path, outputArtifact.GCS.Key)
client, err := g.newGCSClient()
if err != nil {
return isTransientGCSErr(err), err
}
defer client.Close()
err = uploadObjects(client, outputArtifact.GCS.Bucket, outputArtifact.GCS.Key, path)
if err != nil {
return isTransientGCSErr(err), err
}
return true, nil
})
return err
}
// list all the file relative paths under a dir
// path is suppoese to be a dir
// relPath is a given relative path to be inserted in front
func listFileRelPaths(path string, relPath string) ([]string, error) {
results := []string{}
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
fs, err := listFileRelPaths(path+file.Name()+string(os.PathSeparator), relPath+file.Name()+string(os.PathSeparator))
if err != nil {
return nil, err
}
results = append(results, fs...)
} else {
results = append(results, relPath+file.Name())
}
}
return results, nil
}
// upload a local file or dir to GCS
func uploadObjects(client *storage.Client, bucket, key, path string) error {
isDir, err := file.IsDirectory(path)
if err != nil {
return fmt.Errorf("test if %s is a dir: %v", path, err)
}
if isDir {
dirName := filepath.Clean(path) + string(os.PathSeparator)
keyPrefix := filepath.Clean(key) + "/"
fileRelPaths, err := listFileRelPaths(dirName, "")
if err != nil {
return err
}
for _, relPath := range fileRelPaths {
fullKey := keyPrefix + relPath
if os.PathSeparator == '\\' {
fullKey = strings.ReplaceAll(fullKey, "\\", "/")
}
err = uploadObject(client, bucket, fullKey, dirName+relPath)
if err != nil {
return fmt.Errorf("upload %s: %v", dirName+relPath, err)
}
}
} else {
objectKey := filepath.Clean(key)
if os.PathSeparator == '\\' {
objectKey = strings.ReplaceAll(objectKey, "\\", "/")
}
err = uploadObject(client, bucket, objectKey, path)
if err != nil {
return fmt.Errorf("upload %s: %v", path, err)
}
}
return nil
}
// upload an object to GCS
func uploadObject(client *storage.Client, bucket, key, localPath string) error {
f, err := os.Open(filepath.Clean(localPath))
if err != nil {
return fmt.Errorf("os open: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("Error closing file[%s]: %v", localPath, err)
}
}()
ctx := context.Background()
wc := client.Bucket(bucket).Object(key).NewWriter(ctx)
if _, err = io.Copy(wc, f); err != nil {
return fmt.Errorf("io copy: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("writer close: %v", err)
}
return nil
}
func (g *ArtifactDriver) ListObjects(artifact *wfv1.Artifact) ([]string, error) {
return nil, fmt.Errorf("ListObjects is currently not supported for this artifact type, but it will be in a future version")
}