/
main.go
134 lines (110 loc) · 2.91 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/pkg/errors"
"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/interrupts"
"k8s.io/test-infra/prow/pluginhelp/externalplugins"
"k8s.io/test-infra/prow/plugins"
"github.com/kubernetes-analysis/kubernetes-analysis/pkg/plugin"
"github.com/kubernetes-analysis/kubernetes-analysis/pkg/server"
)
const (
port = 8888
pluginName = "kubernetes-analysis"
)
type options struct {
pluginConfig string
webhookSecretFile string
dryRun bool
github prowflagutil.GitHubOptions
}
func (o *options) validate() error {
for idx, group := range []flagutil.OptionGroup{&o.github} {
if err := group.Validate(o.dryRun); err != nil {
return fmt.Errorf("%d: %w", idx, err)
}
}
return nil
}
func gatherOptions() (*options, error) {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(
&o.pluginConfig,
"plugin-config",
"/etc/plugins/plugins.yaml",
"Path to plugin config file.",
)
fs.BoolVar(
&o.dryRun,
"dry-run",
true,
"Dry run for testing. Uses API tokens but does not mutate.",
)
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 {
return nil, errors.Wrap(err, "parsing CLI args")
}
return &o, nil
}
func main() {
if err := run(); err != nil {
logrus.Fatal(err)
}
}
func run() error {
logrus.SetFormatter(&logrus.JSONFormatter{DisableTimestamp: true})
logrus.SetLevel(logrus.InfoLevel)
o, err := gatherOptions()
if err != nil {
return errors.Wrap(err, "gathering options")
}
if err := o.validate(); err != nil {
return errors.Wrap(err, "validating options")
}
secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{
o.github.TokenPath, o.webhookSecretFile,
}); err != nil {
return errors.Wrap(err, "starting secrets agent")
}
pa := &plugins.ConfigAgent{}
if err := pa.Start(o.pluginConfig, false); err != nil {
return errors.Wrap(err, "starting config agent")
}
githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
return errors.Wrap(err, "getting GitHub client")
}
githubClient.Throttle(360, 360)
log := logrus.StandardLogger().WithField("plugin", pluginName)
s := server.New(
secretAgent.GetTokenGenerator(o.webhookSecretFile),
githubClient,
log,
)
defer interrupts.WaitForGracefulShutdown()
mux := http.NewServeMux()
mux.Handle("/", s)
externalplugins.ServeExternalPluginHelp(mux, log, plugin.HelpProvider)
httpServer := &http.Server{Addr: ":" + strconv.Itoa(port), Handler: mux}
interrupts.ListenAndServe(httpServer, 5*time.Second)
return nil
}