forked from openshift/ci-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
259 lines (229 loc) · 8.83 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"regexp"
"sync"
"time"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"gopkg.in/fsnotify.v1"
"gopkg.in/yaml.v2"
"github.com/spf13/pflag"
citools "github.com/openshift/ci-tools/pkg/api"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
imageclientset "github.com/openshift/client-go/image/clientset/versioned"
projectclientset "github.com/openshift/client-go/project/clientset/versioned"
)
var (
reBuildClusterName = regexp.MustCompile(`(.*).kubeconfig`)
)
type options struct {
prowconfig configflagutil.ConfigOptions
GithubEndpoint string
ForcePROwner string
BuildClusterKubeconfig string
BuildClusterKubeconfigsLocation string
ReleaseClusterKubeconfig string
ConfigResolver string
WorkflowConfigPath string
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
emptyFlags := flag.NewFlagSet("empty", flag.ContinueOnError)
klog.InitFlags(emptyFlags)
opt := &options{
prowconfig: configflagutil.ConfigOptions{
ConfigPathFlagName: "prow-config",
JobConfigPathFlagName: "job-config",
},
GithubEndpoint: "https://api.github.com",
ConfigResolver: "http://config.ci.openshift.org/config",
BuildClusterKubeconfigsLocation: "/var/build-cluster-kubeconfigs",
}
var ignored string
pflag.StringVar(&opt.ConfigResolver, "config-resolver", opt.ConfigResolver, "A URL pointing to a config resolver for retrieving ci-operator config. You may pass a location on disk with file://<abs_path_to_ci_operator_config>")
pflag.StringVar(&opt.GithubEndpoint, "github-endpoint", opt.GithubEndpoint, "An optional proxy for connecting to github.")
pflag.StringVar(&opt.ForcePROwner, "force-pr-owner", opt.ForcePROwner, "Make the supplied user the owner of all PRs for access control purposes.")
pflag.StringVar(&ignored, "build-cluster-kubeconfig", ignored, "REMOVED: Kubeconfig to use for buildcluster. Defaults to normal kubeconfig if unset.")
pflag.StringVar(&opt.BuildClusterKubeconfigsLocation, "build-cluster-kubeconfigs-location", opt.BuildClusterKubeconfigsLocation, "Path to the location of the Kubeconfigs for the various buildclusters. Default is \"/var/build-cluster-kubeconfigs\".")
pflag.StringVar(&opt.ReleaseClusterKubeconfig, "release-cluster-kubeconfig", "", "Kubeconfig to use for cluster housing the release imagestreams. Defaults to normal kubeconfig if unset.")
pflag.StringVar(&opt.WorkflowConfigPath, "workflow-config-path", "", "Path to config file used for workflow commands")
opt.prowconfig.AddFlags(emptyFlags)
pflag.CommandLine.AddGoFlagSet(emptyFlags)
pflag.Parse()
klog.SetOutput(os.Stderr)
buildClusterClientConfigs, err := readBuildClusterKubeConfigs(opt.BuildClusterKubeconfigsLocation)
if err != nil {
return fmt.Errorf("unable to load build cluster configurations: %v", err)
}
if err := setupKubeconfigWatches(buildClusterClientConfigs); err != nil {
klog.Warningf("failed to set up kubeconfig watches: %v", err)
}
resolverURL, err := url.Parse(opt.ConfigResolver)
if err != nil {
return fmt.Errorf("--config-resolver is not a valid URL: %v", err)
}
resolver := &URLConfigResolver{URL: resolverURL}
botToken := os.Getenv("BOT_TOKEN")
if len(botToken) == 0 {
return fmt.Errorf("the environment variable BOT_TOKEN must be set")
}
prowJobKubeconfig, _, _, err := loadKubeconfig()
if err != nil {
return err
}
dynamicClient, err := dynamic.NewForConfig(prowJobKubeconfig)
if err != nil {
return fmt.Errorf("unable to create prow client: %v", err)
}
prowClient := dynamicClient.Resource(schema.GroupVersionResource{Group: "prow.k8s.io", Version: "v1", Resource: "prowjobs"})
// Config and Client to access release images
releaseConfig, err := loadKubeconfigFromFlagOrDefault(opt.ReleaseClusterKubeconfig, prowJobKubeconfig)
imageClient, err := imageclientset.NewForConfig(releaseConfig)
if err != nil {
return fmt.Errorf("unable to create image client: %v", err)
}
configAgent, err := opt.prowconfig.ConfigAgent()
if err != nil {
return err
}
workflows := WorkflowConfig{}
go manageWorkflowConfig(opt.WorkflowConfigPath, &workflows)
manager := NewJobManager(configAgent, resolver, prowClient, imageClient, buildClusterClientConfigs, opt.GithubEndpoint, opt.ForcePROwner, &workflows)
if err := manager.Start(); err != nil {
return fmt.Errorf("unable to load initial configuration: %v", err)
}
bot := NewBot(botToken, &workflows)
for {
if err := bot.Start(manager); err != nil && !isRetriable(err) {
return err
}
time.Sleep(5 * time.Second)
}
}
type BuildClusterClientConfig struct {
KubeconfigPath string
CoreConfig *rest.Config
CoreClient *clientset.Clientset
ProjectClient *projectclientset.Clientset
TargetImageClient *imageclientset.Clientset
}
type BuildClusterClientConfigMap map[string]*BuildClusterClientConfig
func readBuildClusterKubeConfigs(location string) (BuildClusterClientConfigMap, error) {
files, err := ioutil.ReadDir(location)
if err != nil {
return nil, fmt.Errorf("unable to access location %q: %v", location, err)
}
clusterMap := make(BuildClusterClientConfigMap)
for _, file := range files {
if file.IsDir() {
continue
}
fullPath := path.Join(location, file.Name())
if m := reBuildClusterName.FindStringSubmatch(file.Name()); m != nil {
contents, err := ioutil.ReadFile(fullPath)
if err != nil {
return nil, fmt.Errorf("unable to access kubeconfig %q: %v", file.Name(), err)
}
cfg, err := clientcmd.NewClientConfigFromBytes(contents)
if err != nil {
return nil, fmt.Errorf("could not load build client configuration: %v", err)
}
clusterConfig, err := cfg.ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not load cluster configuration: %v", err)
}
coreClient, err := clientset.NewForConfig(clusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to create core client: %v", err)
}
targetImageClient, err := imageclientset.NewForConfig(clusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to create target image client: %v", err)
}
projectClient, err := projectclientset.NewForConfig(clusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to create project client: %v", err)
}
clusterMap[m[1]] = &BuildClusterClientConfig{
KubeconfigPath: fullPath,
CoreConfig: clusterConfig,
CoreClient: coreClient,
ProjectClient: projectClient,
TargetImageClient: targetImageClient,
}
}
}
return clusterMap, nil
}
func setupKubeconfigWatches(clusters BuildClusterClientConfigMap) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to set up watcher: %w", err)
}
for _, cluster := range clusters {
if _, err := os.Stat(cluster.KubeconfigPath); err != nil {
continue
}
if err := watcher.Add(cluster.KubeconfigPath); err != nil {
return fmt.Errorf("failed to watch %v: %w", cluster, err)
}
}
go func() {
for e := range watcher.Events {
if e.Op == fsnotify.Chmod {
// For some reason we get frequent chmod events from Openshift
continue
}
klog.Infof("event: %s, kubeconfig changed, exiting to make the kubelet restart us so we can pick them up", e.String())
os.Exit(0)
}
}()
return nil
}
type WorkflowConfig struct {
Workflows map[string]WorkflowConfigItem `yaml:"workflows"`
mutex sync.RWMutex `yaml:"-"` // this field just allows us to update the above values without races
}
type WorkflowConfigItem struct {
BaseImages map[string]citools.ImageStreamTagReference `yaml:"base_images,omitempty"`
Architecture string `yaml:"architecture,omitempty"`
Platform string `yaml:"platform"`
}
func manageWorkflowConfig(path string, workflows *WorkflowConfig) {
for {
// To prevent the ci-chat-bot from crashlooping due to a bad config change,
// we will only log that the config is broken and set the workflows to an
// empty map. To prevent broken configs in the future, a presubmit should
// be creating for openshift/release that verifies this config.
var config WorkflowConfig
rawConfig, err := ioutil.ReadFile(path)
if err != nil {
klog.Errorf("Failed to load workflow config file at %s: %v", path, err)
} else if err := yaml.Unmarshal(rawConfig, &config); err != nil {
klog.Errorf("Failed to unmarshal workflow config: %v", err)
}
workflows.mutex.Lock()
if config.Workflows != nil {
workflows.Workflows = config.Workflows
} else {
workflows.Workflows = make(map[string]WorkflowConfigItem)
}
workflows.mutex.Unlock()
time.Sleep(2 * time.Minute)
}
}