-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
main.go
143 lines (117 loc) · 4.39 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
/*
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 (
"bytes"
"flag"
"io/ioutil"
"net/http"
"os/signal"
"strconv"
"syscall"
"github.com/Sirupsen/logrus"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/plugins"
_ "k8s.io/test-infra/prow/plugins/assign"
_ "k8s.io/test-infra/prow/plugins/cla"
_ "k8s.io/test-infra/prow/plugins/close"
_ "k8s.io/test-infra/prow/plugins/heart"
_ "k8s.io/test-infra/prow/plugins/label"
_ "k8s.io/test-infra/prow/plugins/lgtm"
_ "k8s.io/test-infra/prow/plugins/releasenote"
_ "k8s.io/test-infra/prow/plugins/reopen"
_ "k8s.io/test-infra/prow/plugins/trigger"
_ "k8s.io/test-infra/prow/plugins/yuks"
)
var (
port = flag.Int("port", 8888, "Port to listen on.")
configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.")
pluginConfig = flag.String("plugin-config", "/etc/plugins/plugins", "Path to plugin config file.")
local = flag.Bool("local", false, "Run locally for testing purposes only. Does not require secret files.")
dry = flag.Bool("dry", false, "Dry run for testing. Uses API tokens but does not mutate.")
githubBotName = flag.String("github-bot-name", "", "Name of the GitHub bot.")
webhookSecretFile = flag.String("hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
githubTokenFile = flag.String("github-token-file", "/etc/github/oauth", "Path to the file containing the GitHub OAuth secret.")
)
func main() {
flag.Parse()
var webhookSecret []byte
var githubClient *github.Client
var kubeClient *kube.Client
if *local {
logrus.Warning("Running in local mode for dev only.")
logrus.Print("HMAC Secret: abcde12345")
webhookSecret = []byte("abcde12345")
if *githubBotName == "" {
*githubBotName = "fake-robot"
}
githubClient = github.NewFakeClient(*githubBotName)
githubClient.Logger = logrus.StandardLogger()
kubeClient = kube.NewFakeClient()
kubeClient.Logger = logrus.StandardLogger()
} else {
logrus.SetFormatter(&logrus.JSONFormatter{})
// Ignore SIGTERM so that we don't drop hooks when the pod is removed.
// We'll get SIGTERM first and then SIGKILL after our graceful termination
// deadline.
signal.Ignore(syscall.SIGTERM)
webhookSecretRaw, err := ioutil.ReadFile(*webhookSecretFile)
if err != nil {
logrus.WithError(err).Fatal("Could not read webhook secret file.")
}
webhookSecret = bytes.TrimSpace(webhookSecretRaw)
oauthSecretRaw, err := ioutil.ReadFile(*githubTokenFile)
if err != nil {
logrus.WithError(err).Fatal("Could not read oauth secret file.")
}
oauthSecret := string(bytes.TrimSpace(oauthSecretRaw))
if *githubBotName == "" {
logrus.Fatal("Must specify --github-bot-name.")
}
if *dry {
githubClient = github.NewDryRunClient(*githubBotName, oauthSecret)
} else {
githubClient = github.NewClient(*githubBotName, oauthSecret)
}
kubeClient, err = kube.NewClientInCluster(kube.ProwNamespace)
if err != nil {
logrus.WithError(err).Fatal("Error getting kube client.")
}
}
configAgent := &config.ConfigAgent{}
if err := configAgent.Start(*configPath); err != nil {
logrus.WithError(err).Fatal("Error starting config agent.")
}
pluginAgent := &plugins.PluginAgent{
PluginClient: plugins.PluginClient{
GitHubClient: githubClient,
KubeClient: kubeClient,
Logger: logrus.NewEntry(logrus.StandardLogger()),
},
}
if err := pluginAgent.Start(*pluginConfig); err != nil {
logrus.WithError(err).Fatal("Error starting plugins.")
}
server := &Server{
HMACSecret: webhookSecret,
ConfigAgent: configAgent,
Plugins: pluginAgent,
}
// Return 200 on / for health checks.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
// For /hook, handle a webhook normally.
http.Handle("/hook", server)
logrus.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}