Skip to content

Commit ed1506f

Browse files
authored
[feat] idempotent kubernetes hooks enabling (#866)
Signed-off-by: Stepan Paksashvili <stepan.paksashvili@flant.com>
1 parent 4f12b98 commit ed1506f

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

pkg/hook/controller/hook_controller.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,25 @@ func (hc *HookController) UnlockKubernetesEventsFor(monitorID string) {
252252
}
253253
}
254254

255-
func (hc *HookController) StopMonitors() {
255+
func (hc *HookController) UpdateMonitor(monitorId string, kind, apiVersion string) error {
256256
if hc.KubernetesController != nil {
257-
hc.KubernetesController.StopMonitors()
257+
return hc.KubernetesController.UpdateMonitor(monitorId, kind, apiVersion)
258258
}
259+
return nil
259260
}
260261

261-
func (hc *HookController) UpdateMonitor(monitorId string, kind, apiVersion string) error {
262+
func (hc *HookController) EnableKubernetesBindings() ([]BindingExecutionInfo, error) {
262263
if hc.KubernetesController != nil {
263-
return hc.KubernetesController.UpdateMonitor(monitorId, kind, apiVersion)
264+
return hc.KubernetesController.EnableKubernetesBindings()
265+
}
266+
267+
return nil, nil
268+
}
269+
270+
func (hc *HookController) DisableKubernetesBindings() {
271+
if hc.KubernetesController != nil {
272+
hc.KubernetesController.DisableKubernetesBindings()
264273
}
265-
return nil
266274
}
267275

268276
func (hc *HookController) EnableScheduleBindings() {

pkg/hook/controller/kubernetes_bindings_controller.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/deckhouse/deckhouse/pkg/log"
1010

11-
pkg "github.com/flant/shell-operator/pkg"
11+
"github.com/flant/shell-operator/pkg"
1212
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
1313
htypes "github.com/flant/shell-operator/pkg/hook/types"
1414
kubeeventsmanager "github.com/flant/shell-operator/pkg/kube_events_manager"
@@ -27,10 +27,10 @@ type KubernetesBindingsController interface {
2727
WithKubernetesBindings([]htypes.OnKubernetesEventConfig)
2828
WithKubeEventsManager(kubeeventsmanager.KubeEventsSource)
2929
EnableKubernetesBindings() ([]BindingExecutionInfo, error)
30+
DisableKubernetesBindings()
3031
UpdateMonitor(monitorId string, kind, apiVersion string) error
3132
UnlockEvents()
3233
UnlockEventsFor(monitorID string)
33-
StopMonitors()
3434
CanHandleEvent(kubeEvent kemtypes.KubeEvent) bool
3535
HandleEvent(ctx context.Context, kubeEvent kemtypes.KubeEvent) BindingExecutionInfo
3636
BindingNames() []string
@@ -82,17 +82,25 @@ func (c *kubernetesBindingsController) WithKubeEventsManager(kubeEventsManager k
8282
func (c *kubernetesBindingsController) EnableKubernetesBindings() ([]BindingExecutionInfo, error) {
8383
res := make([]BindingExecutionInfo, 0)
8484

85+
c.l.RLock()
86+
alreadyEnabled := len(c.BindingMonitorLinks) == len(c.KubernetesBindings)
87+
c.l.RUnlock()
88+
if alreadyEnabled {
89+
return res, nil
90+
}
91+
8592
for _, config := range c.KubernetesBindings {
86-
err := c.kubeEventsManager.AddMonitor(config.Monitor)
87-
if err != nil {
88-
return nil, fmt.Errorf("run monitor: %s", err)
93+
if _, found := c.getBindingMonitorLinksById(config.Monitor.Metadata.MonitorId); !found {
94+
if err := c.kubeEventsManager.AddMonitor(config.Monitor); err != nil {
95+
return nil, fmt.Errorf("run monitor: %s", err)
96+
}
97+
c.setBindingMonitorLinks(config.Monitor.Metadata.MonitorId, &KubernetesBindingToMonitorLink{
98+
MonitorId: config.Monitor.Metadata.MonitorId,
99+
BindingConfig: config,
100+
})
101+
// Start monitor's informers to fill the cache.
102+
c.kubeEventsManager.StartMonitor(config.Monitor.Metadata.MonitorId)
89103
}
90-
c.setBindingMonitorLinks(config.Monitor.Metadata.MonitorId, &KubernetesBindingToMonitorLink{
91-
MonitorId: config.Monitor.Metadata.MonitorId,
92-
BindingConfig: config,
93-
})
94-
// Start monitor's informers to fill the cache.
95-
c.kubeEventsManager.StartMonitor(config.Monitor.Metadata.MonitorId)
96104

97105
synchronizationInfo := c.HandleEvent(context.TODO(), kemtypes.KubeEvent{
98106
MonitorId: config.Monitor.Metadata.MonitorId,
@@ -173,13 +181,18 @@ func (c *kubernetesBindingsController) UnlockEventsFor(monitorID string) {
173181
m.EnableKubeEventCb()
174182
}
175183

176-
// StopMonitors stops all monitors for the hook.
177-
// TODO handle error!
178-
func (c *kubernetesBindingsController) StopMonitors() {
179-
c.iterateBindingMonitorLinks(func(monitorID string) bool {
180-
_ = c.kubeEventsManager.StopMonitor(monitorID)
181-
return false
182-
})
184+
func (c *kubernetesBindingsController) DisableKubernetesBindings() {
185+
c.l.Lock()
186+
ids := make([]string, 0, len(c.BindingMonitorLinks))
187+
for id := range c.BindingMonitorLinks {
188+
ids = append(ids, id)
189+
}
190+
c.BindingMonitorLinks = make(map[string]*KubernetesBindingToMonitorLink)
191+
c.l.Unlock()
192+
193+
for _, id := range ids {
194+
_ = c.kubeEventsManager.StopMonitor(id)
195+
}
183196
}
184197

185198
func (c *kubernetesBindingsController) CanHandleEvent(kubeEvent kemtypes.KubeEvent) bool {

pkg/kube_events_manager/kube_events_manager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
metricsstorage "github.com/deckhouse/deckhouse/pkg/metrics-storage"
1212

1313
klient "github.com/flant/kube-client/client"
14-
pkg "github.com/flant/shell-operator/pkg"
14+
"github.com/flant/shell-operator/pkg"
1515
kemtypes "github.com/flant/shell-operator/pkg/kube_events_manager/types"
1616
)
1717

@@ -92,9 +92,8 @@ func (mgr *kubeEventsManager) WithMetricStorage(mstor metricsstorage.Storage) {
9292
// TODO cleanup informers in case of error
9393
// TODO use Context to stop informers
9494
func (mgr *kubeEventsManager) AddMonitor(monitorConfig *MonitorConfig) error {
95-
log.Debug("Add MONITOR",
96-
slog.String(pkg.LogKeyConfig, fmt.Sprintf("%+v", monitorConfig)))
97-
monitor := NewMonitor(
95+
mgr.logger.Debug("add kubernetes monitor", slog.String(pkg.LogKeyConfig, fmt.Sprintf("%+v", monitorConfig)))
96+
mon := NewMonitor(
9897
mgr.ctx,
9998
mgr.KubeClient,
10099
mgr.metricStorage,
@@ -107,13 +106,12 @@ func (mgr *kubeEventsManager) AddMonitor(monitorConfig *MonitorConfig) error {
107106
mgr.logger.Named("monitor"),
108107
)
109108

110-
err := monitor.CreateInformers()
111-
if err != nil {
109+
if err := mon.CreateInformers(); err != nil {
112110
return err
113111
}
114112

115113
mgr.m.Lock()
116-
mgr.Monitors[monitorConfig.Metadata.MonitorId] = monitor
114+
mgr.Monitors[monitorConfig.Metadata.MonitorId] = mon
117115
mgr.m.Unlock()
118116

119117
return nil

test/hook/context/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,6 @@ func (b *BindingContextController) RunBindingWithAllSnapshots(binding types.Bind
195195

196196
func (b *BindingContextController) Stop() {
197197
if b.HookCtrl != nil {
198-
b.HookCtrl.StopMonitors()
198+
b.HookCtrl.DisableKubernetesBindings()
199199
}
200200
}

0 commit comments

Comments
 (0)