-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
157 lines (136 loc) · 4.48 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
/*
Copyright 2024 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 (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/pkg/flagutil"
"k8s.io/test-infra/prow/config/secret"
prowflagutil "k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/plugins"
"sigs.k8s.io/verify-conformance/internal/plugin"
)
const (
pluginName = "verify-conformance"
)
type options struct {
port int
repo string
prEventJSONPath string
dryRun bool
github prowflagutil.GitHubOptions
updatePeriod time.Duration
webhookSecretFile string
}
func (o *options) Validate() error {
for _, group := range []flagutil.OptionGroup{&o.github} {
if err := group.Validate(o.dryRun); err != nil {
return err
}
}
if o.repo == "" {
return fmt.Errorf("repo cannot be empty. Use: 'cncf/k8s-conformance'.")
}
if len(strings.Split(o.repo, "/")) != 2 {
return fmt.Errorf("repo must be formatted as ORG/NAME")
}
return nil
}
func gatherOptions() options {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.IntVar(&o.port, "port", 8888, "Port to listen on.")
fs.StringVar(&o.repo, "repo", "", "GitHub repo to use (i.e: 'cncf/k8s-conformance' or 'cncf-infra/k8s-conformance').")
fs.StringVar(&o.prEventJSONPath, "pr-event-json-path", "", "path to a GitHub workflow event.json file")
fs.BoolVar(&o.dryRun, "dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.")
fs.DurationVar(&o.updatePeriod, "update-period", time.Hour*24, "Period duration for periodic scans of all PRs.")
fs.StringVar(&o.webhookSecretFile, "hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
for _, group := range []flagutil.OptionGroup{&o.github} {
group.AddFlags(fs)
}
if err := fs.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Fatal("error parsing args[1:]")
}
return o
}
func main() {
o := gatherOptions()
if err := o.Validate(); err != nil {
logrus.Fatalf("Invalid options: %v", err)
}
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", pluginName)
secrets := []string{}
if o.github.TokenPath != "" {
secrets = append(secrets, o.github.TokenPath)
}
if o.github.AppPrivateKeyPath != "" {
secrets = append(secrets, o.github.AppPrivateKeyPath)
}
if o.webhookSecretFile != "" {
secrets = append(secrets, o.webhookSecretFile)
}
if err := secret.Add(secrets...); err != nil {
logrus.WithError(err).Fatal("Error starting test-infra/prow/config/secret agent.")
}
githubClient, err := o.github.GitHubClient(o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
}
if err := githubClient.Throttle(360, 360); err != nil {
logrus.WithError(err).Fatal("error: throttling GitHub client")
}
if file := o.prEventJSONPath; file != "" {
_, err := os.Stat(o.prEventJSONPath)
if err == nil {
payload, err := os.ReadFile(file)
if err != nil {
logrus.WithError(err).Fatal("Error reading PR event.json file.")
return
}
var pre github.PullRequestEvent
if err := json.Unmarshal(payload, &pre); err != nil {
logrus.WithError(err).Fatal("Error unmarshalling PR event.json file.")
return
}
// TODO: resolve issue with org repo name (https://github.com/cncf-infra/verify-conformance/issues/180)
if err := plugin.HandlePullRequestEvent(log, githubClient, &pre); err != nil {
log.WithError(err).Info("Error handling event.")
}
return
} else if os.IsNotExist(err) {
logrus.WithError(err).Fatal("Error finding PR event.json file.")
return
}
}
if err := plugin.HandleAll(log, githubClient, &plugins.Configuration{
ExternalPlugins: map[string][]plugins.ExternalPlugin{
o.repo: {{
Name: pluginName,
Events: []string{
"issue_comment",
"pull_request",
},
}},
},
}); err != nil {
log.WithError(err).Error("Error during periodic update of all PRs.")
}
}