github.com/abayer/test-infra@v0.0.5/prow/pjutil/tot.go (about) 1 /* 2 Copyright 2017 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 pjutil 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "net/url" 24 "path" 25 "time" 26 27 "k8s.io/test-infra/prow/config" 28 "k8s.io/test-infra/prow/kube" 29 "k8s.io/test-infra/prow/pod-utils/downwardapi" 30 ) 31 32 // PresubmitToJobSpec generates a downwardapi.JobSpec out of a Presubmit. 33 // Useful for figuring out GCS paths when parsing jobs out 34 // of a prow config. 35 func PresubmitToJobSpec(pre config.Presubmit) *downwardapi.JobSpec { 36 return &downwardapi.JobSpec{ 37 Type: kube.PresubmitJob, 38 Job: pre.Name, 39 } 40 } 41 42 // PostsubmitToJobSpec generates a downwardapi.JobSpec out of a Postsubmit. 43 // Useful for figuring out GCS paths when parsing jobs out 44 // of a prow config. 45 func PostsubmitToJobSpec(post config.Postsubmit) *downwardapi.JobSpec { 46 return &downwardapi.JobSpec{ 47 Type: kube.PostsubmitJob, 48 Job: post.Name, 49 } 50 } 51 52 // PeriodicToJobSpec generates a downwardapi.JobSpec out of a Periodic. 53 // Useful for figuring out GCS paths when parsing jobs out 54 // of a prow config. 55 func PeriodicToJobSpec(periodic config.Periodic) *downwardapi.JobSpec { 56 return &downwardapi.JobSpec{ 57 Type: kube.PeriodicJob, 58 Job: periodic.Name, 59 } 60 } 61 62 // GetBuildID calls out to `tot` in order 63 // to vend build identifier for the job 64 func GetBuildID(name, totURL string) (string, error) { 65 var err error 66 url, err := url.Parse(totURL) 67 if err != nil { 68 return "", fmt.Errorf("invalid tot url: %v", err) 69 } 70 url.Path = path.Join(url.Path, "vend", name) 71 sleep := 100 * time.Millisecond 72 for retries := 0; retries < 10; retries++ { 73 if retries > 0 { 74 time.Sleep(sleep) 75 sleep = sleep * 2 76 } 77 var resp *http.Response 78 resp, err = http.Get(url.String()) 79 if err != nil { 80 continue 81 } 82 defer resp.Body.Close() 83 if resp.StatusCode != 200 { 84 err = fmt.Errorf("got unexpected response from tot: %v", resp.Status) 85 continue 86 } 87 var buf []byte 88 buf, err = ioutil.ReadAll(resp.Body) 89 if err == nil { 90 return string(buf), nil 91 } 92 return "", err 93 } 94 return "", err 95 }