-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
337 lines (300 loc) · 12.1 KB
/
utils.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
/*
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 utils
import (
"context"
"fmt"
"math/rand"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/oauth2/google"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
"k8s.io/klog/v2"
boskosclient "sigs.k8s.io/boskos/client"
"sigs.k8s.io/boskos/common"
utilcommon "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/remote"
)
const (
DiskLabelKey = "csi"
DiskLabelValue = "e2e-test"
)
var (
boskos, _ = boskosclient.NewClient(os.Getenv("JOB_NAME"), "http://boskos", "", "")
)
type DriverConfig struct {
ComputeEndpoint string
ExtraFlags []string
Zones []string
}
func GCEClientAndDriverSetup(instance *remote.InstanceInfo, driverConfig DriverConfig) (*remote.TestContext, error) {
port := fmt.Sprintf("%v", 1024+rand.Intn(10000))
goPath, ok := os.LookupEnv("GOPATH")
if !ok {
return nil, fmt.Errorf("Could not find environment variable GOPATH")
}
pkgPath := path.Join(goPath, "src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/")
binPath := path.Join(pkgPath, "bin/gce-pd-csi-driver")
endpoint := fmt.Sprintf("tcp://localhost:%s", port)
extra_flags := []string{
fmt.Sprintf("--extra-labels=%s=%s", DiskLabelKey, DiskLabelValue),
"--max-concurrent-format-and-mount=20", // otherwise the serialization times out the e2e test.
"--multi-zone-volume-handle-enable",
"--multi-zone-volume-handle-disk-types=pd-standard,hyperdisk-ml",
"--use-instance-api-to-poll-attachment-disk-types=pd-ssd",
"--use-instance-api-to-list-volumes-published-nodes",
"--supports-dynamic-iops-provisioning=hyperdisk-balanced,hyperdisk-extreme",
"--supports-dynamic-throughput-provisioning=hyperdisk-balanced,hyperdisk-throughput,hyperdisk-ml",
"--allow-hdha-provisioning",
"--device-in-use-timeout=10s", // Set lower than the usual value to expedite tests
fmt.Sprintf("--fallback-requisite-zones=%s", strings.Join(driverConfig.Zones, ",")),
}
extra_flags = append(extra_flags, fmt.Sprintf("--compute-endpoint=%s", driverConfig.ComputeEndpoint))
extra_flags = append(extra_flags, driverConfig.ExtraFlags...)
workspace := remote.NewWorkspaceDir("gce-pd-e2e-")
// Log at V(6) as the compute API calls are emitted at that level and it's
// useful to see what's happening when debugging tests.
driverRunCmd := fmt.Sprintf("sh -c '/usr/bin/nohup %s/gce-pd-csi-driver -v=6 --endpoint=%s %s 2> %s/prog.out < /dev/null > /dev/null &'",
workspace, endpoint, strings.Join(extra_flags, " "), workspace)
config := &remote.ClientConfig{
PkgPath: pkgPath,
BinPath: binPath,
WorkspaceDir: workspace,
RunDriverCmd: driverRunCmd,
Port: port,
}
err := os.Setenv("GCE_PD_CSI_STAGING_VERSION", "latest")
if err != nil {
return nil, err
}
return remote.SetupNewDriverAndClient(instance, config)
}
// getBoskosProject retries acquiring a boskos project until success or timeout
func getBoskosProject(resourceType string) *common.Resource {
timer := time.NewTimer(30 * time.Minute)
defer timer.Stop()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-timer.C:
klog.Fatalf("timed out trying to acquire boskos project")
case <-ticker.C:
p, err := boskos.Acquire(resourceType, "free", "busy")
if err != nil {
klog.Warningf("boskos failed to acquire project: %v", err)
} else if p == nil {
klog.Warningf("boskos does not have a free %s at the moment", resourceType)
} else {
return p
}
}
}
}
func SetupProwConfig(resourceType string) (project, serviceAccount string) {
// Try to get a Boskos project
klog.V(4).Infof("Running in PROW")
klog.V(4).Infof("Fetching a Boskos loaned project")
p := getBoskosProject(resourceType)
project = p.Name
go func(c *boskosclient.Client, proj string) {
for range time.Tick(time.Minute * 5) {
if err := c.UpdateOne(p.Name, "busy", nil); err != nil {
klog.Warningf("[Boskos] Update %s failed with %v", p.Name, err)
}
}
}(boskos, p.Name)
// If we're on CI overwrite the service account
klog.V(4).Infof("Fetching the default compute service account")
c, err := google.DefaultClient(context.Background(), cloudresourcemanager.CloudPlatformScope)
if err != nil {
klog.Fatalf("Failed to get Google Default Client: %v", err)
}
cloudresourcemanagerService, err := cloudresourcemanager.New(c)
if err != nil {
klog.Fatalf("Failed to create new cloudresourcemanager: %v", err)
}
resp, err := cloudresourcemanagerService.Projects.Get(project).Do()
if err != nil {
klog.Fatalf("Failed to get project %v from Cloud Resource Manager: %v", project, err)
}
// Default Compute Engine service account
// [PROJECT_NUMBER][email protected]
serviceAccount = fmt.Sprintf("%[email protected]", resp.ProjectNumber)
klog.Infof("Using project %v and service account %v", project, serviceAccount)
return project, serviceAccount
}
func ForceChmod(instance *remote.InstanceInfo, filePath string, perms string, recursive bool) error {
originalumask, err := instance.SSHNoSudo("umask")
if err != nil {
return fmt.Errorf("failed to umask. Output: %v, errror: %v", originalumask, err.Error())
}
output, err := instance.SSHNoSudo("umask", "0000")
if err != nil {
return fmt.Errorf("failed to umask. Output: %v, errror: %v", output, err.Error())
}
chmodOptions := []string{}
if recursive {
chmodOptions = []string{"-R"}
}
chmodOptions = append(chmodOptions, perms, filePath)
chmodCmd := append([]string{"chmod"}, chmodOptions...)
output, err = instance.SSH(chmodCmd...)
if err != nil {
return fmt.Errorf("failed to chmod file %s. Output: %v, errror: %v", filePath, output, err.Error())
}
output, err = instance.SSHNoSudo("umask", originalumask)
if err != nil {
return fmt.Errorf("failed to umask. Output: %v, errror: %v", output, err.Error())
}
return nil
}
func WriteFile(instance *remote.InstanceInfo, filePath, fileContents string) error {
output, err := instance.SSHNoSudo("echo", fileContents, ">", filePath)
if err != nil {
return fmt.Errorf("failed to write test file %s. Output: %v, errror: %v", filePath, output, err.Error())
}
return nil
}
func ReadFile(instance *remote.InstanceInfo, filePath string) (string, error) {
output, err := instance.SSHNoSudo("cat", filePath)
if err != nil {
return "", fmt.Errorf("failed to read test file %s. Output: %v, errror: %v", filePath, output, err.Error())
}
return output, nil
}
func WriteBlock(instance *remote.InstanceInfo, path, fileContents string) error {
output, err := instance.SSHNoSudo("echo", fileContents, "|", "dd", "of="+path)
if err != nil {
return fmt.Errorf("failed to write test file %s. Output: %v, errror: %v", path, output, err.Error())
}
return nil
}
func ReadBlock(instance *remote.InstanceInfo, path string, length int) (string, error) {
lengthStr := strconv.Itoa(length)
output, err := instance.SSHNoSudo("dd", "if="+path, "bs="+lengthStr, "count=1", "2>", "/dev/null")
if err != nil {
return "", fmt.Errorf("failed to read test file %s. Output: %v, errror: %v", path, output, err.Error())
}
return output, nil
}
func GetFSSizeInGb(instance *remote.InstanceInfo, mountPath string) (int64, error) {
output, err := instance.SSH("df", "--output=size", "-BG", mountPath, "|", "awk", "'NR==2'")
if err != nil {
return -1, fmt.Errorf("failed to get size of path %s. Output: %v, error: %v", mountPath, output, err.Error())
}
output = strings.TrimSuffix(strings.TrimSpace(output), "G")
n, err := strconv.ParseInt(output, 10, 64)
if err != nil {
return -1, fmt.Errorf("failed to parse size %s into int", output)
}
return n, nil
}
func GetBlockSizeInGb(instance *remote.InstanceInfo, devicePath string) (int64, error) {
output, err := instance.SSH("blockdev", "--getsize64", devicePath)
if err != nil {
return -1, fmt.Errorf("failed to get size of path %s. Output: %v, error: %v", devicePath, output, err.Error())
}
n, err := strconv.ParseInt(strings.TrimSpace(output), 10, 64)
if err != nil {
return -1, fmt.Errorf("failed to parse size %s into int", output)
}
return utilcommon.BytesToGbRoundDown(n), nil
}
func Symlink(instance *remote.InstanceInfo, src, dest string) error {
output, err := instance.SSH("ln", "-s", src, dest)
if err != nil {
return fmt.Errorf("failed to symlink from %s to %s. Output: %v, errror: %v", src, dest, output, err.Error())
}
return nil
}
func RmAll(instance *remote.InstanceInfo, filePath string) error {
output, err := instance.SSH("rm", "-rf", filePath)
if err != nil {
return fmt.Errorf("failed to delete all %s. Output: %v, errror: %v", filePath, output, err.Error())
}
return nil
}
func MkdirAll(instance *remote.InstanceInfo, dir string) error {
output, err := instance.SSH("mkdir", "-p", dir)
if err != nil {
return fmt.Errorf("failed to mkdir -p %s. Output: %v, errror: %v", dir, output, err.Error())
}
return nil
}
func CopyFile(instance *remote.InstanceInfo, src, dest string) error {
output, err := instance.SSH("cp", src, dest)
if err != nil {
return fmt.Errorf("failed to copy %s to %s. Output: %v, errror: %v", src, dest, output, err.Error())
}
return nil
}
// ValidateLogicalLinkIsDisk takes a symlink location at "link" and finds the
// link location - it then finds the backing PD using either scsi_id or
// google_nvme_id (depending on the /dev path) and validates that it is the
// same as diskName
func ValidateLogicalLinkIsDisk(instance *remote.InstanceInfo, link, diskName string) (bool, error) {
const (
scsiPattern = `^0Google\s+PersistentDisk\s+([\S]+)\s*$`
sdPattern = `sd\w+`
nvmeSerialPattern = `ID_SERIAL_SHORT=([\S]+)\s*`
nvmeDevPattern = `nvme\w+`
)
// regex to parse scsi_id output and extract the serial
scsiRegex := regexp.MustCompile(scsiPattern)
sdRegex := regexp.MustCompile(sdPattern)
nvmeSerialRegex := regexp.MustCompile(nvmeSerialPattern)
nvmeDevRegex := regexp.MustCompile(nvmeDevPattern)
devFsPath, err := instance.SSH("find", link, "-printf", "'%l'")
if err != nil {
// Skip over if there is no matching symlink.
return false, nil
}
if len(devFsPath) == 0 {
return false, nil
}
if sdx := sdRegex.FindString(devFsPath); len(sdx) != 0 {
fullDevPath := path.Join("/dev/", string(sdx))
scsiIDOut, err := instance.SSH("/lib/udev_containerized/scsi_id", "--page=0x83", "--whitelisted", fmt.Sprintf("--device=%v", fullDevPath))
if err != nil {
return false, fmt.Errorf("failed to find %s's SCSI ID. Output: %v, errror: %v", devFsPath, scsiIDOut, err.Error())
}
scsiID := scsiRegex.FindStringSubmatch(scsiIDOut)
if len(scsiID) == 0 {
return false, fmt.Errorf("scsi_id output cannot be parsed: %s. Output: %v", scsiID, scsiIDOut)
}
if scsiID[1] != diskName {
return false, fmt.Errorf("scsiID %s did not match expected diskName %s", scsiID, diskName)
}
return true, nil
} else if nvmex := nvmeDevRegex.FindString(devFsPath); len(nvmex) != 0 {
fullDevPath := path.Join("/dev/", string(nvmex))
nvmeIDOut, err := instance.SSH("/lib/udev_containerized/google_nvme_id", fmt.Sprintf("-d%v", fullDevPath))
if err != nil {
return false, fmt.Errorf("failed to find %s's NVME ID. Output: %v, errror: %v", devFsPath, nvmeIDOut, err.Error())
}
nvmeID := nvmeSerialRegex.FindStringSubmatch(nvmeIDOut)
if len(nvmeID) == 0 {
return false, fmt.Errorf("google_nvme_id output cannot be parsed: %s. Output: %v", nvmeID, nvmeIDOut)
}
if nvmeID[1] != diskName {
return false, fmt.Errorf("nvmeID %s did not match expected diskName %s", nvmeID, diskName)
}
return true, nil
}
return false, fmt.Errorf("symlinked disk %s for diskName %s does not match a supported /dev/sd* or /dev/nvme* path", devFsPath, diskName)
}