-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmain.go
168 lines (145 loc) · 5.74 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2020 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 (
"context"
"errors"
"flag"
"runtime"
"strings"
"time"
gpubsub "cloud.google.com/go/pubsub"
"github.com/GoogleCloudPlatform/testgrid/pkg/pubsub"
"github.com/GoogleCloudPlatform/testgrid/pkg/summarizer"
"github.com/GoogleCloudPlatform/testgrid/util"
"github.com/GoogleCloudPlatform/testgrid/util/gcs"
"github.com/GoogleCloudPlatform/testgrid/util/metrics/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
)
type options struct {
config gcs.Path // gcs://path/to/config/proto
persistQueue gcs.Path
creds string
confirm bool
dashboards util.Strings
concurrency int
wait time.Duration
summaryPathPrefix string
pubsub string
tabPathPrefix string
features summarizer.FeatureFlags
debug bool
trace bool
jsonLogs bool
}
func (o *options) validate() error {
if o.config.String() == "" {
return errors.New("empty --config")
}
if o.concurrency == 0 {
o.concurrency = 4 * runtime.NumCPU()
}
return nil
}
func gatherOptions() options {
var o options
flag.Var(&o.config, "config", "gs://path/to/config.pb")
flag.Var(&o.persistQueue, "persist-queue", "Load previous queue state from gs://path/to/queue-state.json and regularly save to it thereafter")
flag.StringVar(&o.creds, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
flag.BoolVar(&o.confirm, "confirm", false, "Upload data if set")
flag.Var(&o.dashboards, "dashboard", "Only update named dashboards if set (repeateable)")
flag.IntVar(&o.concurrency, "concurrency", 0, "Manually define the number of dashboards to concurrently update if non-zero")
flag.DurationVar(&o.wait, "wait", 0, "Ensure at least this much time has passed since the last loop (exit if zero).")
flag.StringVar(&o.summaryPathPrefix, "summary-path", "summary", "Write summaries under this GCS path.")
flag.StringVar(&o.pubsub, "pubsub", "", "listen for test group updates at project/subscription")
flag.StringVar(&o.tabPathPrefix, "tab-path", "tabs", "Read from tab state instead of test group")
flag.BoolVar(&o.features.AllowFuzzyFlakiness, "allow-fuzzy-flakiness", false, "Enable the functionality of further classifying flaky tabs (acceptable or not).")
flag.BoolVar(&o.features.AllowIgnoredColumns, "allow-ignored-columns", false, "Enable the functionality to ignore columns with specific test statuses during summarization.")
flag.BoolVar(&o.features.AllowMinNumberOfRuns, "allow-min-num-runs", false, "Enable the functionality to enforce a min limit to test runs.")
flag.BoolVar(&o.debug, "debug", false, "Log debug lines if set")
flag.BoolVar(&o.trace, "trace", false, "Log trace and debug lines if set")
flag.BoolVar(&o.jsonLogs, "json-logs", false, "Uses a json logrus formatter when set")
flag.Parse()
return o
}
func gcsFixer(ctx context.Context, projectSub string, configPath gcs.Path, tabPrefix, credPath string) (summarizer.Fixer, error) {
if projectSub == "" {
return nil, nil
}
parts := strings.SplitN(projectSub, "/", 2)
if len(parts) != 2 {
return nil, errors.New("malformed project/subscription")
}
projID, subID := parts[0], parts[1]
pubsubClient, err := gpubsub.NewClient(ctx, gpubsub.DetectProjectID, option.WithCredentialsFile(credPath))
if err != nil {
logrus.WithError(err).Fatal("Failed to create pubsub client")
}
client := pubsub.NewClient(pubsubClient)
return summarizer.FixGCS(client, logrus.StandardLogger(), projID, subID, configPath, tabPrefix)
}
func main() {
opt := gatherOptions()
if err := opt.validate(); err != nil {
logrus.Fatalf("Invalid flags: %v", err)
}
if !opt.confirm {
logrus.Warning("--confirm=false (DRY-RUN): will not write to gcs")
}
switch {
case opt.trace:
logrus.SetLevel(logrus.TraceLevel)
case opt.debug:
logrus.SetLevel(logrus.DebugLevel)
}
if opt.jsonLogs {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
logrus.SetReportCaller(true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
storageClient, err := gcs.ClientWithCreds(ctx, opt.creds)
if err != nil {
logrus.WithError(err).Fatal("Failed to read storage client")
}
client := gcs.NewClient(storageClient)
metrics := summarizer.CreateMetrics(prometheus.NewFactory())
fixer, err := gcsFixer(ctx, opt.pubsub, opt.config, opt.tabPathPrefix, opt.creds)
if err != nil {
logrus.WithError(err).WithField("subscription", opt.pubsub).Fatal("Failed to configure pubsub")
}
fixers := make([]summarizer.Fixer, 0, 2)
if fixer != nil {
fixers = append(fixers, fixer)
}
if path := opt.persistQueue; path.String() != "" {
const freq = time.Minute
ticker := time.NewTicker(freq)
log := logrus.WithField("frequency", freq)
fixers = append(fixers, summarizer.FixPersistent(log, client, path, ticker.C))
}
opts := &summarizer.UpdateOptions{
ConfigPath: opt.config,
Concurrency: opt.concurrency,
TabPathPrefix: opt.tabPathPrefix,
SummaryPathPrefix: opt.summaryPathPrefix,
AllowedDashboards: opt.dashboards.Strings(),
Confirm: opt.confirm,
Features: opt.features,
Freq: opt.wait,
}
if err := summarizer.Update(ctx, client, metrics, opts, fixers...); err != nil {
logrus.WithError(err).Error("Could not summarize")
}
}