forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageartifact.go
300 lines (273 loc) · 7.79 KB
/
storageartifact.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
/*
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 spyglass
import (
"context"
"fmt"
"io"
"io/ioutil"
"sync"
pkgio "k8s.io/test-infra/prow/io"
"k8s.io/test-infra/prow/spyglass/lenses"
)
// StorageArtifact represents some output of a prow job stored in GCS
type StorageArtifact struct {
// The handle of the object in GCS
handle artifactHandle
// The link to the Artifact in GCS
link string
// The path of the Artifact within the job
path string
// sizeLimit is the max size to read before failing
sizeLimit int64
// ctx provides context for cancellation and timeout. Embedded in struct to preserve
// conformance with io.ReaderAt
ctx context.Context
attrs *pkgio.Attributes
lock sync.RWMutex
}
type artifactHandle interface {
Attrs(ctx context.Context) (pkgio.Attributes, error)
NewRangeReader(ctx context.Context, offset, length int64) (io.ReadCloser, error)
NewReader(ctx context.Context) (io.ReadCloser, error)
UpdateAttrs(context.Context, pkgio.ObjectAttrsToUpdate) (*pkgio.Attributes, error)
}
// NewStorageArtifact returns a new StorageArtifact with a given handle, canonical link, and path within the job
func NewStorageArtifact(ctx context.Context, handle artifactHandle, link string, path string, sizeLimit int64) *StorageArtifact {
return &StorageArtifact{
handle: handle,
link: link,
path: path,
sizeLimit: sizeLimit,
ctx: ctx,
}
}
func (a *StorageArtifact) fetchAttrs() (*pkgio.Attributes, error) {
a.lock.RLock()
attrs := a.attrs
a.lock.RUnlock()
if attrs != nil {
return attrs, nil
}
if a.attrs != nil {
return a.attrs, nil
}
{
attrs, err := a.handle.Attrs(a.ctx)
if err != nil {
return nil, err
}
a.lock.Lock()
defer a.lock.Unlock()
a.attrs = &attrs
}
return a.attrs, nil
}
// Size returns the size of the artifact in GCS
func (a *StorageArtifact) Size() (int64, error) {
attrs, err := a.fetchAttrs()
if err != nil {
return 0, fmt.Errorf("error getting gcs attributes for artifact: %w", err)
}
return attrs.Size, nil
}
func (a *StorageArtifact) Metadata() (map[string]string, error) {
attrs, err := a.fetchAttrs()
if err != nil {
return nil, fmt.Errorf("fetch attributes: %w", err)
}
return attrs.Metadata, nil
}
func (a *StorageArtifact) UpdateMetadata(meta map[string]string) error {
attrs, err := a.handle.UpdateAttrs(a.ctx, pkgio.ObjectAttrsToUpdate{
Metadata: meta,
})
if err != nil {
return err
}
a.lock.Lock()
a.attrs = attrs
a.lock.Unlock()
return nil
}
// JobPath gets the GCS path of the artifact within the current job
func (a *StorageArtifact) JobPath() string {
return a.path
}
// CanonicalLink gets the GCS web address of the artifact
func (a *StorageArtifact) CanonicalLink() string {
return a.link
}
// ReadAt reads len(p) bytes from a file in GCS at offset off
func (a *StorageArtifact) ReadAt(p []byte, off int64) (n int, err error) {
if int64(len(p)) > a.sizeLimit {
return 0, lenses.ErrRequestSizeTooLarge
}
gzipped, err := a.gzipped()
if err != nil {
return 0, fmt.Errorf("error checking artifact for gzip compression: %w", err)
}
if gzipped {
return 0, lenses.ErrGzipOffsetRead
}
artifactSize, err := a.Size()
if err != nil {
return 0, fmt.Errorf("error getting artifact size: %w", err)
}
if off >= artifactSize {
return 0, fmt.Errorf("offset must be less than artifact size")
}
var gotEOF bool
toRead := int64(len(p))
if toRead+off > artifactSize {
return 0, fmt.Errorf("read range exceeds artifact contents")
} else if toRead+off == artifactSize {
gotEOF = true
}
reader, err := a.handle.NewRangeReader(a.ctx, off, toRead)
if err != nil {
return 0, fmt.Errorf("error getting artifact reader: %w", err)
}
defer reader.Close()
// We need to keep reading until we fill the buffer or hit EOF.
offset := 0
for offset < len(p) {
n, err = reader.Read(p[offset:])
offset += n
if err != nil {
if err == io.EOF && gotEOF {
break
}
return 0, fmt.Errorf("error reading from artifact: %w", err)
}
}
if gotEOF {
return offset, io.EOF
}
return offset, nil
}
// ReadAtMost reads at most n bytes from a file in GCS. If the file is compressed (gzip) in GCS, n bytes
// of gzipped content will be downloaded and decompressed into potentially GREATER than n bytes of content.
func (a *StorageArtifact) ReadAtMost(n int64) ([]byte, error) {
if n > a.sizeLimit {
return nil, lenses.ErrRequestSizeTooLarge
}
var reader io.ReadCloser
var p []byte
gzipped, err := a.gzipped()
if err != nil {
return nil, fmt.Errorf("error checking artifact for gzip compression: %w", err)
}
if gzipped {
reader, err = a.handle.NewReader(a.ctx)
if err != nil {
return nil, fmt.Errorf("error getting artifact reader: %w", err)
}
defer reader.Close()
p, err = ioutil.ReadAll(reader) // Must readall for gzipped files
if err != nil {
return nil, fmt.Errorf("error reading all from artifact: %w", err)
}
artifactSize := int64(len(p))
readRange := n
if n > artifactSize {
readRange = artifactSize
return p[:readRange], io.EOF
}
return p[:readRange], nil
}
artifactSize, err := a.Size()
if err != nil {
return nil, fmt.Errorf("error getting artifact size: %w", err)
}
readRange := n
var gotEOF bool
if n > artifactSize {
gotEOF = true
readRange = artifactSize
}
reader, err = a.handle.NewRangeReader(a.ctx, 0, readRange)
if err != nil {
return nil, fmt.Errorf("error getting artifact reader: %w", err)
}
defer reader.Close()
p, err = ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("error reading all from artifact: %w", err)
}
if gotEOF {
return p, io.EOF
}
return p, nil
}
// ReadAll will either read the entire file or throw an error if file size is too big
func (a *StorageArtifact) ReadAll() ([]byte, error) {
size, err := a.Size()
if err != nil {
return nil, fmt.Errorf("error getting artifact size: %w", err)
}
if size > a.sizeLimit {
return nil, lenses.ErrFileTooLarge
}
reader, err := a.handle.NewReader(a.ctx)
if err != nil {
return nil, fmt.Errorf("error getting artifact reader: %w", err)
}
defer reader.Close()
p, err := ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("error reading all from artifact: %w", err)
}
return p, nil
}
// ReadTail reads the last n bytes from a file in GCS
func (a *StorageArtifact) ReadTail(n int64) ([]byte, error) {
if n > a.sizeLimit {
return nil, lenses.ErrRequestSizeTooLarge
}
gzipped, err := a.gzipped()
if err != nil {
return nil, fmt.Errorf("error checking artifact for gzip compression: %w", err)
}
if gzipped {
return nil, lenses.ErrGzipOffsetRead
}
size, err := a.Size()
if err != nil {
return nil, fmt.Errorf("error getting artifact size: %w", err)
}
var offset int64
if n >= size {
offset = 0
} else {
offset = size - n
}
reader, err := a.handle.NewRangeReader(a.ctx, offset, -1)
if err != nil && err != io.EOF {
return nil, fmt.Errorf("error getting artifact reader: %w", err)
}
defer reader.Close()
read, err := ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("error reading all from artiact: %w", err)
}
return read, nil
}
// gzipped returns whether the file is gzip-encoded in GCS
func (a *StorageArtifact) gzipped() (bool, error) {
attrs, err := a.handle.Attrs(a.ctx)
if err != nil {
return false, fmt.Errorf("error getting gcs attributes for artifact: %w", err)
}
return attrs.ContentEncoding == "gzip", nil
}