mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Merge pull request #91543 from bsdnet/runner
Simplify the logic by removing dead code and enhance logging
This commit is contained in:
commit
4c8e5c5a50
@ -80,7 +80,7 @@ func (e *envs) String() string {
|
|||||||
func (e *envs) Set(value string) error {
|
func (e *envs) Set(value string) error {
|
||||||
kv := strings.SplitN(value, "=", 2)
|
kv := strings.SplitN(value, "=", 2)
|
||||||
if len(kv) != 2 {
|
if len(kv) != 2 {
|
||||||
return fmt.Errorf("invalid env string")
|
return fmt.Errorf("invalid env string %s", value)
|
||||||
}
|
}
|
||||||
emap := *e
|
emap := *e
|
||||||
emap[kv[0]] = kv[1]
|
emap[kv[0]] = kv[1]
|
||||||
@ -148,19 +148,15 @@ type Resources struct {
|
|||||||
Accelerators []Accelerator `json:"accelerators,omitempty"`
|
Accelerators []Accelerator `json:"accelerators,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GCEImage contains some information about CGE Image.
|
// GCEImage contains some information about GCE Image.
|
||||||
type GCEImage struct {
|
type GCEImage struct {
|
||||||
Image string `json:"image,omitempty"`
|
Image string `json:"image,omitempty"`
|
||||||
|
ImageRegex string `json:"image_regex,omitempty"`
|
||||||
|
// ImageFamily is the image family to use. The latest image from the image family will be used, e.g cos-81-lts.
|
||||||
|
ImageFamily string `json:"image_family,omitempty"`
|
||||||
ImageDesc string `json:"image_description,omitempty"`
|
ImageDesc string `json:"image_description,omitempty"`
|
||||||
Project string `json:"project"`
|
Project string `json:"project"`
|
||||||
Metadata string `json:"metadata"`
|
Metadata string `json:"metadata"`
|
||||||
ImageRegex string `json:"image_regex,omitempty"`
|
|
||||||
// Defaults to using only the latest image. Acceptable values are [0, # of images that match the regex).
|
|
||||||
// If the number of existing previous images is lesser than what is desired, the test will use that is available.
|
|
||||||
PreviousImages int `json:"previous_images,omitempty"`
|
|
||||||
// ImageFamily is the image family to use. The latest image from the image family will be used.
|
|
||||||
ImageFamily string `json:"image_family,omitempty"`
|
|
||||||
|
|
||||||
Machine string `json:"machine,omitempty"`
|
Machine string `json:"machine,omitempty"`
|
||||||
Resources Resources `json:"resources,omitempty"`
|
Resources Resources `json:"resources,omitempty"`
|
||||||
// This test is for benchmark (no limit verification, more result log, node name has format 'machine-image-uuid') if 'Tests' is non-empty.
|
// This test is for benchmark (no limit verification, more result log, node name has format 'machine-image-uuid') if 'Tests' is non-empty.
|
||||||
@ -171,6 +167,7 @@ type internalImageConfig struct {
|
|||||||
images map[string]internalGCEImage
|
images map[string]internalGCEImage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// internalGCEImage is an internal GCE image representation for E2E node.
|
||||||
type internalGCEImage struct {
|
type internalGCEImage struct {
|
||||||
image string
|
image string
|
||||||
// imageDesc is the description of the image. If empty, the value in the
|
// imageDesc is the description of the image. If empty, the value in the
|
||||||
@ -218,40 +215,37 @@ func main() {
|
|||||||
gceImages := &internalImageConfig{
|
gceImages := &internalImageConfig{
|
||||||
images: make(map[string]internalGCEImage),
|
images: make(map[string]internalGCEImage),
|
||||||
}
|
}
|
||||||
|
// Parse images from given config file and convert them to internalGCEImage.
|
||||||
if *imageConfigFile != "" {
|
if *imageConfigFile != "" {
|
||||||
configPath := *imageConfigFile
|
configPath := *imageConfigFile
|
||||||
if *imageConfigDir != "" {
|
if *imageConfigDir != "" {
|
||||||
configPath = filepath.Join(*imageConfigDir, *imageConfigFile)
|
configPath = filepath.Join(*imageConfigDir, *imageConfigFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse images
|
|
||||||
imageConfigData, err := ioutil.ReadFile(configPath)
|
imageConfigData, err := ioutil.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Fatalf("Could not read image config file provided: %v", err)
|
klog.Fatalf("Could not read image config file provided: %v", err)
|
||||||
}
|
}
|
||||||
|
// Unmarshal the given image config file. All images for this test run will be organized into a map.
|
||||||
|
// shortName->GCEImage, e.g cos-stable->cos-stable-81-12871-103-0.
|
||||||
externalImageConfig := ImageConfig{Images: make(map[string]GCEImage)}
|
externalImageConfig := ImageConfig{Images: make(map[string]GCEImage)}
|
||||||
err = yaml.Unmarshal(imageConfigData, &externalImageConfig)
|
err = yaml.Unmarshal(imageConfigData, &externalImageConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Fatalf("Could not parse image config file: %v", err)
|
klog.Fatalf("Could not parse image config file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for shortName, imageConfig := range externalImageConfig.Images {
|
for shortName, imageConfig := range externalImageConfig.Images {
|
||||||
var images []string
|
var image string
|
||||||
isRegex, name := false, shortName
|
|
||||||
if (imageConfig.ImageRegex != "" || imageConfig.ImageFamily != "") && imageConfig.Image == "" {
|
if (imageConfig.ImageRegex != "" || imageConfig.ImageFamily != "") && imageConfig.Image == "" {
|
||||||
isRegex = true
|
image, err = getGCEImage(imageConfig.ImageRegex, imageConfig.ImageFamily, imageConfig.Project)
|
||||||
images, err = getGCEImages(imageConfig.ImageRegex, imageConfig.ImageFamily, imageConfig.Project, imageConfig.PreviousImages)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Fatalf("Could not retrieve list of images based on image prefix %q and family %q: %v",
|
klog.Fatalf("Could not retrieve a image based on image regex %q and family %q: %v",
|
||||||
imageConfig.ImageRegex, imageConfig.ImageFamily, err)
|
|
||||||
}
|
|
||||||
if len(images) == 0 { // if we have no images we can't run anything
|
|
||||||
klog.Fatalf("No matching images retrieved on image prefix %q and family %q: %v",
|
|
||||||
imageConfig.ImageRegex, imageConfig.ImageFamily, err)
|
imageConfig.ImageRegex, imageConfig.ImageFamily, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
images = []string{imageConfig.Image}
|
image = imageConfig.Image
|
||||||
}
|
}
|
||||||
for _, image := range images {
|
// Convert the given image into an internalGCEImage.
|
||||||
metadata := imageConfig.Metadata
|
metadata := imageConfig.Metadata
|
||||||
if len(strings.TrimSpace(*instanceMetadata)) > 0 {
|
if len(strings.TrimSpace(*instanceMetadata)) > 0 {
|
||||||
metadata += "," + *instanceMetadata
|
metadata += "," + *instanceMetadata
|
||||||
@ -268,12 +262,7 @@ func main() {
|
|||||||
if gceImage.imageDesc == "" {
|
if gceImage.imageDesc == "" {
|
||||||
gceImage.imageDesc = gceImage.image
|
gceImage.imageDesc = gceImage.image
|
||||||
}
|
}
|
||||||
if isRegex && len(images) > 1 {
|
gceImages.images[shortName] = gceImage
|
||||||
// Use image name when shortName is not unique.
|
|
||||||
name = image
|
|
||||||
}
|
|
||||||
gceImages.images[name] = gceImage
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,21 +273,22 @@ func main() {
|
|||||||
klog.Fatal("Must specify --image-project if you specify --images")
|
klog.Fatal("Must specify --image-project if you specify --images")
|
||||||
}
|
}
|
||||||
cliImages := strings.Split(*images, ",")
|
cliImages := strings.Split(*images, ",")
|
||||||
for _, img := range cliImages {
|
for _, image := range cliImages {
|
||||||
gceImage := internalGCEImage{
|
gceImage := internalGCEImage{
|
||||||
image: img,
|
image: image,
|
||||||
project: *imageProject,
|
project: *imageProject,
|
||||||
metadata: getImageMetadata(*instanceMetadata),
|
metadata: getImageMetadata(*instanceMetadata),
|
||||||
}
|
}
|
||||||
gceImages.images[img] = gceImage
|
gceImages.images[image] = gceImage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(gceImages.images) != 0 && *zone == "" {
|
if len(gceImages.images) != 0 && *zone == "" {
|
||||||
klog.Fatal("Must specify --zone flag")
|
klog.Fatal("Must specify --zone flag")
|
||||||
}
|
}
|
||||||
for shortName, image := range gceImages.images {
|
// Make sure GCP project is set. Without a project, images can't be retrieved..
|
||||||
if image.project == "" {
|
for shortName, imageConfig := range gceImages.images {
|
||||||
|
if imageConfig.project == "" {
|
||||||
klog.Fatalf("Invalid config for %v; must specify a project", shortName)
|
klog.Fatalf("Invalid config for %v; must specify a project", shortName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -328,7 +318,7 @@ func main() {
|
|||||||
running := 0
|
running := 0
|
||||||
for shortName := range gceImages.images {
|
for shortName := range gceImages.images {
|
||||||
imageConfig := gceImages.images[shortName]
|
imageConfig := gceImages.images[shortName]
|
||||||
fmt.Printf("Initializing e2e tests using image %s.\n", shortName)
|
fmt.Printf("Initializing e2e tests using image %s/%s/%s.\n", shortName, imageConfig.project, imageConfig.image)
|
||||||
running++
|
running++
|
||||||
go func(image *internalGCEImage, junitFilePrefix string) {
|
go func(image *internalGCEImage, junitFilePrefix string) {
|
||||||
results <- testImage(image, junitFilePrefix)
|
results <- testImage(image, junitFilePrefix)
|
||||||
@ -470,18 +460,14 @@ type imageObj struct {
|
|||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (io imageObj) string() string {
|
|
||||||
return fmt.Sprintf("%q created %q", io.name, io.creationTime.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
type byCreationTime []imageObj
|
type byCreationTime []imageObj
|
||||||
|
|
||||||
func (a byCreationTime) Len() int { return len(a) }
|
func (a byCreationTime) Len() int { return len(a) }
|
||||||
func (a byCreationTime) Less(i, j int) bool { return a[i].creationTime.After(a[j].creationTime) }
|
func (a byCreationTime) Less(i, j int) bool { return a[i].creationTime.After(a[j].creationTime) }
|
||||||
func (a byCreationTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a byCreationTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
|
||||||
// Returns a list of image names based on regex and number of previous images requested.
|
// Returns an image name based on regex and given GCE project.
|
||||||
func getGCEImages(imageRegex, imageFamily string, project string, previousImages int) ([]string, error) {
|
func getGCEImage(imageRegex, imageFamily string, project string) (string, error) {
|
||||||
imageObjs := []imageObj{}
|
imageObjs := []imageObj{}
|
||||||
imageRe := regexp.MustCompile(imageRegex)
|
imageRe := regexp.MustCompile(imageRegex)
|
||||||
if err := computeService.Images.List(project).Pages(context.Background(),
|
if err := computeService.Images.List(project).Pages(context.Background(),
|
||||||
@ -501,24 +487,21 @@ func getGCEImages(imageRegex, imageFamily string, project string, previousImages
|
|||||||
creationTime: creationTime,
|
creationTime: creationTime,
|
||||||
name: instance.Name,
|
name: instance.Name,
|
||||||
}
|
}
|
||||||
klog.V(4).Infof("Found image %q based on regex %q and family %q in project %q", io.string(), imageRegex, imageFamily, project)
|
|
||||||
imageObjs = append(imageObjs, io)
|
imageObjs = append(imageObjs, io)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, fmt.Errorf("failed to list images in project %q: %v", project, err)
|
return "", fmt.Errorf("failed to list images in project %q: %v", project, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pick the latest image after sorting.
|
||||||
sort.Sort(byCreationTime(imageObjs))
|
sort.Sort(byCreationTime(imageObjs))
|
||||||
images := []string{}
|
if len(imageObjs) > 0 {
|
||||||
for _, imageObj := range imageObjs {
|
klog.V(4).Infof("found images %+v based on regex %q and family %q in project %q", imageObjs, imageRegex, imageFamily, project)
|
||||||
images = append(images, imageObj.name)
|
return imageObjs[0].name, nil
|
||||||
previousImages--
|
|
||||||
if previousImages < 0 {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
return "", fmt.Errorf("found zero images based on regex %q and family %q in project %q", imageRegex, imageFamily, project)
|
||||||
return images, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provision a gce instance using image and run the tests in archive against the instance.
|
// Provision a gce instance using image and run the tests in archive against the instance.
|
||||||
|
Loading…
Reference in New Issue
Block a user