-
Notifications
You must be signed in to change notification settings - Fork 245
/
gcs-datasource.go
212 lines (176 loc) · 6.3 KB
/
gcs-datasource.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
package importer
import (
"context"
"fmt"
"io"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
"k8s.io/klog/v2"
"kubevirt.io/containerized-data-importer/pkg/common"
"kubevirt.io/containerized-data-importer/pkg/util"
)
const (
gcsFolderSep = "/"
gcsScheme = "gs"
)
// Helper for unit-testing
var newReaderFunc = getGcsObjectReader
// GCSDataSource is the struct containing the information needed to import from a GCS data source.
// Sequence of phases:
// 1. Info -> Transfer
// 2. Transfer -> Convert
type GCSDataSource struct {
// GCS end point
ep *url.URL
// Key File
keyFile string
// Reader
gcsReader io.ReadCloser
// stack of readers
readers *FormatReaders
// The image file in scratch space.
url *url.URL
}
// NewGCSDataSource creates a new instance of the GCSDataSource
func NewGCSDataSource(endpoint, keyFile string) (*GCSDataSource, error) {
klog.V(3).Infoln("GCS Importer: New Data Source")
// Placeholders
var bucket, object, host string
var options []option.ClientOption
// Parsing Endpoint
ep, err := ParseEndpoint(endpoint)
if err != nil {
return nil, errors.Wrapf(err, fmt.Sprintf("GCS Importer: unable to parse endpoint %q", endpoint))
}
// Getting Context
ctx, _ := context.WithTimeout(context.Background(), time.Second*60) //nolint:govet // todo - solve this: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
if ep.Scheme == "gs" {
// Using gs:// endpoint and extracting bucket and object name
bucket, object = extractGcsBucketAndObject(endpoint)
} else if ep.Scheme == "http" || ep.Scheme == "https" {
// Using http(s):// endpoint and extracting bucket, object name and host
bucket, object, host = extractGcsBucketObjectAndHost(endpoint)
options = append(options, option.WithEndpoint(host))
}
// Creating GCS Client
client, err := getGcsClient(ctx, keyFile, options...)
if err != nil {
klog.Errorf("GCS Importer: Error creating GCS Client")
return nil, err
}
// Creating GCS Reader
gcsReader, err := newReaderFunc(ctx, client, bucket, object)
if err != nil {
klog.Errorf("GCS Importer: Error creating Reader")
return nil, err
}
return &GCSDataSource{
ep: ep,
keyFile: keyFile,
gcsReader: gcsReader,
}, nil
}
// Info is called to get initial information about the data.
func (sd *GCSDataSource) Info() (ProcessingPhase, error) {
var err error
sd.readers, err = NewFormatReaders(sd.gcsReader, uint64(0))
if err != nil {
klog.Errorf("GCS Importer: Error creating readers: %v", err)
return ProcessingPhaseError, err
}
if !sd.readers.Convert {
// Downloading a raw file, we can write that directly to the target.
return ProcessingPhaseTransferDataFile, nil
}
return ProcessingPhaseTransferScratch, nil
}
// Transfer is called to transfer the data from the source to a temporary location.
func (sd *GCSDataSource) Transfer(path string) (ProcessingPhase, error) {
klog.V(3).Infoln("GCS Importer: Transfer")
file := filepath.Join(path, tempFile)
if err := CleanAll(file); err != nil {
return ProcessingPhaseError, err
}
size, _ := util.GetAvailableSpace(path)
if size <= int64(0) {
//Path provided is invalid.
klog.V(3).Infoln("GCS Importer: Transfer Error: ", ErrInvalidPath)
return ProcessingPhaseError, ErrInvalidPath
}
err := util.StreamDataToFile(sd.readers.TopReader(), file)
if err != nil {
klog.V(3).Infoln("GCS Importer: Transfer Error: ", err)
return ProcessingPhaseError, err
}
// If streaming succeeded, then parsing the file into URL will also succeed, no need to check error status
sd.url, _ = url.Parse(file)
return ProcessingPhaseConvert, nil
}
// TransferFile is called to transfer the data from the source to the passed in file.
func (sd *GCSDataSource) TransferFile(fileName string) (ProcessingPhase, error) {
if err := CleanAll(fileName); err != nil {
return ProcessingPhaseError, err
}
err := util.StreamDataToFile(sd.readers.TopReader(), fileName)
if err != nil {
return ProcessingPhaseError, err
}
return ProcessingPhaseResize, nil
}
// GetURL returns the url that the data processor can use when converting the data.
func (sd *GCSDataSource) GetURL() *url.URL {
return sd.url
}
// GetTerminationMessage returns data to be serialized and used as the termination message of the importer.
func (sd *GCSDataSource) GetTerminationMessage() *common.TerminationMessage {
return nil
}
// Close closes any readers or other open resources.
func (sd *GCSDataSource) Close() error {
var err error
if sd.readers != nil {
err = sd.readers.Close()
}
return err
}
// Create a Cloud Storage Client
func getGcsClient(ctx context.Context, keyFile string, options ...option.ClientOption) (*storage.Client, error) {
klog.V(3).Infoln("GCS Importer: Creating Client")
if keyFile == "" {
options = append(options, option.WithoutAuthentication())
klog.V(3).Infoln("GCS Importer: Authentication: Anonymous")
}
return storage.NewClient(ctx, options...)
}
// Create Cloud Storage Object Reader
func getGcsObjectReader(ctx context.Context, client *storage.Client, bucket, object string) (io.ReadCloser, error) {
klog.V(3).Infoln("GCS Importer: Creating Reader for bucket:", bucket, "object:", object)
return client.Bucket(bucket).Object(object).NewReader(ctx)
}
// Extract url in format gs://bucket/filename or gs://bucket/subdir/filename
func extractGcsBucketAndObject(s string) (string, string) {
klog.V(3).Infoln("GCS Importer: Extracting GCS Bucket and Object")
pathSplit := strings.Split(s, gcsFolderSep)
bucket := pathSplit[2]
object := strings.Join(pathSplit[3:], gcsFolderSep)
klog.V(3).Infoln("GCS Importer: GCS Bucket:", bucket)
klog.V(3).Infoln("GCS Importer: GCS Object:", object)
return bucket, object
}
// Extract url in format https://storage.cloud.google.com/bucket/filename
func extractGcsBucketObjectAndHost(s string) (string, string, string) {
klog.V(3).Infoln("GCS Importer: Extracting GCS Bucket, Object and Host")
pathSplit := strings.Split(s, gcsFolderSep)
host := strings.Join(pathSplit[:3], gcsFolderSep) + "/"
bucket := pathSplit[3]
object := strings.Join(pathSplit[4:], gcsFolderSep)
klog.V(3).Infoln("GCS Importer: GCS Host:", host)
klog.V(3).Infoln("GCS Importer: GCS Bucket:", bucket)
klog.V(3).Infoln("GCS Importer: GCS Object:", object)
return bucket, object, host
}