Skip to content

Commit afaa95b

Browse files
ipaqsaldmonster
andauthored
[fix] 1.15 module enabling (#711) (#712)
Signed-off-by: Stepan Paksashvili <[email protected]> Signed-off-by: Pavel Okhlopkov <[email protected]> Co-authored-by: Pavel Okhlopkov <[email protected]>
1 parent b238283 commit afaa95b

File tree

7 files changed

+110
-80
lines changed

7 files changed

+110
-80
lines changed

.github/workflows/lint.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ jobs:
1111
name: Run Go linters
1212
runs-on: ubuntu-latest
1313
steps:
14-
- name: Set up Go 1.23
14+
- name: Set up Go 1.25
1515
uses: actions/setup-go@v5
1616
with:
17-
go-version: '1.23'
17+
go-version: '1.25'
1818
id: go
1919

2020
- name: Check out addon-operator code
@@ -38,7 +38,7 @@ jobs:
3838
3939
- name: Run golangci-lint
4040
run: |
41-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b . v2.1.6
41+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b . v2.7.2
4242
./golangci-lint run
4343
4444
codespell:

pkg/kube_config_manager/backend/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type ConfigHandler interface {
1616
LoadConfig(ctx context.Context, modulesNames ...string) (*config.KubeConfig, error)
1717

1818
// SaveConfigValues saves patches for modules in backend (if supported), overriding the configuration
19-
// Deprecated: saving values in the values source is not recommended and shouldn't be used anymore
19+
// Deprecated:
20+
// saving values in the values source is not recommended and shouldn't be used anymore
2021
SaveConfigValues(ctx context.Context, key string, values utils.Values) ( /*checksum*/ string, error)
2122
}

pkg/kube_config_manager/backend/configmap/configmap.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/client-go/tools/cache"
1818

1919
"github.com/flant/addon-operator/pkg/kube_config_manager/config"
20+
kcmcontext "github.com/flant/addon-operator/pkg/kube_config_manager/context"
2021
"github.com/flant/addon-operator/pkg/utils"
2122
"github.com/flant/kube-client/client"
2223
)
@@ -93,12 +94,7 @@ func (b Backend) saveGlobalConfigValues(ctx context.Context, values utils.Values
9394
}
9495

9596
func (b Backend) isDebugEnabled(ctx context.Context) bool {
96-
debug, ok := ctx.Value("kube-config-manager-debug").(bool)
97-
if !ok {
98-
return false
99-
}
100-
101-
return debug
97+
return kcmcontext.IsKubeConfigManagerDebug(ctx)
10298
}
10399

104100
// saveModuleConfigValues updates module section in ConfigMap.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Package context provides context helpers for kube-config-manager.
2+
package context
3+
4+
import "context"
5+
6+
type contextValue string
7+
8+
const (
9+
kubeConfigManagerDebug contextValue = "kube-config-manager-debug"
10+
)
11+
12+
func WithKubeConfigManagerDebug(ctx context.Context, value bool) context.Context {
13+
return context.WithValue(ctx, kubeConfigManagerDebug, value)
14+
}
15+
16+
func IsKubeConfigManagerDebug(ctx context.Context) bool {
17+
val := ctx.Value(kubeConfigManagerDebug)
18+
if val == nil {
19+
return false
20+
}
21+
debug, ok := val.(bool)
22+
return ok && debug
23+
}

pkg/kube_config_manager/kube_config_manager.go

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/flant/addon-operator/pkg/kube_config_manager/backend"
1414
"github.com/flant/addon-operator/pkg/kube_config_manager/config"
15+
kcmcontext "github.com/flant/addon-operator/pkg/kube_config_manager/context"
1516
"github.com/flant/addon-operator/pkg/utils"
1617
runtimeConfig "github.com/flant/shell-operator/pkg/config"
1718
)
@@ -53,8 +54,9 @@ func NewKubeConfigManager(ctx context.Context, bk backend.ConfigHandler, runtime
5354
if err != nil {
5455
return err
5556
}
56-
//nolint: revive,staticcheck // basic type is enough here
57-
cctx = context.WithValue(cctx, "kube-config-manager-debug", val)
57+
58+
cctx = kcmcontext.WithKubeConfigManagerDebug(cctx, val)
59+
5860
return nil
5961
},
6062
nil,
@@ -241,84 +243,90 @@ func (kcm *KubeConfigManager) handleConfigEvent(obj config.Event) {
241243
}
242244

243245
default:
244-
// some module values
245-
modulesChanged := []string{}
246-
modulesStateChanged := []string{}
247-
moduleMaintenanceChanged := make(map[string]utils.Maintenance)
248-
249-
// module update
250246
kcm.m.Lock()
251247
defer kcm.m.Unlock()
248+
252249
moduleName := obj.Key
253250
moduleCfg := obj.Config.Modules[obj.Key]
254-
if obj.Op == config.EventDelete {
255-
kcm.logger.Info("Module section deleted", slog.String("moduleName", moduleName))
256-
modulesChanged = append(modulesChanged, moduleName)
257-
if kcm.currentConfig.Modules[moduleName].GetEnabled() != "" && kcm.currentConfig.Modules[moduleName].GetEnabled() != "n/d" {
258-
modulesStateChanged = append(modulesStateChanged, moduleName)
259-
}
260251

261-
if kcm.currentConfig.Modules[moduleName].GetMaintenanceState() == utils.NoResourceReconciliation {
262-
moduleMaintenanceChanged[moduleName] = utils.Managed
263-
}
252+
switch obj.Op {
253+
case config.EventDelete:
254+
kcm.handleDeleteEvent(moduleName, moduleCfg)
255+
default:
256+
kcm.handleUpdateEvent(moduleName, moduleCfg)
257+
}
258+
}
259+
}
264260

265-
moduleCfg.Reset()
266-
moduleCfg.Checksum = moduleCfg.ModuleConfig.Checksum()
267-
kcm.currentConfig.Modules[obj.Key] = moduleCfg
268-
kcm.configEventCh <- config.KubeConfigEvent{
269-
Type: config.KubeConfigChanged,
270-
ModuleValuesChanged: modulesChanged,
271-
ModuleEnabledStateChanged: modulesStateChanged,
272-
ModuleMaintenanceChanged: moduleMaintenanceChanged,
273-
}
274-
return
261+
func (kcm *KubeConfigManager) handleDeleteEvent(moduleName string, cfg *config.ModuleKubeConfig) {
262+
var modulesChanged []string
263+
var modulesStateChanged []string
264+
moduleMaintenanceChanged := make(map[string]utils.Maintenance)
265+
266+
kcm.logger.Info("module section deleted", slog.String("name", moduleName))
267+
modulesChanged = append(modulesChanged, moduleName)
268+
if kcm.currentConfig.Modules[moduleName].GetEnabled() != "" && kcm.currentConfig.Modules[moduleName].GetEnabled() != "n/d" {
269+
modulesStateChanged = append(modulesStateChanged, moduleName)
270+
}
271+
272+
if kcm.currentConfig.Modules[moduleName].GetMaintenanceState() == utils.NoResourceReconciliation {
273+
moduleMaintenanceChanged[moduleName] = utils.Managed
274+
}
275+
276+
cfg.Reset()
277+
cfg.Checksum = cfg.ModuleConfig.Checksum()
278+
kcm.currentConfig.Modules[moduleName] = cfg
279+
280+
kcm.configEventCh <- config.KubeConfigEvent{
281+
Type: config.KubeConfigChanged,
282+
ModuleValuesChanged: modulesChanged,
283+
ModuleEnabledStateChanged: modulesStateChanged,
284+
ModuleMaintenanceChanged: moduleMaintenanceChanged,
285+
}
286+
}
287+
288+
func (kcm *KubeConfigManager) handleUpdateEvent(moduleName string, cfg *config.ModuleKubeConfig) {
289+
var modulesChanged []string
290+
var modulesStateChanged []string
291+
moduleMaintenanceChanged := make(map[string]utils.Maintenance)
292+
293+
var changed bool
294+
295+
if currentCfg, has := kcm.currentConfig.Modules[moduleName]; has {
296+
if currentCfg.Checksum != cfg.Checksum {
297+
changed = true
298+
modulesChanged = append(modulesChanged, moduleName)
275299
}
276-
// Module section is changed if a new checksum doesn't equal to saved one and isn't in known checksums, or the module new state doesn't equal to the previous one.
277-
if kcm.knownChecksums.HasEqualChecksum(moduleName, moduleCfg.Checksum) {
278-
// Remove known checksum, do not fire event on self-update.
279-
kcm.knownChecksums.Remove(moduleName, moduleCfg.Checksum)
280-
} else {
281-
if currModuleCfg, has := kcm.currentConfig.Modules[moduleName]; has {
282-
if currModuleCfg.Checksum != moduleCfg.Checksum {
283-
modulesChanged = append(modulesChanged, moduleName)
284-
}
285300

286-
if kcm.currentConfig.Modules[moduleName].GetEnabled() != moduleCfg.GetEnabled() {
287-
modulesStateChanged = append(modulesStateChanged, moduleName)
288-
}
301+
if currentCfg.GetEnabled() != cfg.GetEnabled() {
302+
changed = true
303+
modulesStateChanged = append(modulesStateChanged, moduleName)
304+
}
289305

290-
if kcm.currentConfig.Modules[moduleName].GetMaintenanceState() != moduleCfg.GetMaintenanceState() {
291-
moduleMaintenanceChanged[moduleName] = moduleCfg.GetMaintenanceState()
292-
}
293-
kcm.logger.Info("Module section changed. Enabled flag transition.",
294-
slog.String("moduleName", moduleName),
295-
slog.String("previous", kcm.currentConfig.Modules[moduleName].GetEnabled()),
296-
slog.String("current", moduleCfg.GetEnabled()),
297-
slog.String("maintenanceFlag", moduleCfg.GetMaintenanceState().String()))
298-
} else {
299-
modulesChanged = append(modulesChanged, moduleName)
300-
if moduleCfg.GetEnabled() != "" && moduleCfg.GetEnabled() != "n/d" {
301-
modulesStateChanged = append(modulesStateChanged, moduleName)
302-
}
306+
if currentCfg.GetMaintenanceState() != cfg.GetMaintenanceState() {
307+
changed = true
308+
moduleMaintenanceChanged[moduleName] = currentCfg.GetMaintenanceState()
309+
}
310+
} else {
311+
changed = true
303312

304-
if moduleCfg.GetMaintenanceState() == utils.NoResourceReconciliation {
305-
moduleMaintenanceChanged[moduleName] = utils.NoResourceReconciliation
306-
}
307-
kcm.logger.Info("Module section added",
308-
slog.String("moduleName", moduleName),
309-
slog.String("enabledFlag", moduleCfg.GetEnabled()),
310-
slog.String("maintenanceFlag", moduleCfg.GetMaintenanceState().String()))
311-
}
313+
modulesChanged = append(modulesChanged, moduleName)
314+
if cfg.GetEnabled() != "" && cfg.GetEnabled() != "n/d" {
315+
modulesStateChanged = append(modulesStateChanged, moduleName)
312316
}
313317

314-
if len(modulesChanged)+len(modulesStateChanged)+len(moduleMaintenanceChanged) > 0 {
315-
kcm.currentConfig.Modules[obj.Key] = moduleCfg
316-
kcm.configEventCh <- config.KubeConfigEvent{
317-
Type: config.KubeConfigChanged,
318-
ModuleValuesChanged: modulesChanged,
319-
ModuleEnabledStateChanged: modulesStateChanged,
320-
ModuleMaintenanceChanged: moduleMaintenanceChanged,
321-
}
318+
if cfg.GetMaintenanceState() == utils.NoResourceReconciliation {
319+
moduleMaintenanceChanged[moduleName] = utils.NoResourceReconciliation
320+
}
321+
}
322+
323+
if changed {
324+
kcm.currentConfig.Modules[moduleName] = cfg
325+
kcm.configEventCh <- config.KubeConfigEvent{
326+
Type: config.KubeConfigChanged,
327+
ModuleValuesChanged: modulesChanged,
328+
ModuleEnabledStateChanged: modulesStateChanged,
329+
ModuleMaintenanceChanged: moduleMaintenanceChanged,
322330
}
323331
}
324332
}

pkg/module_manager/models/hooks/kind/gohook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func (h *GoHook) GetHookController() *controller.HookController {
8383
}
8484

8585
// GetBasicHook returns hook for shell-operator
86-
// Deprecated: don't use it for production purposes. You don't need such a low level for working with hooks
86+
// Deprecated:
87+
// don't use it for production purposes. You don't need such a low level for working with hooks
8788
func (h *GoHook) GetBasicHook() sh_hook.Hook {
8889
return h.basicHook
8990
}

sdk/registry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func (h *HookRegistry) GetGlobalHooks() []*kind.GoHook {
6060
}
6161

6262
// Hooks returns all (module and global) hooks
63-
// Deprecated: method exists for backward compatibility, use GetGlobalHooks or GetModuleHooks instead
63+
// Deprecated:
64+
// method exists for backward compatibility, use GetGlobalHooks or GetModuleHooks instead
6465
func (h *HookRegistry) Hooks() []*kind.GoHook {
6566
res := make([]*kind.GoHook, 0, len(h.globalHooks)+len(h.embeddedModuleHooks))
6667

0 commit comments

Comments
 (0)