github.com/abayer/test-infra@v0.0.5/prow/initupload/run.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package initupload 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "errors" 23 "fmt" 24 "io/ioutil" 25 "time" 26 27 "k8s.io/test-infra/prow/pod-utils/clone" 28 "k8s.io/test-infra/prow/pod-utils/downwardapi" 29 "k8s.io/test-infra/prow/pod-utils/gcs" 30 ) 31 32 func (o Options) Run() error { 33 spec, err := downwardapi.ResolveSpecFromEnv() 34 if err != nil { 35 return fmt.Errorf("could not resolve job spec: %v", err) 36 } 37 38 started := struct { 39 Timestamp int64 `json:"timestamp"` 40 }{ 41 Timestamp: time.Now().Unix(), 42 } 43 startedData, err := json.Marshal(&started) 44 if err != nil { 45 return fmt.Errorf("could not marshal starting data: %v", err) 46 } 47 uploadTargets := map[string]gcs.UploadFunc{ 48 "started.json": gcs.DataUpload(bytes.NewReader(startedData)), 49 } 50 51 var failed bool 52 if o.Log != "" { 53 if failed, err = processCloneLog(o.Log, uploadTargets); err != nil { 54 return err 55 } 56 } 57 58 if err := o.Options.Run(spec, uploadTargets); err != nil { 59 return fmt.Errorf("failed to upload to GCS: %v", err) 60 } 61 62 if failed { 63 return errors.New("cloning the appropriate refs failed") 64 } 65 66 return nil 67 } 68 69 func processCloneLog(logfile string, uploadTargets map[string]gcs.UploadFunc) (bool, error) { 70 var cloneRecords []clone.Record 71 data, err := ioutil.ReadFile(logfile) 72 if err != nil { 73 return true, fmt.Errorf("could not read clone log: %v", err) 74 } 75 if err = json.Unmarshal(data, &cloneRecords); err != nil { 76 return true, fmt.Errorf("could not unmarshal clone records: %v", err) 77 } 78 // Do not read from cloneLog directly. 79 // Instead create multiple readers from cloneLog so it can be uploaded to 80 // both clone-log.txt and build-log.txt on failure. 81 cloneLog := bytes.Buffer{} 82 failed := false 83 for _, record := range cloneRecords { 84 cloneLog.WriteString(clone.FormatRecord(record)) 85 failed = failed || record.Failed 86 } 87 uploadTargets["clone-log.txt"] = gcs.DataUpload(bytes.NewReader(cloneLog.Bytes())) 88 uploadTargets["clone-records.json"] = gcs.FileUpload(logfile) 89 90 if failed { 91 uploadTargets["build-log.txt"] = gcs.DataUpload(bytes.NewReader(cloneLog.Bytes())) 92 93 finished := struct { 94 Timestamp int64 `json:"timestamp"` 95 Passed bool `json:"passed"` 96 Result string `json:"result"` 97 }{ 98 Timestamp: time.Now().Unix(), 99 Passed: false, 100 Result: "FAILURE", 101 } 102 finishedData, err := json.Marshal(&finished) 103 if err != nil { 104 return true, fmt.Errorf("could not marshal finishing data: %v", err) 105 } 106 uploadTargets["finished.json"] = gcs.DataUpload(bytes.NewReader(finishedData)) 107 } 108 return failed, nil 109 }