forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
311 lines (269 loc) · 8.63 KB
/
main.go
File metadata and controls
311 lines (269 loc) · 8.63 KB
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
/*
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 (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"time"
tgCfgUtil "github.com/GoogleCloudPlatform/testgrid/config"
"github.com/GoogleCloudPlatform/testgrid/config/yamlcfg"
"github.com/GoogleCloudPlatform/testgrid/util/gcs"
prowConfig "k8s.io/test-infra/prow/config"
"cloud.google.com/go/storage"
"github.com/sirupsen/logrus"
)
type multiString []string
func (m multiString) String() string {
return strings.Join(m, ",")
}
func (m *multiString) Set(v string) error {
*m = strings.Split(v, ",")
return nil
}
// How long Configurator waits between file checks in polling mode
const pollingTime = time.Second
type options struct {
creds string
inputs multiString
oneshot bool
output string
printText bool
validateConfigFile bool
worldReadable bool
writeYAML bool
prowConfig string
prowJobConfig string
defaultYAML string
}
func (o *options) gatherOptions(fs *flag.FlagSet, args []string) error {
fs.StringVar(&o.creds, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
fs.BoolVar(&o.oneshot, "oneshot", false, "Write proto once and exit instead of monitoring --yaml files for changes")
fs.StringVar(&o.output, "output", "", "write proto to gs://bucket/obj or /local/path")
fs.BoolVar(&o.printText, "print-text", false, "print generated info in text format to stdout")
fs.BoolVar(&o.validateConfigFile, "validate-config-file", false, "validate that the given config files are syntactically correct and exit (proto is not written anywhere)")
fs.BoolVar(&o.worldReadable, "world-readable", false, "when uploading the proto to GCS, makes it world readable. Has no effect on writing to the local filesystem.")
fs.BoolVar(&o.writeYAML, "output-yaml", false, "Output to TestGrid YAML instead of config proto")
fs.Var(&o.inputs, "yaml", "comma-separated list of input YAML files or directories")
fs.StringVar(&o.prowConfig, "prow-config", "", "path to the prow config file. Required by --prow-job-config")
fs.StringVar(&o.prowJobConfig, "prow-job-config", "", "path to the prow job config. If specified, incorporates testgrid annotations on prowjobs. Requires --prow-config.")
fs.StringVar(&o.defaultYAML, "default", "", "path to default settings; required for proto outputs")
if err := fs.Parse(args); err != nil {
return err
}
if len(o.inputs) == 0 || o.inputs[0] == "" {
return errors.New("--yaml must include at least one file")
}
if !o.printText && !o.validateConfigFile && o.output == "" {
return errors.New("--print-text, --validate-config-file, or --output required")
}
if o.validateConfigFile && o.output != "" {
return errors.New("--validate-config-file doesn't write the proto anywhere")
}
if (o.prowConfig == "") != (o.prowJobConfig == "") {
return errors.New("--prow-config and --prow-job-config must be specified together")
}
if o.defaultYAML == "" && !o.writeYAML {
logrus.Warnf("--default not explicitly specified; assuming %s", o.inputs[0])
o.defaultYAML = o.inputs[0]
}
return nil
}
// announceChanges watches for changes in "paths" and writes them to the channel
func announceChanges(ctx context.Context, paths []string, channel chan []string) {
defer close(channel)
modified := map[string]time.Time{}
// TODO(fejta): consider waiting for a notification rather than polling
// but performance isn't that big a deal here.
for {
// Terminate
select {
case <-ctx.Done():
return
default:
}
var changed []string
// Check known files for deletions
for p := range modified {
_, accessErr := os.Stat(p)
if os.IsNotExist(accessErr) {
changed = append(changed, p)
delete(modified, p)
}
}
// Check given locations for new or modified files
err := yamlcfg.SeekYAMLFiles(paths, func(path string, info os.FileInfo) error {
lastModTime, present := modified[path]
if t := info.ModTime(); !present || t.After(lastModTime) {
changed = append(changed, path)
modified[path] = t
}
return nil
})
if err != nil {
logrus.WithError(err).Error("walk issue in announcer")
return
}
if len(changed) > 0 {
select {
case <-ctx.Done():
return
case channel <- changed:
}
} else {
time.Sleep(pollingTime)
}
}
}
func announceProwChanges(ctx context.Context, pca *prowConfig.Agent, channel chan []string) {
pch := make(chan prowConfig.Delta)
pca.Subscribe(pch)
for {
<-pch
select {
case <-ctx.Done():
return
case channel <- []string{"prow config"}:
}
}
}
func write(ctx context.Context, client *storage.Client, path string, bytes []byte, worldReadable bool, cacheControl string) error {
u, err := url.Parse(path)
if err != nil {
return fmt.Errorf("invalid url %s: %v", path, err)
}
if u.Scheme != "gs" {
return ioutil.WriteFile(path, bytes, 0644)
}
var p gcs.Path
if err = p.SetURL(u); err != nil {
return err
}
return gcs.Upload(ctx, client, p, bytes, worldReadable, cacheControl)
}
// Ignores what changed for now and recomputes everything
func doOneshot(ctx context.Context, client *storage.Client, opt options, prowConfigAgent *prowConfig.Agent) error {
// Read Data Sources: Default, YAML configs, Prow Annotations
c, err := yamlcfg.ReadConfig(opt.inputs, opt.defaultYAML)
if err != nil {
return fmt.Errorf("could not read testgrid config: %v", err)
}
// Remains nil if no default YAML
var d *yamlcfg.DefaultConfiguration
if opt.defaultYAML != "" {
b, err := ioutil.ReadFile(opt.defaultYAML)
if err != nil {
return err
}
val, err := yamlcfg.LoadDefaults(b)
if err != nil {
return err
}
d = &val
}
if err := applyProwjobAnnotations(&c, d, prowConfigAgent); err != nil {
return fmt.Errorf("could not apply prowjob annotations: %v", err)
}
// Print proto if requested
if opt.printText {
if opt.writeYAML {
b, err := yamlcfg.MarshalYAML(c)
if err != nil {
return fmt.Errorf("could not print yaml config: %v", err)
}
os.Stdout.Write(b)
} else if err := tgCfgUtil.MarshalText(c, os.Stdout); err != nil {
return fmt.Errorf("could not print config: %v", err)
}
}
// Write proto if requested
if opt.output != "" {
var b []byte
var err error
if opt.writeYAML {
b, err = yamlcfg.MarshalYAML(c)
} else {
b, err = tgCfgUtil.MarshalBytes(c)
}
if err == nil {
err = write(ctx, client, opt.output, b, opt.worldReadable, "")
}
if err != nil {
return fmt.Errorf("could not write config: %v", err)
}
}
return nil
}
func main() {
// Parse flags
var opt options
if err := opt.gatherOptions(flag.CommandLine, os.Args[1:]); err != nil {
log.Fatalf("Bad flags: %v", err)
}
ctx := context.Background()
prowConfigAgent := &prowConfig.Agent{}
if opt.prowConfig != "" && opt.prowJobConfig != "" {
if err := prowConfigAgent.Start(opt.prowConfig, opt.prowJobConfig); err != nil {
log.Fatalf("FAIL: couldn't load prow config: %v", err)
}
}
// Config file validation only
if opt.validateConfigFile {
if err := doOneshot(ctx, nil, opt, prowConfigAgent); err != nil {
log.Fatalf("FAIL: %v", err)
}
log.Println("Config validated successfully")
return
}
// Set up GCS client if output is to GCS
var client *storage.Client
if strings.HasPrefix(opt.output, "gs://") {
var err error
var creds []string
if opt.creds != "" {
creds = append(creds, opt.creds)
}
client, err = gcs.ClientWithCreds(ctx, creds...)
if err != nil {
log.Fatalf("failed to create gcs client: %v", err)
}
}
// Oneshot mode, write config and exit
if opt.oneshot {
if err := doOneshot(ctx, client, opt, prowConfigAgent); err != nil {
log.Fatalf("FAIL: %v", err)
}
return
}
// Service mode, monitor input files for changes
channel := make(chan []string)
// Monitor files for changes
go announceChanges(ctx, opt.inputs, channel)
go announceProwChanges(ctx, prowConfigAgent, channel)
// Wait for changed files
for changes := range channel {
log.Printf("Changed: %v", changes)
log.Println("Writing config...")
if err := doOneshot(ctx, client, opt, prowConfigAgent); err != nil {
log.Printf("FAIL: %v", err)
continue
}
log.Printf("Wrote config to %s", opt.output)
}
}