Skip to content

Commit d01a3fa

Browse files
committed
[feat] idempotent kubernetes hooks enabling
Signed-off-by: Stepan Paksashvili <stepan.paksashvili@flant.com>
1 parent 4f12b98 commit d01a3fa

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

pkg/hook/controller/hook_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ func (hc *HookController) UpdateMonitor(monitorId string, kind, apiVersion strin
265265
return nil
266266
}
267267

268+
func (hc *HookController) EnableKubernetesBindings() ([]BindingExecutionInfo, error) {
269+
if hc.KubernetesController != nil {
270+
return hc.KubernetesController.EnableKubernetesBindings()
271+
}
272+
273+
return nil, nil
274+
}
275+
276+
func (hc *HookController) DisableKubernetesBindings() {
277+
if hc.KubernetesController != nil {
278+
hc.KubernetesController.DisableKubernetesBindings()
279+
}
280+
}
281+
268282
func (hc *HookController) EnableScheduleBindings() {
269283
if hc.ScheduleController != nil {
270284
hc.ScheduleController.EnableScheduleBindings()

pkg/hook/controller/kubernetes_bindings_controller.go

Lines changed: 18 additions & 1 deletion
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,6 +27,7 @@ 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)
@@ -82,6 +83,12 @@ func (c *kubernetesBindingsController) WithKubeEventsManager(kubeEventsManager k
8283
func (c *kubernetesBindingsController) EnableKubernetesBindings() ([]BindingExecutionInfo, error) {
8384
res := make([]BindingExecutionInfo, 0)
8485

86+
c.l.Lock()
87+
if len(c.BindingMonitorLinks) > 0 {
88+
return res, nil
89+
}
90+
c.l.Unlock()
91+
8592
for _, config := range c.KubernetesBindings {
8693
err := c.kubeEventsManager.AddMonitor(config.Monitor)
8794
if err != nil {
@@ -182,6 +189,16 @@ func (c *kubernetesBindingsController) StopMonitors() {
182189
})
183190
}
184191

192+
func (c *kubernetesBindingsController) DisableKubernetesBindings() {
193+
c.l.Lock()
194+
defer c.l.Unlock()
195+
196+
for _, binding := range c.BindingMonitorLinks {
197+
_ = c.kubeEventsManager.StopMonitor(binding.MonitorId)
198+
delete(c.BindingMonitorLinks, binding.MonitorId)
199+
}
200+
}
201+
185202
func (c *kubernetesBindingsController) CanHandleEvent(kubeEvent kemtypes.KubeEvent) bool {
186203
var canHandleEvent bool
187204

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

0 commit comments

Comments
 (0)