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
8282func (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
185198func (c * kubernetesBindingsController ) CanHandleEvent (kubeEvent kemtypes.KubeEvent ) bool {
0 commit comments