-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathschedule_bindings_controller.go
More file actions
128 lines (110 loc) · 3.65 KB
/
Copy pathschedule_bindings_controller.go
File metadata and controls
128 lines (110 loc) · 3.65 KB
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
package controller
import (
"sync"
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
htypes "github.com/flant/shell-operator/pkg/hook/types"
schedulemanager "github.com/flant/shell-operator/pkg/schedule_manager"
)
// ScheduleBindingToCrontabLink a link between a hook and a kube monitor
type ScheduleBindingToCrontabLink struct {
BindingName string
Crontab string
// Useful fields to create a BindingContext
IncludeSnapshots []string
AllowFailure bool
QueueName string
Group string
}
// ScheduleBindingsController handles schedule bindings for one hook.
type ScheduleBindingsController interface {
WithScheduleBindings([]htypes.ScheduleConfig)
WithScheduleManager(schedulemanager.ScheduleManager)
EnableScheduleBindings()
DisableScheduleBindings()
CanHandleEvent(crontab string) bool
HandleEvent(crontab string) []BindingExecutionInfo
}
// scheduleBindingsController is a main implementation of KubernetesHooksController
type scheduleBindingsController struct {
// dependencies
scheduleManager schedulemanager.ScheduleManager
l sync.RWMutex
// All hooks with 'kubernetes' bindings
ScheduleLinks map[string]*ScheduleBindingToCrontabLink
// bindings configurations
ScheduleBindings []htypes.ScheduleConfig
}
// kubernetesHooksController should implement the KubernetesHooksController
var _ ScheduleBindingsController = (*scheduleBindingsController)(nil)
// NewScheduleBindingsController returns an implementation of ScheduleBindingsController
var NewScheduleBindingsController = func() *scheduleBindingsController {
return &scheduleBindingsController{
ScheduleLinks: make(map[string]*ScheduleBindingToCrontabLink),
}
}
func (c *scheduleBindingsController) WithScheduleBindings(bindings []htypes.ScheduleConfig) {
c.ScheduleBindings = bindings
}
func (c *scheduleBindingsController) WithScheduleManager(scheduleManager schedulemanager.ScheduleManager) {
c.l.Lock()
c.scheduleManager = scheduleManager
c.l.Unlock()
}
func (c *scheduleBindingsController) CanHandleEvent(crontab string) bool {
c.l.RLock()
defer c.l.RUnlock()
for _, link := range c.ScheduleLinks {
if link.Crontab == crontab {
return true
}
}
return false
}
func (c *scheduleBindingsController) HandleEvent(crontab string) []BindingExecutionInfo {
res := []BindingExecutionInfo{}
c.l.RLock()
for _, link := range c.ScheduleLinks {
if link.Crontab == crontab {
bc := bctx.BindingContext{
Binding: link.BindingName,
}
bc.Metadata.BindingType = htypes.Schedule
bc.Metadata.IncludeSnapshots = link.IncludeSnapshots
bc.Metadata.Group = link.Group
info := BindingExecutionInfo{
BindingContext: []bctx.BindingContext{bc},
IncludeSnapshots: link.IncludeSnapshots,
AllowFailure: link.AllowFailure,
QueueName: link.QueueName,
Binding: link.BindingName,
Group: link.Group,
}
res = append(res, info)
}
}
c.l.RUnlock()
return res
}
func (c *scheduleBindingsController) EnableScheduleBindings() {
c.l.Lock()
for _, config := range c.ScheduleBindings {
c.ScheduleLinks[config.ScheduleEntry.Id] = &ScheduleBindingToCrontabLink{
BindingName: config.BindingName,
Crontab: config.ScheduleEntry.Crontab,
IncludeSnapshots: config.IncludeSnapshotsFrom,
AllowFailure: config.AllowFailure,
QueueName: config.Queue,
Group: config.Group,
}
c.scheduleManager.Add(config.ScheduleEntry)
}
c.l.Unlock()
}
func (c *scheduleBindingsController) DisableScheduleBindings() {
c.l.Lock()
for _, config := range c.ScheduleBindings {
c.scheduleManager.Remove(config.ScheduleEntry)
delete(c.ScheduleLinks, config.ScheduleEntry.Id)
}
c.l.Unlock()
}