Skip to content

Commit d79f0c6

Browse files
committed
Simplify the logic by removing dead code and enhance logging
Signed-off-by: Roy Yang <[email protected]>
1 parent 5592b5d commit d79f0c6

File tree

1 file changed

+51
-68
lines changed

1 file changed

+51
-68
lines changed

test/e2e_node/runner/remote/run_remote.go

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (e *envs) String() string {
8080
func (e *envs) Set(value string) error {
8181
kv := strings.SplitN(value, "=", 2)
8282
if len(kv) != 2 {
83-
return fmt.Errorf("invalid env string")
83+
return fmt.Errorf("invalid env string %s", value)
8484
}
8585
emap := *e
8686
emap[kv[0]] = kv[1]
@@ -148,21 +148,17 @@ type Resources struct {
148148
Accelerators []Accelerator `json:"accelerators,omitempty"`
149149
}
150150

151-
// GCEImage contains some information about CGE Image.
151+
// GCEImage contains some information about GCE Image.
152152
type GCEImage struct {
153153
Image string `json:"image,omitempty"`
154-
ImageDesc string `json:"image_description,omitempty"`
155-
Project string `json:"project"`
156-
Metadata string `json:"metadata"`
157154
ImageRegex string `json:"image_regex,omitempty"`
158-
// Defaults to using only the latest image. Acceptable values are [0, # of images that match the regex).
159-
// If the number of existing previous images is lesser than what is desired, the test will use that is available.
160-
PreviousImages int `json:"previous_images,omitempty"`
161-
// ImageFamily is the image family to use. The latest image from the image family will be used.
162-
ImageFamily string `json:"image_family,omitempty"`
163-
164-
Machine string `json:"machine,omitempty"`
165-
Resources Resources `json:"resources,omitempty"`
155+
// ImageFamily is the image family to use. The latest image from the image family will be used, e.g cos-81-lts.
156+
ImageFamily string `json:"image_family,omitempty"`
157+
ImageDesc string `json:"image_description,omitempty"`
158+
Project string `json:"project"`
159+
Metadata string `json:"metadata"`
160+
Machine string `json:"machine,omitempty"`
161+
Resources Resources `json:"resources,omitempty"`
166162
// This test is for benchmark (no limit verification, more result log, node name has format 'machine-image-uuid') if 'Tests' is non-empty.
167163
Tests []string `json:"tests,omitempty"`
168164
}
@@ -171,6 +167,7 @@ type internalImageConfig struct {
171167
images map[string]internalGCEImage
172168
}
173169

170+
// internalGCEImage is an internal GCE image representation for E2E node.
174171
type internalGCEImage struct {
175172
image string
176173
// imageDesc is the description of the image. If empty, the value in the
@@ -218,62 +215,54 @@ func main() {
218215
gceImages := &internalImageConfig{
219216
images: make(map[string]internalGCEImage),
220217
}
218+
// Parse images from given config file and convert them to internalGCEImage.
221219
if *imageConfigFile != "" {
222220
configPath := *imageConfigFile
223221
if *imageConfigDir != "" {
224222
configPath = filepath.Join(*imageConfigDir, *imageConfigFile)
225223
}
226224

227-
// parse images
228225
imageConfigData, err := ioutil.ReadFile(configPath)
229226
if err != nil {
230227
klog.Fatalf("Could not read image config file provided: %v", err)
231228
}
229+
// Unmarshal the given image config file. All images for this test run will be organized into a map.
230+
// shortName->GCEImage, e.g cos-stable->cos-stable-81-12871-103-0.
232231
externalImageConfig := ImageConfig{Images: make(map[string]GCEImage)}
233232
err = yaml.Unmarshal(imageConfigData, &externalImageConfig)
234233
if err != nil {
235234
klog.Fatalf("Could not parse image config file: %v", err)
236235
}
236+
237237
for shortName, imageConfig := range externalImageConfig.Images {
238-
var images []string
239-
isRegex, name := false, shortName
238+
var image string
240239
if (imageConfig.ImageRegex != "" || imageConfig.ImageFamily != "") && imageConfig.Image == "" {
241-
isRegex = true
242-
images, err = getGCEImages(imageConfig.ImageRegex, imageConfig.ImageFamily, imageConfig.Project, imageConfig.PreviousImages)
240+
image, err = getGCEImage(imageConfig.ImageRegex, imageConfig.ImageFamily, imageConfig.Project)
243241
if err != nil {
244-
klog.Fatalf("Could not retrieve list of images based on image prefix %q and family %q: %v",
245-
imageConfig.ImageRegex, imageConfig.ImageFamily, err)
246-
}
247-
if len(images) == 0 { // if we have no images we can't run anything
248-
klog.Fatalf("No matching images retrieved on image prefix %q and family %q: %v",
242+
klog.Fatalf("Could not retrieve a image based on image regex %q and family %q: %v",
249243
imageConfig.ImageRegex, imageConfig.ImageFamily, err)
250244
}
251245
} else {
252-
images = []string{imageConfig.Image}
246+
image = imageConfig.Image
253247
}
254-
for _, image := range images {
255-
metadata := imageConfig.Metadata
256-
if len(strings.TrimSpace(*instanceMetadata)) > 0 {
257-
metadata += "," + *instanceMetadata
258-
}
259-
gceImage := internalGCEImage{
260-
image: image,
261-
imageDesc: imageConfig.ImageDesc,
262-
project: imageConfig.Project,
263-
metadata: getImageMetadata(metadata),
264-
machine: imageConfig.Machine,
265-
tests: imageConfig.Tests,
266-
resources: imageConfig.Resources,
267-
}
268-
if gceImage.imageDesc == "" {
269-
gceImage.imageDesc = gceImage.image
270-
}
271-
if isRegex && len(images) > 1 {
272-
// Use image name when shortName is not unique.
273-
name = image
274-
}
275-
gceImages.images[name] = gceImage
248+
// Convert the given image into an internalGCEImage.
249+
metadata := imageConfig.Metadata
250+
if len(strings.TrimSpace(*instanceMetadata)) > 0 {
251+
metadata += "," + *instanceMetadata
276252
}
253+
gceImage := internalGCEImage{
254+
image: image,
255+
imageDesc: imageConfig.ImageDesc,
256+
project: imageConfig.Project,
257+
metadata: getImageMetadata(metadata),
258+
machine: imageConfig.Machine,
259+
tests: imageConfig.Tests,
260+
resources: imageConfig.Resources,
261+
}
262+
if gceImage.imageDesc == "" {
263+
gceImage.imageDesc = gceImage.image
264+
}
265+
gceImages.images[shortName] = gceImage
277266
}
278267
}
279268

@@ -284,21 +273,22 @@ func main() {
284273
klog.Fatal("Must specify --image-project if you specify --images")
285274
}
286275
cliImages := strings.Split(*images, ",")
287-
for _, img := range cliImages {
276+
for _, image := range cliImages {
288277
gceImage := internalGCEImage{
289-
image: img,
278+
image: image,
290279
project: *imageProject,
291280
metadata: getImageMetadata(*instanceMetadata),
292281
}
293-
gceImages.images[img] = gceImage
282+
gceImages.images[image] = gceImage
294283
}
295284
}
296285

297286
if len(gceImages.images) != 0 && *zone == "" {
298287
klog.Fatal("Must specify --zone flag")
299288
}
300-
for shortName, image := range gceImages.images {
301-
if image.project == "" {
289+
// Make sure GCP project is set. Without a project, images can't be retrieved..
290+
for shortName, imageConfig := range gceImages.images {
291+
if imageConfig.project == "" {
302292
klog.Fatalf("Invalid config for %v; must specify a project", shortName)
303293
}
304294
}
@@ -328,7 +318,7 @@ func main() {
328318
running := 0
329319
for shortName := range gceImages.images {
330320
imageConfig := gceImages.images[shortName]
331-
fmt.Printf("Initializing e2e tests using image %s.\n", shortName)
321+
fmt.Printf("Initializing e2e tests using image %s/%s/%s.\n", shortName, imageConfig.project, imageConfig.image)
332322
running++
333323
go func(image *internalGCEImage, junitFilePrefix string) {
334324
results <- testImage(image, junitFilePrefix)
@@ -470,18 +460,14 @@ type imageObj struct {
470460
name string
471461
}
472462

473-
func (io imageObj) string() string {
474-
return fmt.Sprintf("%q created %q", io.name, io.creationTime.String())
475-
}
476-
477463
type byCreationTime []imageObj
478464

479465
func (a byCreationTime) Len() int { return len(a) }
480466
func (a byCreationTime) Less(i, j int) bool { return a[i].creationTime.After(a[j].creationTime) }
481467
func (a byCreationTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
482468

483-
// Returns a list of image names based on regex and number of previous images requested.
484-
func getGCEImages(imageRegex, imageFamily string, project string, previousImages int) ([]string, error) {
469+
// Returns an image name based on regex and given GCE project.
470+
func getGCEImage(imageRegex, imageFamily string, project string) (string, error) {
485471
imageObjs := []imageObj{}
486472
imageRe := regexp.MustCompile(imageRegex)
487473
if err := computeService.Images.List(project).Pages(context.Background(),
@@ -501,24 +487,21 @@ func getGCEImages(imageRegex, imageFamily string, project string, previousImages
501487
creationTime: creationTime,
502488
name: instance.Name,
503489
}
504-
klog.V(4).Infof("Found image %q based on regex %q and family %q in project %q", io.string(), imageRegex, imageFamily, project)
505490
imageObjs = append(imageObjs, io)
506491
}
507492
return nil
508493
},
509494
); err != nil {
510-
return nil, fmt.Errorf("failed to list images in project %q: %v", project, err)
495+
return "", fmt.Errorf("failed to list images in project %q: %v", project, err)
511496
}
497+
498+
// Pick the latest image after sorting.
512499
sort.Sort(byCreationTime(imageObjs))
513-
images := []string{}
514-
for _, imageObj := range imageObjs {
515-
images = append(images, imageObj.name)
516-
previousImages--
517-
if previousImages < 0 {
518-
break
519-
}
500+
if len(imageObjs) > 0 {
501+
klog.V(4).Infof("found images %+v based on regex %q and family %q in project %q", imageObjs, imageRegex, imageFamily, project)
502+
return imageObjs[0].name, nil
520503
}
521-
return images, nil
504+
return "", fmt.Errorf("found zero images based on regex %q and family %q in project %q", imageRegex, imageFamily, project)
522505
}
523506

524507
// Provision a gce instance using image and run the tests in archive against the instance.

0 commit comments

Comments
 (0)