Skip to content

Commit

Permalink
add moduel kube config update
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <[email protected]>
  • Loading branch information
miklezzzz committed Dec 20, 2023
1 parent f2d803a commit ed207d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
18 changes: 18 additions & 0 deletions pkg/kube_config_manager/kube_config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ func (kcm *KubeConfigManager) KubeConfigEventCh() chan config.KubeConfigEvent {
return kcm.configEventCh
}

// UpdateModuleConfig updates a single module config
func (kcm *KubeConfigManager) UpdateModuleConfig(moduleName string) error {
newConfig, err := kcm.backend.LoadConfig(kcm.ctx)
if err != nil {
return err
}

if moduleConfig, found := newConfig.Modules[moduleName]; found {
if kcm.knownChecksums != nil {
kcm.knownChecksums.Set(moduleName, moduleConfig.Checksum)
}

kcm.currentConfig.Modules[moduleName] = moduleConfig
}

return nil
}

// loadConfig gets config from ConfigMap before starting informer.
// Set checksums for global section and modules.
func (kcm *KubeConfigManager) loadConfig() error {
Expand Down
28 changes: 25 additions & 3 deletions pkg/module_manager/module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type DirectoryConfig struct {
type KubeConfigManager interface {
SaveConfigValues(key string, values utils.Values) error
IsModuleEnabled(moduleName string) bool
UpdateModuleConfig(moduleName string) error
SafeReadConfig(handler func(config *config.KubeConfig))
}

// ModuleManagerDependencies pass dependencies for ModuleManager
Expand Down Expand Up @@ -1114,10 +1116,22 @@ func (mm *ModuleManager) PushDeleteModule(moduleName string) {
}

// PushRerunModule push moduleRun task for a module into the main queue
func (mm *ModuleManager) PushRerunModule(moduleName string) {
func (mm *ModuleManager) PushRerunModule(moduleName string) error {
err := mm.dependencies.KubeConfigManager.UpdateModuleConfig(moduleName)
if err != nil {
return fmt.Errorf("couldn't update module %s kube config: %w", moduleName, err)
}

mm.dependencies.KubeConfigManager.SafeReadConfig(func(config *config.KubeConfig) {
_, err = mm.HandleNewKubeConfig(config)
})
if err != nil {
return fmt.Errorf("couldn't reload kube config: %s", err)
}

// check if there is already moduleRun task in the main queue for the module
if queueHasPendingModuleRunTaskWithStartup(mm.dependencies.TaskQueues.GetMain(), moduleName) {
return
return nil
}

newTask := sh_task.NewTask(task.ModuleRun).
Expand All @@ -1130,6 +1144,8 @@ func (mm *ModuleManager) PushRerunModule(moduleName string) {
newTask.SetProp("triggered-by", "ModuleManager")

mm.dependencies.TaskQueues.GetMain().AddLast(newTask.WithQueuedAt(time.Now()))

return nil
}

// AreModulesInited returns true if moduleset has already been initialized
Expand Down Expand Up @@ -1190,7 +1206,10 @@ func (mm *ModuleManager) ReregisterModule(moduleName, modulePath string) error {
mm.AddEnabledModuleName(mod.GetName())
mm.AddEnabledModuleByConfigName(mod.GetName())
// enqueue module startup sequence if it is enabled
mm.PushRerunModule(mod.GetName())
err := mm.PushRerunModule(mod.GetName())
if err != nil {
return err
}
mm.SendModuleEvent(events.ModuleEvent{
ModuleName: mod.GetName(),
EventType: events.ModuleEnabled,
Expand Down Expand Up @@ -1227,6 +1246,9 @@ func (mm *ModuleManager) ReregisterModule(moduleName, modulePath string) error {
if isEnabled {
// enqueue module startup sequence if it is enabled
mm.PushRerunModule(mod.GetName())
if err != nil {
return err
}
} else {
mm.DeleteEnabledModuleName(mod.GetName())
// enqueue module delete sequence if it is disabled
Expand Down

0 comments on commit ed207d9

Please sign in to comment.