forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
408 lines (343 loc) · 10.5 KB
/
context.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Copyright 2016 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"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/denverdino/aliyungo/oss"
"github.com/golang/glog"
"github.com/gophercloud/gophercloud"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
storage "google.golang.org/api/storage/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
// VFSContext is a 'context' for VFS, that is normally a singleton
// but allows us to configure S3 credentials, for example
type VFSContext struct {
s3Context *S3Context
k8sContext *KubernetesContext
memfsContext *MemFSContext
// mutex guards gcsClient
mutex sync.Mutex
// The google cloud storage client, if initialized
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{
s3Context: NewS3Context(),
k8sContext: NewKubernetesContext(),
}
// ReadLocation reads a file from a vfs URL
// It supports additional schemes which don't (yet) have full VFS implementations:
// metadata: reads from instance metadata on GCE/AWS
// http / https: reads from HTTP
func (c *VFSContext) ReadFile(location string) ([]byte, error) {
if strings.Contains(location, "://") && !strings.HasPrefix(location, "file://") {
// Handle our special case schemas
u, err := url.Parse(location)
if err != nil {
return nil, fmt.Errorf("error parsing location %q - not a valid URI", location)
}
switch u.Scheme {
case "metadata":
switch u.Host {
case "gce":
httpURL := "http://169.254.169.254/computeMetadata/v1/instance/attributes/" + u.Path
httpHeaders := make(map[string]string)
httpHeaders["Metadata-Flavor"] = "Google"
return c.readHttpLocation(httpURL, httpHeaders)
case "aws":
httpURL := "http://169.254.169.254/latest/" + u.Path
return c.readHttpLocation(httpURL, nil)
case "digitalocean":
httpURL := "http://169.254.169.254/metadata/v1" + u.Path
return c.readHttpLocation(httpURL, nil)
default:
return nil, fmt.Errorf("unknown metadata type: %q in %q", u.Host, location)
}
case "http", "https":
return c.readHttpLocation(location, nil)
}
}
location = strings.TrimPrefix(location, "file://")
p, err := c.BuildVfsPath(location)
if err != nil {
return nil, err
}
return p.ReadFile()
}
func (c *VFSContext) BuildVfsPath(p string) (Path, error) {
if !strings.Contains(p, "://") {
return NewFSPath(p), nil
}
if strings.HasPrefix(p, "file://") {
f := strings.TrimPrefix(p, "file://")
return NewFSPath(f), nil
}
if strings.HasPrefix(p, "s3://") {
return c.buildS3Path(p)
}
if strings.HasPrefix(p, "do://") {
return c.buildDOPath(p)
}
if strings.HasPrefix(p, "memfs://") {
return c.buildMemFSPath(p)
}
if strings.HasPrefix(p, "gs://") {
return c.buildGCSPath(p)
}
if strings.HasPrefix(p, "k8s://") {
return c.buildKubernetesPath(p)
}
if strings.HasPrefix(p, "swift://") {
return c.buildOpenstackSwiftPath(p)
}
if strings.HasPrefix(p, "oss://") {
return c.buildOSSPath(p)
}
return nil, fmt.Errorf("unknown / unhandled path type: %q", p)
}
// readHttpLocation reads an http (or https) url.
// It returns the contents, or an error on any non-200 response. On a 404, it will return os.ErrNotExist
// It will retry a few times on a 500 class error
func (c *VFSContext) readHttpLocation(httpURL string, httpHeaders map[string]string) ([]byte, error) {
// Exponential backoff, starting with 500 milliseconds, doubling each time, 5 steps
backoff := wait.Backoff{
Duration: 500 * time.Millisecond,
Factor: 2,
Steps: 5,
}
var body []byte
done, err := RetryWithBackoff(backoff, func() (bool, error) {
glog.V(4).Infof("Performing HTTP request: GET %s", httpURL)
req, err := http.NewRequest("GET", httpURL, nil)
if err != nil {
return false, err
}
for k, v := range httpHeaders {
req.Header.Add(k, v)
}
response, err := http.DefaultClient.Do(req)
if response != nil {
defer response.Body.Close()
}
if err != nil {
return false, fmt.Errorf("error fetching %q: %v", httpURL, err)
}
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return false, fmt.Errorf("error reading response for %q: %v", httpURL, err)
}
if response.StatusCode == 404 {
// We retry on 404s in case of eventual consistency
return false, os.ErrNotExist
}
if response.StatusCode >= 500 && response.StatusCode <= 599 {
// Retry on 5XX errors
return false, fmt.Errorf("unexpected response code %q for %q: %v", response.Status, httpURL, string(body))
}
if response.StatusCode == 200 {
return true, nil
}
// Don't retry on other errors
return true, fmt.Errorf("unexpected response code %q for %q: %v", response.Status, httpURL, string(body))
})
if err != nil {
return nil, err
} else if done {
return body, nil
} else {
// Shouldn't happen - we always return a non-nil error with false
return nil, wait.ErrWaitTimeout
}
}
// RetryWithBackoff runs until a condition function returns true, or until Steps attempts have been taken
// As compared to wait.ExponentialBackoff, this function returns the results from the function on the final attempt
func RetryWithBackoff(backoff wait.Backoff, condition func() (bool, error)) (bool, error) {
duration := backoff.Duration
i := 0
for {
if i != 0 {
adjusted := duration
if backoff.Jitter > 0.0 {
adjusted = wait.Jitter(duration, backoff.Jitter)
}
time.Sleep(adjusted)
duration = time.Duration(float64(duration) * backoff.Factor)
}
i++
done, err := condition()
if done {
return done, err
}
noMoreRetries := i >= backoff.Steps
if !noMoreRetries && err != nil {
glog.V(2).Infof("retrying after error %v", err)
}
if noMoreRetries {
glog.V(2).Infof("hit maximum retries %d with error %v", i, err)
return done, err
}
}
}
func (c *VFSContext) buildS3Path(p string) (*S3Path, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid s3 path: %q", p)
}
if u.Scheme != "s3" {
return nil, fmt.Errorf("invalid s3 path: %q", p)
}
bucket := strings.TrimSuffix(u.Host, "/")
if bucket == "" {
return nil, fmt.Errorf("invalid s3 path: %q", p)
}
s3path := newS3Path(c.s3Context, u.Scheme, bucket, u.Path, true)
return s3path, nil
}
func (c *VFSContext) buildDOPath(p string) (*S3Path, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid spaces path: %q", p)
}
if u.Scheme != "do" {
return nil, fmt.Errorf("invalid spaces path: %q", p)
}
bucket := strings.TrimSuffix(u.Host, "/")
if bucket == "" {
return nil, fmt.Errorf("invalid spaces path: %q", p)
}
s3path := newS3Path(c.s3Context, u.Scheme, bucket, u.Path, false)
return s3path, nil
}
func (c *VFSContext) buildKubernetesPath(p string) (*KubernetesPath, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid kubernetes vfs path: %q", p)
}
if u.Scheme != "k8s" {
return nil, fmt.Errorf("invalid kubernetes vfs path: %q", p)
}
bucket := strings.TrimSuffix(u.Host, "/")
if bucket == "" {
return nil, fmt.Errorf("invalid kubernetes vfs path: %q", p)
}
k8sPath := newKubernetesPath(c.k8sContext, bucket, u.Path)
return k8sPath, nil
}
func (c *VFSContext) buildMemFSPath(p string) (*MemFSPath, error) {
if !strings.HasPrefix(p, "memfs://") {
return nil, fmt.Errorf("memfs path not recognized: %q", p)
}
location := strings.TrimPrefix(p, "memfs://")
if c.memfsContext == nil {
// We only initialize this in unit tests etc
return nil, fmt.Errorf("memfs context not initialized")
}
fspath := NewMemFSPath(c.memfsContext, location)
return fspath, nil
}
func (c *VFSContext) ResetMemfsContext(clusterReadable bool) {
c.memfsContext = NewMemFSContext()
if clusterReadable {
c.memfsContext.MarkClusterReadable()
}
}
func (c *VFSContext) buildGCSPath(p string) (*GSPath, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid google cloud storage path: %q", p)
}
if u.Scheme != "gs" {
return nil, fmt.Errorf("invalid google cloud storage path: %q", p)
}
bucket := strings.TrimSuffix(u.Host, "/")
gcsClient, err := c.getGCSClient()
if err != nil {
return nil, err
}
gcsPath := NewGSPath(gcsClient, bucket, u.Path)
return gcsPath, nil
}
// getGCSClient returns the google storage.Service client, caching it for future calls
func (c *VFSContext) getGCSClient() (*storage.Service, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.gcsClient != nil {
return c.gcsClient, nil
}
// TODO: Should we fall back to read-only?
scope := storage.DevstorageReadWriteScope
httpClient, err := google.DefaultClient(context.Background(), scope)
if err != nil {
return nil, fmt.Errorf("error building GCS HTTP client: %v", err)
}
gcsClient, err := storage.New(httpClient)
if err != nil {
return nil, fmt.Errorf("error building GCS client: %v", err)
}
c.gcsClient = gcsClient
return gcsClient, nil
}
func (c *VFSContext) buildOpenstackSwiftPath(p string) (*SwiftPath, error) {
u, err := url.Parse(p)
if err != nil {
return nil, fmt.Errorf("invalid openstack cloud storage path: %q", p)
}
if u.Scheme != "swift" {
return nil, fmt.Errorf("invalid openstack cloud storage path: %q", p)
}
bucket := strings.TrimSuffix(u.Host, "/")
if bucket == "" {
return nil, fmt.Errorf("invalid swift path: %q", p)
}
if c.swiftClient == nil {
swiftClient, err := NewSwiftClient()
if err != nil {
return nil, err
}
c.swiftClient = swiftClient
}
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)
}