forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-gcs-downloader.go
112 lines (100 loc) · 3.75 KB
/
google-gcs-downloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"k8s.io/contrib/test-utils/utils"
"k8s.io/kubernetes/test/e2e/perftype"
"sync"
)
// GoogleGCSDownloader that gets data about Google results from the GCS repository
type GoogleGCSDownloader struct {
Builds int
GoogleGCSBucketUtils *utils.Utils
}
// NewGoogleGCSDownloader creates a new GoogleGCSDownloader
func NewGoogleGCSDownloader(builds int) *GoogleGCSDownloader {
return &GoogleGCSDownloader{
Builds: builds,
GoogleGCSBucketUtils: utils.NewUtils(utils.KubekinsBucket, utils.LogDir),
}
}
// TODO(random-liu): Only download and update new data each time.
func (g *GoogleGCSDownloader) getData() (TestToBuildData, error) {
fmt.Print("Getting Data from GCS...\n")
result := make(TestToBuildData)
var resultLock sync.Mutex
var wg sync.WaitGroup
wg.Add(len(TestConfig[utils.KubekinsBucket]))
for job, tests := range TestConfig[utils.KubekinsBucket] {
if tests.Prefix == "" {
return result, fmt.Errorf("Invalid empty Prefix for job %s", job)
}
for testLabel := range tests.Descriptions {
testName := tests.Prefix + "-" + testLabel
resultLock.Lock()
if _, found := result[testName]; found {
return result, fmt.Errorf("Duplicate name %s", testName)
}
result[testName] = &BuildData{Job: job, Version: "", Builds: map[string][]perftype.DataItem{}}
resultLock.Unlock()
}
go g.getJobData(&wg, result, &resultLock, job, tests)
}
wg.Wait()
return result, nil
}
func (g *GoogleGCSDownloader) getJobData(wg *sync.WaitGroup, result TestToBuildData, resultLock *sync.Mutex, job string, tests Tests) {
defer wg.Done()
lastBuildNo, err := g.GoogleGCSBucketUtils.GetLastestBuildNumberFromJenkinsGoogleBucket(job)
if err != nil {
panic(err)
}
fmt.Printf("Last build no for %v: %v\n", job, lastBuildNo)
for buildNumber := lastBuildNo; buildNumber > lastBuildNo-g.Builds && buildNumber > 0; buildNumber-- {
fmt.Printf("Fetching %s build %v...\n", job, buildNumber)
for testLabel, testDescription := range tests.Descriptions {
fileStem := fmt.Sprintf("artifacts/%v_%v", testDescription.OutputFilePrefix, testDescription.Name)
artifacts, err := g.GoogleGCSBucketUtils.ListFilesInBuild(job, buildNumber, fileStem)
if err != nil || len(artifacts) == 0 {
fmt.Printf("Error while looking for %s* in %s build %v: %v\n", fileStem, job, buildNumber, err)
continue
}
metricsFilename := artifacts[0][strings.LastIndex(artifacts[0], "/")+1:]
if len(artifacts) > 1 {
fmt.Printf("WARNING: found multiple %s files with data, reading only one: %s\n", fileStem, metricsFilename)
}
testDataResponse, err := g.GoogleGCSBucketUtils.GetFileFromJenkinsGoogleBucket(job, buildNumber, fmt.Sprintf("artifacts/%v", metricsFilename))
if err != nil {
panic(err)
}
func() {
testDataBody := testDataResponse.Body
defer testDataBody.Close()
data, err := ioutil.ReadAll(testDataBody)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when reading response Body: %v\n", err)
return
}
testName := tests.Prefix + "-" + testLabel
resultLock.Lock()
buildData := result[testName]
resultLock.Unlock()
testDescription.Parser(data, buildNumber, buildData)
}()
}
}
}