forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
328 lines (271 loc) · 12 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2018 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"
"os"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/pjutil/pprof"
"sigs.k8s.io/controller-runtime/pkg/manager"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/config/secret"
"k8s.io/test-infra/prow/crier"
gcsreporter "k8s.io/test-infra/prow/crier/reporters/gcs"
k8sgcsreporter "k8s.io/test-infra/prow/crier/reporters/gcs/kubernetes"
gerritreporter "k8s.io/test-infra/prow/crier/reporters/gerrit"
githubreporter "k8s.io/test-infra/prow/crier/reporters/github"
pubsubreporter "k8s.io/test-infra/prow/crier/reporters/pubsub"
slackreporter "k8s.io/test-infra/prow/crier/reporters/slack"
prowflagutil "k8s.io/test-infra/prow/flagutil"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
gerritclient "k8s.io/test-infra/prow/gerrit/client"
"k8s.io/test-infra/prow/interrupts"
"k8s.io/test-infra/prow/io"
"k8s.io/test-infra/prow/logrusutil"
"k8s.io/test-infra/prow/metrics"
slackclient "k8s.io/test-infra/prow/slack"
)
type options struct {
client prowflagutil.KubernetesOptions
cookiefilePath string
gerritProjects gerritclient.ProjectsFlag
github prowflagutil.GitHubOptions
githubEnablement prowflagutil.GitHubEnablementOptions
config configflagutil.ConfigOptions
gerritWorkers int
pubsubWorkers int
githubWorkers int
slackWorkers int
gcsWorkers int
k8sGCSWorkers int
blobStorageWorkers int
k8sBlobStorageWorkers int
slackTokenFile string
additionalSlackTokenFiles slackclient.HostsFlag
storage prowflagutil.StorageClientOptions
instrumentationOptions prowflagutil.InstrumentationOptions
k8sReportFraction float64
dryrun bool
reportAgent string
}
func (o *options) validate() error {
// TODO(krzyzacy): gerrit && github report are actually stateful..
// Need a better design to re-enable parallel reporting
if o.gerritWorkers > 1 {
logrus.Warn("gerrit reporter only supports one worker")
o.gerritWorkers = 1
}
if o.gerritWorkers+o.pubsubWorkers+o.githubWorkers+o.slackWorkers+o.gcsWorkers+o.k8sGCSWorkers+o.blobStorageWorkers+o.k8sBlobStorageWorkers <= 0 {
return errors.New("crier need to have at least one report worker to start")
}
if o.k8sReportFraction < 0 || o.k8sReportFraction > 1 {
return errors.New("--kubernetes-report-fraction must be a float between 0 and 1")
}
if o.gerritWorkers > 0 {
if len(o.gerritProjects) == 0 {
logrus.Info("--gerrit-projects is not set, using global config")
}
if o.cookiefilePath == "" {
logrus.Info("--cookiefile is not set, using anonymous authentication")
}
}
if o.githubWorkers > 0 {
if err := o.github.Validate(o.dryrun); err != nil {
return err
}
}
if o.slackWorkers > 0 {
if o.slackTokenFile == "" && len(o.additionalSlackTokenFiles) == 0 {
return errors.New("one of --slack-token-file or --additional-slack-token-files must be set")
}
}
if o.gcsWorkers > 0 {
logrus.Warn("--gcs-workers is deprecated and will be removed in August 2020. Use --blob-storage-workers instead.")
// return an error when the old and new flags are both set
if o.blobStorageWorkers != 0 {
return errors.New("only one of --gcs-workers or --blog-storage-workers can be set at the same time")
}
// use gcsWorkers if blobStorageWorkers is not set
o.blobStorageWorkers = o.gcsWorkers
}
if o.k8sGCSWorkers > 0 {
logrus.Warn("--kubernetes-gcs-workers is deprecated and will be removed in August 2020. Use --kubernetes-blob-storage-workers instead.")
// return an error when the old and new flags are both set
if o.k8sBlobStorageWorkers != 0 {
return errors.New("only one of --kubernetes-gcs-workers or --kubernetes-blog-storage-workers can be set at the same time")
}
// use k8sGCSWorkers if k8sBlobStorageWorkers is not set
o.k8sBlobStorageWorkers = o.k8sGCSWorkers
}
for _, opt := range []interface{ Validate(bool) error }{&o.client, &o.githubEnablement, &o.config} {
if err := opt.Validate(o.dryrun); err != nil {
return err
}
}
return nil
}
func (o *options) parseArgs(fs *flag.FlagSet, args []string) error {
o.gerritProjects = gerritclient.ProjectsFlag{}
fs.StringVar(&o.cookiefilePath, "cookiefile", "", "Path to git http.cookiefile, leave empty for anonymous")
fs.Var(&o.gerritProjects, "gerrit-projects", "Set of gerrit repos to monitor on a host example: --gerrit-host=https://android.googlesource.com=platform/build,toolchain/llvm, repeat flag for each host")
fs.IntVar(&o.gerritWorkers, "gerrit-workers", 0, "Number of gerrit report workers (0 means disabled)")
fs.IntVar(&o.pubsubWorkers, "pubsub-workers", 0, "Number of pubsub report workers (0 means disabled)")
fs.IntVar(&o.githubWorkers, "github-workers", 0, "Number of github report workers (0 means disabled)")
fs.IntVar(&o.slackWorkers, "slack-workers", 0, "Number of Slack report workers (0 means disabled)")
fs.Var(&o.additionalSlackTokenFiles, "additional-slack-token-files", "Map of additional slack token files. example: --additional-slack-token-files=foo=/etc/foo-slack-tokens/token, repeat flag for each host")
fs.IntVar(&o.gcsWorkers, "gcs-workers", 0, "Number of GCS report workers (0 means disabled)")
fs.IntVar(&o.k8sGCSWorkers, "kubernetes-gcs-workers", 0, "Number of Kubernetes-specific GCS report workers (0 means disabled)")
fs.IntVar(&o.blobStorageWorkers, "blob-storage-workers", 0, "Number of blob storage report workers (0 means disabled)")
fs.IntVar(&o.k8sBlobStorageWorkers, "kubernetes-blob-storage-workers", 0, "Number of Kubernetes-specific blob storage report workers (0 means disabled)")
fs.Float64Var(&o.k8sReportFraction, "kubernetes-report-fraction", 1.0, "Approximate portion of jobs to report pod information for, if kubernetes-gcs-workers are enabled (0 - > none, 1.0 -> all)")
fs.StringVar(&o.slackTokenFile, "slack-token-file", "", "Path to a Slack token file")
fs.StringVar(&o.reportAgent, "report-agent", "", "Only report specified agent - empty means report to all agents (effective for github and Slack only)")
// TODO(krzyzacy): implement dryrun for gerrit/pubsub
fs.BoolVar(&o.dryrun, "dry-run", false, "Run in dry-run mode, not doing actual report (effective for github and Slack only)")
o.config.AddFlags(fs)
o.github.AddFlags(fs)
o.client.AddFlags(fs)
o.storage.AddFlags(fs)
o.instrumentationOptions.AddFlags(fs)
o.githubEnablement.AddFlags(fs)
fs.Parse(args)
return o.validate()
}
func parseOptions() options {
var o options
if err := o.parseArgs(flag.CommandLine, os.Args[1:]); err != nil {
logrus.WithError(err).Fatal("Invalid flag options")
}
return o
}
func main() {
logrusutil.ComponentInit()
o := parseOptions()
defer interrupts.WaitForGracefulShutdown()
pprof.Instrument(o.instrumentationOptions)
configAgent, err := o.config.ConfigAgent()
if err != nil {
logrus.WithError(err).Fatal("Error starting config agent.")
}
cfg := configAgent.Config
restCfg, err := o.client.InfrastructureClusterConfig(o.dryrun)
if err != nil {
logrus.WithError(err).Fatal("Failed to get kubeconfig")
}
mgr, err := manager.New(restCfg, manager.Options{
Namespace: cfg().ProwJobNamespace,
MetricsBindAddress: "0",
})
if err != nil {
logrus.WithError(err).Fatal("failed to create manager")
}
// The watch apimachinery doesn't support restarts, so just exit the binary if a kubeconfig changes
// to make the kubelet restart us.
if err := o.client.AddKubeconfigChangeCallback(func() {
logrus.Info("Kubeconfig changed, exiting to trigger a restart")
interrupts.Terminate()
}); err != nil {
logrus.WithError(err).Fatal("Failed to register kubeconfig change callback")
}
var hasReporter bool
if o.slackWorkers > 0 {
if cfg().SlackReporterConfigs == nil {
logrus.Fatal("slackreporter is enabled but has no config")
}
slackConfig := func(refs *prowapi.Refs) config.SlackReporter {
return cfg().SlackReporterConfigs.GetSlackReporter(refs)
}
tokensMap := make(map[string]func() []byte)
if o.slackTokenFile != "" {
tokensMap[slackreporter.DefaultHostName] = secret.GetTokenGenerator(o.slackTokenFile)
if err := secret.Add(o.slackTokenFile); err != nil {
logrus.WithError(err).Fatal("could not read slack token")
}
}
hasReporter = true
for host, additionalTokenFile := range o.additionalSlackTokenFiles {
tokensMap[host] = secret.GetTokenGenerator(additionalTokenFile)
if err := secret.Add(additionalTokenFile); err != nil {
logrus.WithError(err).Fatal("could not read slack token")
}
}
slackReporter := slackreporter.New(slackConfig, o.dryrun, tokensMap)
if err := crier.New(mgr, slackReporter, o.slackWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct slack reporter controller")
}
}
if o.gerritWorkers > 0 {
gerritReporter, err := gerritreporter.NewReporter(cfg, o.cookiefilePath, o.gerritProjects, mgr.GetCache())
if err != nil {
logrus.WithError(err).Fatal("Error starting gerrit reporter")
}
hasReporter = true
if err := crier.New(mgr, gerritReporter, o.gerritWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct gerrit reporter controller")
}
}
if o.pubsubWorkers > 0 {
hasReporter = true
if err := crier.New(mgr, pubsubreporter.NewReporter(cfg), o.pubsubWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct pubsub reporter controller")
}
}
if o.githubWorkers > 0 {
if o.github.TokenPath != "" {
if err := secret.Add(o.github.TokenPath); err != nil {
logrus.WithError(err).Fatal("Error reading GitHub credentials")
}
}
githubClient, err := o.github.GitHubClient(o.dryrun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
}
hasReporter = true
githubReporter := githubreporter.NewReporter(githubClient, cfg, prowapi.ProwJobAgent(o.reportAgent), mgr.GetCache())
if err := crier.New(mgr, githubReporter, o.githubWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct github reporter controller")
}
}
if o.blobStorageWorkers > 0 || o.k8sBlobStorageWorkers > 0 {
opener, err := io.NewOpener(context.Background(), o.storage.GCSCredentialsFile, o.storage.S3CredentialsFile)
if err != nil {
logrus.WithError(err).Fatal("Error creating opener")
}
hasReporter = true
if err := crier.New(mgr, gcsreporter.New(cfg, opener, o.dryrun), o.blobStorageWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct gcsreporter controller")
}
if o.k8sBlobStorageWorkers > 0 {
coreClients, err := o.client.BuildClusterCoreV1Clients(o.dryrun)
if err != nil {
logrus.WithError(err).Fatal("Error building pod client sets for Kubernetes GCS workers")
}
k8sGcsReporter := k8sgcsreporter.New(cfg, opener, coreClients, float32(o.k8sReportFraction), o.dryrun)
if err := crier.New(mgr, k8sGcsReporter, o.k8sBlobStorageWorkers, o.githubEnablement.EnablementChecker()); err != nil {
logrus.WithError(err).Fatal("failed to construct k8sgcsreporter controller")
}
}
}
if !hasReporter {
logrus.Fatalf("should have at least one controller to start crier.")
}
// Push metrics to the configured prometheus pushgateway endpoint or serve them
metrics.ExposeMetrics("crier", cfg().PushGateway, o.instrumentationOptions.MetricsPort)
if err := mgr.Start(interrupts.Context()); err != nil {
logrus.WithError(err).Fatal("controller manager failed")
}
logrus.Info("Ended gracefully")
}