run_remote.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // To run the node e2e tests remotely against one or more hosts on gce:
  14. // $ go run run_remote.go --logtostderr --v 2 --ssh-env gce --hosts <comma separated hosts>
  15. // To run the node e2e tests remotely against one or more images on gce and provision them:
  16. // $ go run run_remote.go --logtostderr --v 2 --project <project> --zone <zone> --ssh-env gce --images <comma separated images>
  17. package main
  18. import (
  19. "context"
  20. "flag"
  21. "fmt"
  22. "io/ioutil"
  23. "math/rand"
  24. "net/http"
  25. "os"
  26. "os/exec"
  27. "path/filepath"
  28. "regexp"
  29. "sort"
  30. "strings"
  31. "sync"
  32. "time"
  33. "k8s.io/kubernetes/test/e2e_node/remote"
  34. "k8s.io/kubernetes/test/e2e_node/system"
  35. "github.com/google/uuid"
  36. "golang.org/x/oauth2/google"
  37. compute "google.golang.org/api/compute/v0.beta"
  38. "google.golang.org/api/option"
  39. "k8s.io/klog"
  40. "sigs.k8s.io/yaml"
  41. )
  42. var testArgs = flag.String("test_args", "", "Space-separated list of arguments to pass to Ginkgo test runner.")
  43. var testSuite = flag.String("test-suite", "default", "Test suite the runner initializes with. Currently support default|conformance")
  44. var instanceNamePrefix = flag.String("instance-name-prefix", "", "prefix for instance names")
  45. var zone = flag.String("zone", "", "gce zone the hosts live in")
  46. var project = flag.String("project", "", "gce project the hosts live in")
  47. var imageConfigFile = flag.String("image-config-file", "", "yaml file describing images to run")
  48. var imageConfigDir = flag.String("image-config-dir", "", "(optional)path to image config files")
  49. var imageProject = flag.String("image-project", "", "gce project the hosts live in")
  50. var images = flag.String("images", "", "images to test")
  51. var preemptibleInstances = flag.Bool("preemptible-instances", false, "If true, gce instances will be configured to be preemptible")
  52. var hosts = flag.String("hosts", "", "hosts to test")
  53. var cleanup = flag.Bool("cleanup", true, "If true remove files from remote hosts and delete temporary instances")
  54. var deleteInstances = flag.Bool("delete-instances", true, "If true, delete any instances created")
  55. var buildOnly = flag.Bool("build-only", false, "If true, build e2e_node_test.tar.gz and exit.")
  56. var instanceMetadata = flag.String("instance-metadata", "", "key/value metadata for instances separated by '=' or '<', 'k=v' means the key is 'k' and the value is 'v'; 'k<p' means the key is 'k' and the value is extracted from the local path 'p', e.g. k1=v1,k2<p2")
  57. var gubernator = flag.Bool("gubernator", false, "If true, output Gubernator link to view logs")
  58. var ginkgoFlags = flag.String("ginkgo-flags", "", "Passed to ginkgo to specify additional flags such as --skip=.")
  59. var systemSpecName = flag.String("system-spec-name", "", fmt.Sprintf("The name of the system spec used for validating the image in the node conformance test. The specs are at %s. If unspecified, the default built-in spec (system.DefaultSpec) will be used.", system.SystemSpecPath))
  60. var extraEnvs = flag.String("extra-envs", "", "The extra environment variables needed for node e2e tests. Format: a list of key=value pairs, e.g., env1=val1,env2=val2")
  61. // envs is the type used to collect all node envs. The key is the env name,
  62. // and the value is the env value
  63. type envs map[string]string
  64. // String function of flag.Value
  65. func (e *envs) String() string {
  66. return fmt.Sprint(*e)
  67. }
  68. // Set function of flag.Value
  69. func (e *envs) Set(value string) error {
  70. kv := strings.SplitN(value, "=", 2)
  71. if len(kv) != 2 {
  72. return fmt.Errorf("invalid env string")
  73. }
  74. emap := *e
  75. emap[kv[0]] = kv[1]
  76. return nil
  77. }
  78. // nodeEnvs is the node envs from the flag `node-env`.
  79. var nodeEnvs = make(envs)
  80. func init() {
  81. flag.Var(&nodeEnvs, "node-env", "An environment variable passed to instance as metadata, e.g. when '--node-env=PATH=/usr/bin' is specified, there will be an extra instance metadata 'PATH=/usr/bin'.")
  82. }
  83. const (
  84. defaultMachine = "n1-standard-1"
  85. acceleratorTypeResourceFormat = "https://www.googleapis.com/compute/beta/projects/%s/zones/%s/acceleratorTypes/%s"
  86. )
  87. var (
  88. computeService *compute.Service
  89. arc Archive
  90. suite remote.TestSuite
  91. )
  92. // Archive contains path info in the archive.
  93. type Archive struct {
  94. sync.Once
  95. path string
  96. err error
  97. }
  98. // TestResult contains some information about the test results.
  99. type TestResult struct {
  100. output string
  101. err error
  102. host string
  103. exitOk bool
  104. }
  105. // ImageConfig specifies what images should be run and how for these tests.
  106. // It can be created via the `--images` and `--image-project` flags, or by
  107. // specifying the `--image-config-file` flag, pointing to a json or yaml file
  108. // of the form:
  109. //
  110. // images:
  111. // short-name:
  112. // image: gce-image-name
  113. // project: gce-image-project
  114. // machine: for benchmark only, the machine type (GCE instance) to run test
  115. // tests: for benchmark only, a list of ginkgo focus strings to match tests
  116. // TODO(coufon): replace 'image' with 'node' in configurations
  117. // and we plan to support testing custom machines other than GCE by specifying host
  118. type ImageConfig struct {
  119. Images map[string]GCEImage `json:"images"`
  120. }
  121. // Accelerator contains type and count about resource.
  122. type Accelerator struct {
  123. Type string `json:"type,omitempty"`
  124. Count int64 `json:"count,omitempty"`
  125. }
  126. // Resources contains accelerators array.
  127. type Resources struct {
  128. Accelerators []Accelerator `json:"accelerators,omitempty"`
  129. }
  130. // GCEImage contains some information about CGE Image.
  131. type GCEImage struct {
  132. Image string `json:"image,omitempty"`
  133. ImageDesc string `json:"image_description,omitempty"`
  134. Project string `json:"project"`
  135. Metadata string `json:"metadata"`
  136. ImageRegex string `json:"image_regex,omitempty"`
  137. // Defaults to using only the latest image. Acceptable values are [0, # of images that match the regex).
  138. // If the number of existing previous images is lesser than what is desired, the test will use that is available.
  139. PreviousImages int `json:"previous_images,omitempty"`
  140. // ImageFamily is the image family to use. The latest image from the image family will be used.
  141. ImageFamily string `json:"image_family,omitempty"`
  142. Machine string `json:"machine,omitempty"`
  143. Resources Resources `json:"resources,omitempty"`
  144. // This test is for benchmark (no limit verification, more result log, node name has format 'machine-image-uuid') if 'Tests' is non-empty.
  145. Tests []string `json:"tests,omitempty"`
  146. }
  147. type internalImageConfig struct {
  148. images map[string]internalGCEImage
  149. }
  150. type internalGCEImage struct {
  151. image string
  152. // imageDesc is the description of the image. If empty, the value in the
  153. // 'image' will be used.
  154. imageDesc string
  155. project string
  156. resources Resources
  157. metadata *compute.Metadata
  158. machine string
  159. tests []string
  160. }
  161. func main() {
  162. klog.InitFlags(nil)
  163. flag.Parse()
  164. switch *testSuite {
  165. case "conformance":
  166. suite = remote.InitConformanceRemote()
  167. case "cadvisor":
  168. suite = remote.InitCAdvisorE2ERemote()
  169. // TODO: Add subcommand for node soaking, node conformance, cri validation.
  170. case "default":
  171. // Use node e2e suite by default if no subcommand is specified.
  172. suite = remote.InitNodeE2ERemote()
  173. default:
  174. klog.Fatalf("--test-suite must be one of default or conformance")
  175. }
  176. rand.Seed(time.Now().UnixNano())
  177. if *buildOnly {
  178. // Build the archive and exit
  179. remote.CreateTestArchive(suite, *systemSpecName)
  180. return
  181. }
  182. if *hosts == "" && *imageConfigFile == "" && *images == "" {
  183. klog.Fatalf("Must specify one of --image-config-file, --hosts, --images.")
  184. }
  185. var err error
  186. computeService, err = getComputeClient()
  187. if err != nil {
  188. klog.Fatalf("Unable to create gcloud compute service using defaults. Make sure you are authenticated. %v", err)
  189. }
  190. gceImages := &internalImageConfig{
  191. images: make(map[string]internalGCEImage),
  192. }
  193. if *imageConfigFile != "" {
  194. configPath := *imageConfigFile
  195. if *imageConfigDir != "" {
  196. configPath = filepath.Join(*imageConfigDir, *imageConfigFile)
  197. }
  198. // parse images
  199. imageConfigData, err := ioutil.ReadFile(configPath)
  200. if err != nil {
  201. klog.Fatalf("Could not read image config file provided: %v", err)
  202. }
  203. externalImageConfig := ImageConfig{Images: make(map[string]GCEImage)}
  204. err = yaml.Unmarshal(imageConfigData, &externalImageConfig)
  205. if err != nil {
  206. klog.Fatalf("Could not parse image config file: %v", err)
  207. }
  208. for shortName, imageConfig := range externalImageConfig.Images {
  209. var images []string
  210. isRegex, name := false, shortName
  211. if (imageConfig.ImageRegex != "" || imageConfig.ImageFamily != "") && imageConfig.Image == "" {
  212. isRegex = true
  213. images, err = getGCEImages(imageConfig.ImageRegex, imageConfig.ImageFamily, imageConfig.Project, imageConfig.PreviousImages)
  214. if err != nil {
  215. klog.Fatalf("Could not retrieve list of images based on image prefix %q and family %q: %v",
  216. imageConfig.ImageRegex, imageConfig.ImageFamily, err)
  217. }
  218. } else {
  219. images = []string{imageConfig.Image}
  220. }
  221. for _, image := range images {
  222. metadata := imageConfig.Metadata
  223. if len(strings.TrimSpace(*instanceMetadata)) > 0 {
  224. metadata += "," + *instanceMetadata
  225. }
  226. gceImage := internalGCEImage{
  227. image: image,
  228. imageDesc: imageConfig.ImageDesc,
  229. project: imageConfig.Project,
  230. metadata: getImageMetadata(metadata),
  231. machine: imageConfig.Machine,
  232. tests: imageConfig.Tests,
  233. resources: imageConfig.Resources,
  234. }
  235. if gceImage.imageDesc == "" {
  236. gceImage.imageDesc = gceImage.image
  237. }
  238. if isRegex && len(images) > 1 {
  239. // Use image name when shortName is not unique.
  240. name = image
  241. }
  242. gceImages.images[name] = gceImage
  243. }
  244. }
  245. }
  246. // Allow users to specify additional images via cli flags for local testing
  247. // convenience; merge in with config file
  248. if *images != "" {
  249. if *imageProject == "" {
  250. klog.Fatal("Must specify --image-project if you specify --images")
  251. }
  252. cliImages := strings.Split(*images, ",")
  253. for _, img := range cliImages {
  254. gceImage := internalGCEImage{
  255. image: img,
  256. project: *imageProject,
  257. metadata: getImageMetadata(*instanceMetadata),
  258. }
  259. gceImages.images[img] = gceImage
  260. }
  261. }
  262. if len(gceImages.images) != 0 && *zone == "" {
  263. klog.Fatal("Must specify --zone flag")
  264. }
  265. for shortName, image := range gceImages.images {
  266. if image.project == "" {
  267. klog.Fatalf("Invalid config for %v; must specify a project", shortName)
  268. }
  269. }
  270. if len(gceImages.images) != 0 {
  271. if *project == "" {
  272. klog.Fatal("Must specify --project flag to launch images into")
  273. }
  274. }
  275. if *instanceNamePrefix == "" {
  276. *instanceNamePrefix = "tmp-node-e2e-" + uuid.New().String()[:8]
  277. }
  278. // Setup coloring
  279. stat, _ := os.Stdout.Stat()
  280. useColor := (stat.Mode() & os.ModeCharDevice) != 0
  281. blue := ""
  282. noColour := ""
  283. if useColor {
  284. blue = "\033[0;34m"
  285. noColour = "\033[0m"
  286. }
  287. go arc.getArchive()
  288. defer arc.deleteArchive()
  289. results := make(chan *TestResult)
  290. running := 0
  291. for shortName := range gceImages.images {
  292. imageConfig := gceImages.images[shortName]
  293. fmt.Printf("Initializing e2e tests using image %s.\n", shortName)
  294. running++
  295. go func(image *internalGCEImage, junitFilePrefix string) {
  296. results <- testImage(image, junitFilePrefix)
  297. }(&imageConfig, shortName)
  298. }
  299. if *hosts != "" {
  300. for _, host := range strings.Split(*hosts, ",") {
  301. fmt.Printf("Initializing e2e tests using host %s.\n", host)
  302. running++
  303. go func(host string, junitFilePrefix string) {
  304. results <- testHost(host, *cleanup, "", junitFilePrefix, *ginkgoFlags)
  305. }(host, host)
  306. }
  307. }
  308. // Wait for all tests to complete and emit the results
  309. errCount := 0
  310. exitOk := true
  311. for i := 0; i < running; i++ {
  312. tr := <-results
  313. host := tr.host
  314. fmt.Println() // Print an empty line
  315. fmt.Printf("%s>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%s\n", blue, noColour)
  316. fmt.Printf("%s> START TEST >%s\n", blue, noColour)
  317. fmt.Printf("%s>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%s\n", blue, noColour)
  318. fmt.Printf("Start Test Suite on Host %s\n", host)
  319. fmt.Printf("%s\n", tr.output)
  320. if tr.err != nil {
  321. errCount++
  322. fmt.Printf("Failure Finished Test Suite on Host %s\n%v\n", host, tr.err)
  323. } else {
  324. fmt.Printf("Success Finished Test Suite on Host %s\n", host)
  325. }
  326. exitOk = exitOk && tr.exitOk
  327. fmt.Printf("%s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<%s\n", blue, noColour)
  328. fmt.Printf("%s< FINISH TEST <%s\n", blue, noColour)
  329. fmt.Printf("%s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<%s\n", blue, noColour)
  330. fmt.Println() // Print an empty line
  331. }
  332. // Set the exit code if there were failures
  333. if !exitOk {
  334. fmt.Printf("Failure: %d errors encountered.\n", errCount)
  335. callGubernator(*gubernator)
  336. arc.deleteArchive()
  337. os.Exit(1)
  338. }
  339. callGubernator(*gubernator)
  340. }
  341. func callGubernator(gubernator bool) {
  342. if gubernator {
  343. fmt.Println("Running gubernator.sh")
  344. output, err := exec.Command("./test/e2e_node/gubernator.sh", "y").Output()
  345. if err != nil {
  346. fmt.Println("gubernator.sh Failed")
  347. fmt.Println(err)
  348. return
  349. }
  350. fmt.Printf("%s", output)
  351. }
  352. return
  353. }
  354. func (a *Archive) getArchive() (string, error) {
  355. a.Do(func() { a.path, a.err = remote.CreateTestArchive(suite, *systemSpecName) })
  356. return a.path, a.err
  357. }
  358. func (a *Archive) deleteArchive() {
  359. path, err := a.getArchive()
  360. if err != nil {
  361. return
  362. }
  363. os.Remove(path)
  364. }
  365. func getImageMetadata(input string) *compute.Metadata {
  366. if input == "" {
  367. return nil
  368. }
  369. klog.V(3).Infof("parsing instance metadata: %q", input)
  370. raw := parseInstanceMetadata(input)
  371. klog.V(4).Infof("parsed instance metadata: %v", raw)
  372. metadataItems := []*compute.MetadataItems{}
  373. for k, v := range raw {
  374. val := v
  375. metadataItems = append(metadataItems, &compute.MetadataItems{
  376. Key: k,
  377. Value: &val,
  378. })
  379. }
  380. ret := compute.Metadata{Items: metadataItems}
  381. return &ret
  382. }
  383. // Run tests in archive against host
  384. func testHost(host string, deleteFiles bool, imageDesc, junitFilePrefix, ginkgoFlagsStr string) *TestResult {
  385. instance, err := computeService.Instances.Get(*project, *zone, host).Do()
  386. if err != nil {
  387. return &TestResult{
  388. err: err,
  389. host: host,
  390. exitOk: false,
  391. }
  392. }
  393. if strings.ToUpper(instance.Status) != "RUNNING" {
  394. err = fmt.Errorf("instance %s not in state RUNNING, was %s", host, instance.Status)
  395. return &TestResult{
  396. err: err,
  397. host: host,
  398. exitOk: false,
  399. }
  400. }
  401. externalIP := getExternalIP(instance)
  402. if len(externalIP) > 0 {
  403. remote.AddHostnameIP(host, externalIP)
  404. }
  405. path, err := arc.getArchive()
  406. if err != nil {
  407. // Don't log fatal because we need to do any needed cleanup contained in "defer" statements
  408. return &TestResult{
  409. err: fmt.Errorf("unable to create test archive: %v", err),
  410. }
  411. }
  412. output, exitOk, err := remote.RunRemote(suite, path, host, deleteFiles, imageDesc, junitFilePrefix, *testArgs, ginkgoFlagsStr, *systemSpecName, *extraEnvs)
  413. return &TestResult{
  414. output: output,
  415. err: err,
  416. host: host,
  417. exitOk: exitOk,
  418. }
  419. }
  420. type imageObj struct {
  421. creationTime time.Time
  422. name string
  423. }
  424. func (io imageObj) string() string {
  425. return fmt.Sprintf("%q created %q", io.name, io.creationTime.String())
  426. }
  427. type byCreationTime []imageObj
  428. func (a byCreationTime) Len() int { return len(a) }
  429. func (a byCreationTime) Less(i, j int) bool { return a[i].creationTime.After(a[j].creationTime) }
  430. func (a byCreationTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  431. // Returns a list of image names based on regex and number of previous images requested.
  432. func getGCEImages(imageRegex, imageFamily string, project string, previousImages int) ([]string, error) {
  433. imageObjs := []imageObj{}
  434. imageRe := regexp.MustCompile(imageRegex)
  435. if err := computeService.Images.List(project).Pages(context.Background(),
  436. func(ilc *compute.ImageList) error {
  437. for _, instance := range ilc.Items {
  438. if imageRegex != "" && !imageRe.MatchString(instance.Name) {
  439. continue
  440. }
  441. if imageFamily != "" && instance.Family != imageFamily {
  442. continue
  443. }
  444. creationTime, err := time.Parse(time.RFC3339, instance.CreationTimestamp)
  445. if err != nil {
  446. return fmt.Errorf("failed to parse instance creation timestamp %q: %v", instance.CreationTimestamp, err)
  447. }
  448. io := imageObj{
  449. creationTime: creationTime,
  450. name: instance.Name,
  451. }
  452. klog.V(4).Infof("Found image %q based on regex %q and family %q in project %q", io.string(), imageRegex, imageFamily, project)
  453. imageObjs = append(imageObjs, io)
  454. }
  455. return nil
  456. },
  457. ); err != nil {
  458. return nil, fmt.Errorf("failed to list images in project %q: %v", project, err)
  459. }
  460. sort.Sort(byCreationTime(imageObjs))
  461. images := []string{}
  462. for _, imageObj := range imageObjs {
  463. images = append(images, imageObj.name)
  464. previousImages--
  465. if previousImages < 0 {
  466. break
  467. }
  468. }
  469. return images, nil
  470. }
  471. // Provision a gce instance using image and run the tests in archive against the instance.
  472. // Delete the instance afterward.
  473. func testImage(imageConfig *internalGCEImage, junitFilePrefix string) *TestResult {
  474. ginkgoFlagsStr := *ginkgoFlags
  475. // Check whether the test is for benchmark.
  476. if len(imageConfig.tests) > 0 {
  477. // Benchmark needs machine type non-empty.
  478. if imageConfig.machine == "" {
  479. imageConfig.machine = defaultMachine
  480. }
  481. // Use the Ginkgo focus in benchmark config.
  482. ginkgoFlagsStr += (" " + testsToGinkgoFocus(imageConfig.tests))
  483. }
  484. host, err := createInstance(imageConfig)
  485. if *deleteInstances {
  486. defer deleteInstance(host)
  487. }
  488. if err != nil {
  489. return &TestResult{
  490. err: fmt.Errorf("unable to create gce instance with running docker daemon for image %s. %v", imageConfig.image, err),
  491. }
  492. }
  493. // Only delete the files if we are keeping the instance and want it cleaned up.
  494. // If we are going to delete the instance, don't bother with cleaning up the files
  495. deleteFiles := !*deleteInstances && *cleanup
  496. result := testHost(host, deleteFiles, imageConfig.imageDesc, junitFilePrefix, ginkgoFlagsStr)
  497. // This is a temporary solution to collect serial node serial log. Only port 1 contains useful information.
  498. // TODO(random-liu): Extract out and unify log collection logic with cluste e2e.
  499. serialPortOutput, err := computeService.Instances.GetSerialPortOutput(*project, *zone, host).Port(1).Do()
  500. if err != nil {
  501. klog.Errorf("Failed to collect serial output from node %q: %v", host, err)
  502. } else {
  503. logFilename := "serial-1.log"
  504. err := remote.WriteLog(host, logFilename, serialPortOutput.Contents)
  505. if err != nil {
  506. klog.Errorf("Failed to write serial output from node %q to %q: %v", host, logFilename, err)
  507. }
  508. }
  509. return result
  510. }
  511. // Provision a gce instance using image
  512. func createInstance(imageConfig *internalGCEImage) (string, error) {
  513. p, err := computeService.Projects.Get(*project).Do()
  514. if err != nil {
  515. return "", fmt.Errorf("failed to get project info %q", *project)
  516. }
  517. // Use default service account
  518. serviceAccount := p.DefaultServiceAccount
  519. klog.V(1).Infof("Creating instance %+v with service account %q", *imageConfig, serviceAccount)
  520. name := imageToInstanceName(imageConfig)
  521. i := &compute.Instance{
  522. Name: name,
  523. MachineType: machineType(imageConfig.machine),
  524. NetworkInterfaces: []*compute.NetworkInterface{
  525. {
  526. AccessConfigs: []*compute.AccessConfig{
  527. {
  528. Type: "ONE_TO_ONE_NAT",
  529. Name: "External NAT",
  530. },
  531. }},
  532. },
  533. Disks: []*compute.AttachedDisk{
  534. {
  535. AutoDelete: true,
  536. Boot: true,
  537. Type: "PERSISTENT",
  538. InitializeParams: &compute.AttachedDiskInitializeParams{
  539. SourceImage: sourceImage(imageConfig.image, imageConfig.project),
  540. DiskSizeGb: 20,
  541. },
  542. },
  543. },
  544. ServiceAccounts: []*compute.ServiceAccount{
  545. {
  546. Email: serviceAccount,
  547. Scopes: []string{
  548. "https://www.googleapis.com/auth/cloud-platform",
  549. },
  550. },
  551. },
  552. }
  553. scheduling := compute.Scheduling{
  554. Preemptible: *preemptibleInstances,
  555. }
  556. for _, accelerator := range imageConfig.resources.Accelerators {
  557. if i.GuestAccelerators == nil {
  558. autoRestart := true
  559. i.GuestAccelerators = []*compute.AcceleratorConfig{}
  560. scheduling.OnHostMaintenance = "TERMINATE"
  561. scheduling.AutomaticRestart = &autoRestart
  562. }
  563. aType := fmt.Sprintf(acceleratorTypeResourceFormat, *project, *zone, accelerator.Type)
  564. ac := &compute.AcceleratorConfig{
  565. AcceleratorCount: accelerator.Count,
  566. AcceleratorType: aType,
  567. }
  568. i.GuestAccelerators = append(i.GuestAccelerators, ac)
  569. }
  570. i.Scheduling = &scheduling
  571. i.Metadata = imageConfig.metadata
  572. var insertionOperationName string
  573. if _, err := computeService.Instances.Get(*project, *zone, i.Name).Do(); err != nil {
  574. op, err := computeService.Instances.Insert(*project, *zone, i).Do()
  575. if err != nil {
  576. ret := fmt.Sprintf("could not create instance %s: API error: %v", name, err)
  577. if op != nil {
  578. ret = fmt.Sprintf("%s: %v", ret, op.Error)
  579. }
  580. return "", fmt.Errorf(ret)
  581. } else if op.Error != nil {
  582. var errs []string
  583. for _, insertErr := range op.Error.Errors {
  584. errs = append(errs, fmt.Sprintf("%+v", insertErr))
  585. }
  586. return "", fmt.Errorf("could not create instance %s: %+v", name, errs)
  587. }
  588. insertionOperationName = op.Name
  589. }
  590. instanceRunning := false
  591. for i := 0; i < 30 && !instanceRunning; i++ {
  592. if i > 0 {
  593. time.Sleep(time.Second * 20)
  594. }
  595. var insertionOperation *compute.Operation
  596. insertionOperation, err = computeService.ZoneOperations.Get(*project, *zone, insertionOperationName).Do()
  597. if err != nil {
  598. continue
  599. }
  600. if strings.ToUpper(insertionOperation.Status) != "DONE" {
  601. err = fmt.Errorf("instance insert operation %s not in state DONE, was %s", name, insertionOperation.Status)
  602. continue
  603. }
  604. if insertionOperation.Error != nil {
  605. var errs []string
  606. for _, insertErr := range insertionOperation.Error.Errors {
  607. errs = append(errs, fmt.Sprintf("%+v", insertErr))
  608. }
  609. return name, fmt.Errorf("could not create instance %s: %+v", name, errs)
  610. }
  611. var instance *compute.Instance
  612. instance, err = computeService.Instances.Get(*project, *zone, name).Do()
  613. if err != nil {
  614. continue
  615. }
  616. if strings.ToUpper(instance.Status) != "RUNNING" {
  617. err = fmt.Errorf("instance %s not in state RUNNING, was %s", name, instance.Status)
  618. continue
  619. }
  620. externalIP := getExternalIP(instance)
  621. if len(externalIP) > 0 {
  622. remote.AddHostnameIP(name, externalIP)
  623. }
  624. // TODO(random-liu): Remove the docker version check. Use some other command to check
  625. // instance readiness.
  626. var output string
  627. output, err = remote.SSH(name, "docker", "version")
  628. if err != nil {
  629. err = fmt.Errorf("instance %s not running docker daemon - Command failed: %s", name, output)
  630. continue
  631. }
  632. if !strings.Contains(output, "Server") {
  633. err = fmt.Errorf("instance %s not running docker daemon - Server not found: %s", name, output)
  634. continue
  635. }
  636. instanceRunning = true
  637. }
  638. // If instance didn't reach running state in time, return with error now.
  639. if err != nil {
  640. return name, err
  641. }
  642. // Instance reached running state in time, make sure that cloud-init is complete
  643. if isCloudInitUsed(imageConfig.metadata) {
  644. cloudInitFinished := false
  645. for i := 0; i < 60 && !cloudInitFinished; i++ {
  646. if i > 0 {
  647. time.Sleep(time.Second * 20)
  648. }
  649. var finished string
  650. finished, err = remote.SSH(name, "ls", "/var/lib/cloud/instance/boot-finished")
  651. if err != nil {
  652. err = fmt.Errorf("instance %s has not finished cloud-init script: %s", name, finished)
  653. continue
  654. }
  655. cloudInitFinished = true
  656. }
  657. }
  658. return name, err
  659. }
  660. func isCloudInitUsed(metadata *compute.Metadata) bool {
  661. if metadata == nil {
  662. return false
  663. }
  664. for _, item := range metadata.Items {
  665. if item.Key == "user-data" && item.Value != nil && strings.HasPrefix(*item.Value, "#cloud-config") {
  666. return true
  667. }
  668. }
  669. return false
  670. }
  671. func getExternalIP(instance *compute.Instance) string {
  672. for i := range instance.NetworkInterfaces {
  673. ni := instance.NetworkInterfaces[i]
  674. for j := range ni.AccessConfigs {
  675. ac := ni.AccessConfigs[j]
  676. if len(ac.NatIP) > 0 {
  677. return ac.NatIP
  678. }
  679. }
  680. }
  681. return ""
  682. }
  683. func getComputeClient() (*compute.Service, error) {
  684. const retries = 10
  685. const backoff = time.Second * 6
  686. // Setup the gce client for provisioning instances
  687. // Getting credentials on gce jenkins is flaky, so try a couple times
  688. var err error
  689. var cs *compute.Service
  690. for i := 0; i < retries; i++ {
  691. if i > 0 {
  692. time.Sleep(backoff)
  693. }
  694. var client *http.Client
  695. client, err = google.DefaultClient(context.Background(), compute.ComputeScope)
  696. if err != nil {
  697. continue
  698. }
  699. cs, err = compute.NewService(context.Background(), option.WithHTTPClient(client))
  700. if err != nil {
  701. continue
  702. }
  703. return cs, nil
  704. }
  705. return nil, err
  706. }
  707. func deleteInstance(host string) {
  708. klog.Infof("Deleting instance %q", host)
  709. _, err := computeService.Instances.Delete(*project, *zone, host).Do()
  710. if err != nil {
  711. klog.Errorf("Error deleting instance %q: %v", host, err)
  712. }
  713. }
  714. func parseInstanceMetadata(str string) map[string]string {
  715. metadata := make(map[string]string)
  716. ss := strings.Split(str, ",")
  717. for _, s := range ss {
  718. kv := strings.Split(s, "=")
  719. if len(kv) == 2 {
  720. metadata[kv[0]] = kv[1]
  721. continue
  722. }
  723. kp := strings.Split(s, "<")
  724. if len(kp) != 2 {
  725. klog.Fatalf("Invalid instance metadata: %q", s)
  726. continue
  727. }
  728. metaPath := kp[1]
  729. if *imageConfigDir != "" {
  730. metaPath = filepath.Join(*imageConfigDir, metaPath)
  731. }
  732. v, err := ioutil.ReadFile(metaPath)
  733. if err != nil {
  734. klog.Fatalf("Failed to read metadata file %q: %v", metaPath, err)
  735. continue
  736. }
  737. metadata[kp[0]] = string(v)
  738. }
  739. for k, v := range nodeEnvs {
  740. metadata[k] = v
  741. }
  742. return metadata
  743. }
  744. func imageToInstanceName(imageConfig *internalGCEImage) string {
  745. if imageConfig.machine == "" {
  746. return *instanceNamePrefix + "-" + imageConfig.image
  747. }
  748. // For benchmark test, node name has the format 'machine-image-uuid' to run
  749. // different machine types with the same image in parallel
  750. return imageConfig.machine + "-" + imageConfig.image + "-" + uuid.New().String()[:8]
  751. }
  752. func sourceImage(image, imageProject string) string {
  753. return fmt.Sprintf("projects/%s/global/images/%s", imageProject, image)
  754. }
  755. func machineType(machine string) string {
  756. if machine == "" {
  757. machine = defaultMachine
  758. }
  759. return fmt.Sprintf("zones/%s/machineTypes/%s", *zone, machine)
  760. }
  761. // testsToGinkgoFocus converts the test string list to Ginkgo focus
  762. func testsToGinkgoFocus(tests []string) string {
  763. focus := "--focus=\""
  764. for i, test := range tests {
  765. if i == 0 {
  766. focus += test
  767. } else {
  768. focus += ("|" + test)
  769. }
  770. }
  771. return focus + "\""
  772. }