-
Notifications
You must be signed in to change notification settings - Fork 214
/
hook_manager.go
363 lines (301 loc) · 11.1 KB
/
hook_manager.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package hook
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
log "github.com/sirupsen/logrus"
. "github.com/flant/shell-operator/pkg/hook/types"
. "github.com/flant/shell-operator/pkg/kube_events_manager/types"
. "github.com/flant/shell-operator/pkg/webhook/validating/types"
"github.com/flant/shell-operator/pkg/executor"
"github.com/flant/shell-operator/pkg/hook/controller"
"github.com/flant/shell-operator/pkg/kube_events_manager"
"github.com/flant/shell-operator/pkg/schedule_manager"
utils_file "github.com/flant/shell-operator/pkg/utils/file"
"github.com/flant/shell-operator/pkg/webhook/conversion"
"github.com/flant/shell-operator/pkg/webhook/validating"
)
type HookManager interface {
Init() error
Run()
WithDirectories(workingDir string, tempDir string)
WithKubeEventManager(kube_events_manager.KubeEventsManager)
WithScheduleManager(schedule_manager.ScheduleManager)
WithConversionWebhookManager(*conversion.WebhookManager)
WithValidatingWebhookManager(*validating.WebhookManager)
WorkingDir() string
TempDir() string
GetHook(name string) *Hook
GetHookNames() []string
GetHooksInOrder(bindingType BindingType) ([]string, error)
HandleKubeEvent(kubeEvent KubeEvent, createTaskFn func(*Hook, controller.BindingExecutionInfo))
HandleScheduleEvent(crontab string, createTaskFn func(*Hook, controller.BindingExecutionInfo))
HandleValidatingEvent(event ValidatingEvent, createTaskFn func(*Hook, controller.BindingExecutionInfo))
HandleConversionEvent(event conversion.Event, rule conversion.Rule, createTaskFn func(*Hook, controller.BindingExecutionInfo))
FindConversionChain(crdName string, rule conversion.Rule) []conversion.Rule
}
type hookManager struct {
// dependencies
workingDir string
tempDir string
kubeEventsManager kube_events_manager.KubeEventsManager
scheduleManager schedule_manager.ScheduleManager
conversionWebhookManager *conversion.WebhookManager
validatingWebhookManager *validating.WebhookManager
// sorted hook names
hookNamesInOrder []string
// index by name
hooksByName map[string]*Hook
// index to search hooks by binding type
hooksInOrder map[BindingType][]*Hook
// Index crdName -> fromVersion -> conversionLink
conversionChains *conversion.ChainStorage
}
// hookManager should implement HookManager
var _ HookManager = &hookManager{}
func NewHookManager() *hookManager {
return &hookManager{
hooksByName: make(map[string]*Hook),
hookNamesInOrder: make([]string, 0),
hooksInOrder: make(map[BindingType][]*Hook),
conversionChains: conversion.NewChainStorage(),
}
}
func (hm *hookManager) WithDirectories(workingDir string, tempDir string) {
hm.workingDir = workingDir
hm.tempDir = tempDir
}
func (hm *hookManager) WithKubeEventManager(mgr kube_events_manager.KubeEventsManager) {
hm.kubeEventsManager = mgr
}
func (hm *hookManager) WithScheduleManager(mgr schedule_manager.ScheduleManager) {
hm.scheduleManager = mgr
}
func (hm *hookManager) WithValidatingWebhookManager(mgr *validating.WebhookManager) {
hm.validatingWebhookManager = mgr
}
func (hm *hookManager) WithConversionWebhookManager(mgr *conversion.WebhookManager) {
hm.conversionWebhookManager = mgr
}
func (hm *hookManager) WorkingDir() string {
return hm.workingDir
}
func (hm *hookManager) TempDir() string {
return hm.tempDir
}
// Init finds executables in WorkingDir, execute them with --config argument and add them into indices.
func (hm *hookManager) Init() error {
log.Info("Initialize hooks manager. Search for and load all hooks.")
hm.hooksInOrder = make(map[BindingType][]*Hook)
hm.hooksByName = make(map[string]*Hook)
hooksRelativePaths, err := utils_file.RecursiveGetExecutablePaths(hm.workingDir)
if err != nil {
return err
}
// sort hooks by path
sort.Strings(hooksRelativePaths)
log.Debugf(" Search hooks in this paths: %+v", hooksRelativePaths)
for _, hookPath := range hooksRelativePaths {
hook, err := hm.loadHook(hookPath)
if err != nil {
return err
}
// register hook in indices
for _, binding := range hook.Config.Bindings() {
hm.hooksInOrder[binding] = append(hm.hooksInOrder[binding], hook)
}
hm.hooksByName[hook.Name] = hook
hm.hookNamesInOrder = append(hm.hookNamesInOrder, hook.Name)
}
// Validate conversion chains and create index with conversion paths.
err = hm.UpdateConversionChains()
if err != nil {
return fmt.Errorf("check conversion configs: %v", err)
}
return nil
}
// TODO move --config execution to a Hook method
func (hm *hookManager) loadHook(hookPath string) (hook *Hook, err error) {
hookName, err := filepath.Rel(hm.workingDir, hookPath)
if err != nil {
return nil, err
}
hook = NewHook(hookName, hookPath)
hookEntry := log.WithField("hook", hook.Name).
WithField("phase", "config")
hookEntry.Infof("Load config from '%s'", hookPath)
envs := []string{}
configOutput, err := hm.execCommandOutput(hook.Name, hm.workingDir, hookPath, envs, []string{"--config"})
if err != nil {
hookEntry.Errorf("Hook config output:\n%s", string(configOutput))
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
hookEntry.Errorf("Hook config stderr:\n%s", string(ee.Stderr))
}
return nil, fmt.Errorf("cannot get config for hook '%s': %s", hookPath, err)
}
_, err = hook.LoadConfig(configOutput)
if err != nil {
return nil, fmt.Errorf("creating hook '%s': %s", hookName, err.Error())
}
// Add hook info as log labels, update MetricLabels
for _, kubeCfg := range hook.GetConfig().OnKubernetesEvents {
kubeCfg.Monitor.Metadata.LogLabels["hook"] = hook.Name
kubeCfg.Monitor.Metadata.MetricLabels = map[string]string{
"hook": hook.Name,
"binding": kubeCfg.BindingName,
"queue": kubeCfg.Queue,
}
}
for _, conversionCfg := range hook.GetConfig().KubernetesConversion {
conversionCfg.Webhook.Metadata.LogLabels["hook"] = hook.Name
conversionCfg.Webhook.Metadata.MetricLabels = map[string]string{
"hook": hook.Name,
"binding": conversionCfg.BindingName,
}
}
for _, validatingCfg := range hook.GetConfig().KubernetesValidating {
validatingCfg.Webhook.Metadata.LogLabels["hook"] = hook.Name
validatingCfg.Webhook.Metadata.MetricLabels = map[string]string{
"hook": hook.Name,
"binding": validatingCfg.BindingName,
}
validatingCfg.Webhook.UpdateIds("", validatingCfg.BindingName)
}
hookCtrl := controller.NewHookController()
hookCtrl.InitKubernetesBindings(hook.GetConfig().OnKubernetesEvents, hm.kubeEventsManager)
hookCtrl.InitScheduleBindings(hook.GetConfig().Schedules, hm.scheduleManager)
hookCtrl.InitConversionBindings(hook.GetConfig().KubernetesConversion, hm.conversionWebhookManager)
hookCtrl.InitValidatingBindings(hook.GetConfig().KubernetesValidating, hm.validatingWebhookManager)
hook.WithHookController(hookCtrl)
hook.WithTmpDir(hm.TempDir())
hookEntry.Infof("Loaded config: %s", hook.GetConfigDescription())
return hook, nil
}
func (hm *hookManager) execCommandOutput(hookName string, dir string, entrypoint string, envs []string, args []string) ([]byte, error) {
envs = append(os.Environ(), envs...)
cmd := executor.MakeCommand(dir, entrypoint, args, envs)
cmd.Stdout = nil
cmd.Stderr = nil
debugEntry := log.WithField("hook", hookName).
WithField("cmd", strings.Join(cmd.Args, " "))
debugEntry.Debugf("Executing hook in %s", cmd.Dir)
output, err := executor.Output(cmd)
if err != nil {
return output, err
}
debugEntry.Debugf("output:\n%s", string(output))
return output, nil
}
// HookManager has no events for now.
func (hm *hookManager) Run() {
panic("implement me")
}
func (hm *hookManager) GetHook(name string) *Hook {
hook, exists := hm.hooksByName[name]
if exists {
return hook
} else {
log.Errorf("Possible bug!!! Hook '%s' not found in hook manager", name)
return nil
}
}
func (hm *hookManager) GetHookNames() []string {
return hm.hookNamesInOrder
}
func (hm *hookManager) GetHooksInOrder(bindingType BindingType) ([]string, error) {
hooks, ok := hm.hooksInOrder[bindingType]
if !ok {
return []string{}, nil
}
// OnStartup hooks are sorted by onStartup config value
// FIXME: onStartup value is now a config validating error, no need to check it here again.
if bindingType == OnStartup {
for _, hook := range hooks {
if !hook.Config.HasBinding(OnStartup) {
return nil, fmt.Errorf("possible bug: hook '%s' is registered as OnStartup but has no onStartup value", hook.Name)
}
}
sort.Slice(hooks[:], func(i, j int) bool {
return hooks[i].Config.OnStartup.Order < hooks[j].Config.OnStartup.Order
})
}
var hooksNames []string
for _, hook := range hooks {
hooksNames = append(hooksNames, hook.Name)
}
return hooksNames, nil
}
func (hm *hookManager) HandleKubeEvent(kubeEvent KubeEvent, createTaskFn func(*Hook, controller.BindingExecutionInfo)) {
kubeHooks, _ := hm.GetHooksInOrder(OnKubernetesEvent)
for _, hookName := range kubeHooks {
h := hm.GetHook(hookName)
if h.HookController.CanHandleKubeEvent(kubeEvent) {
h.HookController.HandleKubeEvent(kubeEvent, func(info controller.BindingExecutionInfo) {
if createTaskFn != nil {
createTaskFn(h, info)
}
})
}
}
}
func (hm *hookManager) HandleScheduleEvent(crontab string, createTaskFn func(*Hook, controller.BindingExecutionInfo)) {
schHooks, _ := hm.GetHooksInOrder(Schedule)
for _, hookName := range schHooks {
h := hm.GetHook(hookName)
if h.HookController.CanHandleScheduleEvent(crontab) {
h.HookController.HandleScheduleEvent(crontab, func(info controller.BindingExecutionInfo) {
if createTaskFn != nil {
createTaskFn(h, info)
}
})
}
}
}
func (hm *hookManager) HandleValidatingEvent(event ValidatingEvent, createTaskFn func(*Hook, controller.BindingExecutionInfo)) {
vHooks, _ := hm.GetHooksInOrder(KubernetesValidating)
for _, hookName := range vHooks {
h := hm.GetHook(hookName)
if h.HookController.CanHandleValidatingEvent(event) {
h.HookController.HandleValidatingEvent(event, func(info controller.BindingExecutionInfo) {
if createTaskFn != nil {
createTaskFn(h, info)
}
})
}
}
}
// HandleConversionEvent receives a crdName and calculates a sequence of hooks to run.
func (hm *hookManager) HandleConversionEvent(event conversion.Event, rule conversion.Rule, createTaskFn func(*Hook, controller.BindingExecutionInfo)) {
vHooks, _ := hm.GetHooksInOrder(KubernetesConversion)
for _, hookName := range vHooks {
h := hm.GetHook(hookName)
if h.HookController.CanHandleConversionEvent(event, rule) {
h.HookController.HandleConversionEvent(event, rule, func(info controller.BindingExecutionInfo) {
if createTaskFn != nil {
createTaskFn(h, info)
}
})
}
}
}
func (hm *hookManager) UpdateConversionChains() error {
vHooks, _ := hm.GetHooksInOrder(KubernetesConversion)
// Update conversionChains.
for _, hookName := range vHooks {
h := hm.GetHook(hookName)
for _, cfg := range h.Config.KubernetesConversion {
crdName := cfg.Webhook.CrdName
chain := hm.conversionChains.Get(crdName)
for _, conversionRule := range cfg.Webhook.Rules {
chain.Put(conversionRule)
}
}
}
return nil
}
func (hm *hookManager) FindConversionChain(crdName string, rule conversion.Rule) []conversion.Rule {
return hm.conversionChains.FindConversionChain(crdName, rule)
}