run.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2018 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. package initupload
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "time"
  21. "k8s.io/test-infra/prow/pod-utils/clone"
  22. "k8s.io/test-infra/prow/pod-utils/downwardapi"
  23. "k8s.io/test-infra/prow/pod-utils/gcs"
  24. )
  25. func (o Options) Run() error {
  26. spec, err := downwardapi.ResolveSpecFromEnv()
  27. if err != nil {
  28. return fmt.Errorf("could not resolve job spec: %v", err)
  29. }
  30. started := struct {
  31. Timestamp int64 `json:"timestamp"`
  32. }{
  33. Timestamp: time.Now().Unix(),
  34. }
  35. startedData, err := json.Marshal(&started)
  36. if err != nil {
  37. return fmt.Errorf("could not marshal starting data: %v", err)
  38. }
  39. uploadTargets := map[string]gcs.UploadFunc{
  40. "started.json": gcs.DataUpload(bytes.NewReader(startedData)),
  41. }
  42. var failed bool
  43. if o.Log != "" {
  44. if failed, err = processCloneLog(o.Log, uploadTargets); err != nil {
  45. return err
  46. }
  47. }
  48. if err := o.Options.Run(spec, uploadTargets); err != nil {
  49. return fmt.Errorf("failed to upload to GCS: %v", err)
  50. }
  51. if failed {
  52. return errors.New("cloning the appropriate refs failed")
  53. }
  54. return nil
  55. }
  56. func processCloneLog(logfile string, uploadTargets map[string]gcs.UploadFunc) (bool, error) {
  57. var cloneRecords []clone.Record
  58. data, err := ioutil.ReadFile(logfile)
  59. if err != nil {
  60. return true, fmt.Errorf("could not read clone log: %v", err)
  61. }
  62. if err = json.Unmarshal(data, &cloneRecords); err != nil {
  63. return true, fmt.Errorf("could not unmarshal clone records: %v", err)
  64. }
  65. // Do not read from cloneLog directly.
  66. // Instead create multiple readers from cloneLog so it can be uploaded to
  67. // both clone-log.txt and build-log.txt on failure.
  68. cloneLog := bytes.Buffer{}
  69. failed := false
  70. for _, record := range cloneRecords {
  71. cloneLog.WriteString(clone.FormatRecord(record))
  72. failed = failed || record.Failed
  73. }
  74. uploadTargets["clone-log.txt"] = gcs.DataUpload(bytes.NewReader(cloneLog.Bytes()))
  75. uploadTargets["clone-records.json"] = gcs.FileUpload(logfile)
  76. if failed {
  77. uploadTargets["build-log.txt"] = gcs.DataUpload(bytes.NewReader(cloneLog.Bytes()))
  78. finished := struct {
  79. Timestamp int64 `json:"timestamp"`
  80. Passed bool `json:"passed"`
  81. Result string `json:"result"`
  82. }{
  83. Timestamp: time.Now().Unix(),
  84. Passed: false,
  85. Result: "FAILURE",
  86. }
  87. finishedData, err := json.Marshal(&finished)
  88. if err != nil {
  89. return true, fmt.Errorf("could not marshal finishing data: %v", err)
  90. }
  91. uploadTargets["finished.json"] = gcs.DataUpload(bytes.NewReader(finishedData))
  92. }
  93. return failed, nil
  94. }