Skip to content

Commit

Permalink
imorove the way extenders are applied
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>
  • Loading branch information
miklezzzz committed Jul 2, 2024
1 parent c60c38a commit 5cc487c
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 54 deletions.
4 changes: 2 additions & 2 deletions pkg/addon-operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
"github.com/flant/addon-operator/pkg/module_manager/models/modules"
"github.com/flant/addon-operator/pkg/module_manager/models/modules/events"
"github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders"
kube_config_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/kube_config"
"github.com/flant/addon-operator/pkg/task"
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/kube-client/client"
Expand Down Expand Up @@ -964,7 +964,7 @@ func (op *AddonOperator) StartModuleManagerEventHandler() {
eventLogEntry.Debugf("ModuleManagerEventHandler-KubeConfigChanged: GlobalSectionChanged %v, ModuleValuesChanged %s, ModuleEnabledStateChanged %s", event.GlobalSectionChanged, event.ModuleValuesChanged, event.ModuleEnabledStateChanged)
newModulesStateChanged := []string{}
for _, module := range event.ModuleEnabledStateChanged {
stateChanged, err := op.ModuleManager.StateChangedByExtender(extenders.KubeConfigExtender, module)
stateChanged, err := op.ModuleManager.StateChangedByExtender(kube_config_extender.Name, module)
if err != nil {
eventLogEntry.Errorf("Couldn't determine the %s module's new state via KubeConfig extender: %s", module, err)
continue
Expand Down
12 changes: 10 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var (

// StrictModeEnabled fail with error if MODULES_DIR/values.yaml does not exist
StrictModeEnabled = false

// AppliedExtenders defines the list and the order of applied module extenders
AppliedExtenders = ""
)

const (
Expand Down Expand Up @@ -128,16 +131,21 @@ func DefineStartCommandFlags(kpApp *kingpin.Application, cmd *kingpin.CmdClause)
Default("").
StringVar(&AdmissionServerCertsDir)

cmd.Flag("admission-server-enabled", "Flat to enable admission http server.").
cmd.Flag("admission-server-enabled", "Flag to enable admission http server.").
Envar("ADDON_OPERATOR_ADMISSION_SERVER_ENABLED").
Default("false").
BoolVar(&AdmissionServerEnabled)

cmd.Flag("strict-check-values-mode-enabled", "Flat to enable admission http server.").
cmd.Flag("strict-check-values-mode-enabled", "Flag to enable strict-check-values mode.").
Envar("STRICT_CHECK_VALUES_MODE_ENABLED").
Default("false").
BoolVar(&StrictModeEnabled)

cmd.Flag("applied-module-extenders", "Flag to define which module extenders to apply").
Envar("ADDON_OPERATOR_APPLIED_MODULE_EXTENDERS").
Default(AppliedExtenders).
StringVar(&AppliedExtenders)

sh_app.DefineKubeClientFlags(cmd)
sh_app.DefineJqFlags(cmd)
sh_app.DefineLoggingFlags(cmd)
Expand Down
24 changes: 15 additions & 9 deletions pkg/module_manager/module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/flant/addon-operator/pkg/addon-operator/converge"
"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/helm"
"github.com/flant/addon-operator/pkg/helm_resources_manager"
. "github.com/flant/addon-operator/pkg/hook/types"
Expand All @@ -27,10 +28,10 @@ import (
"github.com/flant/addon-operator/pkg/module_manager/models/moduleset"
"github.com/flant/addon-operator/pkg/module_manager/scheduler"
"github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders"
"github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled"
dynamic_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled"
kube_config_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/kube_config"
"github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/script_enabled"
"github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/static"
script_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/script_enabled"
static_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/static"
"github.com/flant/addon-operator/pkg/task"
"github.com/flant/addon-operator/pkg/utils"
. "github.com/flant/shell-operator/pkg/hook/binding_context"
Expand Down Expand Up @@ -282,7 +283,7 @@ func (mm *ModuleManager) Init() error {
return err
}

staticExtender, err := static.NewExtender(mm.ModulesDir)
staticExtender, err := static_extender.NewExtender(mm.ModulesDir)
if err != nil {
return err
}
Expand All @@ -300,7 +301,7 @@ func (mm *ModuleManager) Init() error {
return err
}

scriptEnabledExtender, err := script_enabled.NewExtender(mm.TempDir)
scriptEnabledExtender, err := script_extender.NewExtender(mm.TempDir)
if err != nil {
return err
}
Expand All @@ -309,6 +310,11 @@ func (mm *ModuleManager) Init() error {
return err
}

// by this point we must have all required scheduler extenders attached
if err := mm.moduleScheduler.ApplyExtenders(app.AppliedExtenders); err != nil {
return err
}

return mm.registerModules()
}

Expand Down Expand Up @@ -789,7 +795,7 @@ func (mm *ModuleManager) LoopByBinding(binding BindingType, fn func(gh *hooks.Gl
return nil
}

func (mm *ModuleManager) runDynamicEnabledLoop(extender *dynamically_enabled.Extender) {
func (mm *ModuleManager) runDynamicEnabledLoop(extender *dynamic_extender.Extender) {
for report := range mm.global.EnabledReportChannel() {
err := mm.applyEnabledPatch(report.Patch, extender)
report.Done <- err
Expand All @@ -798,7 +804,7 @@ func (mm *ModuleManager) runDynamicEnabledLoop(extender *dynamically_enabled.Ext

// applyEnabledPatch changes "dynamicEnabled" map with patches.
// TODO: can add some optimization here
func (mm *ModuleManager) applyEnabledPatch(enabledPatch utils.ValuesPatch, extender *dynamically_enabled.Extender) error {
func (mm *ModuleManager) applyEnabledPatch(enabledPatch utils.ValuesPatch, extender *dynamic_extender.Extender) error {
for _, op := range enabledPatch.Operations {
// Extract module name from json patch: '"path": "/moduleNameEnabled"'
modName := strings.TrimSuffix(op.Path, "Enabled")
Expand Down Expand Up @@ -830,13 +836,13 @@ func (mm *ModuleManager) applyEnabledPatch(enabledPatch utils.ValuesPatch, exten

// DynamicEnabledChecksum returns checksum for dynamicEnabled map
func (mm *ModuleManager) DynamicEnabledChecksum() string {
jsonBytes, _ := json.Marshal(mm.moduleScheduler.DumpExtender(extenders.DynamicallyEnabledExtender))
jsonBytes, _ := json.Marshal(mm.moduleScheduler.DumpExtender(dynamic_extender.Name))
return utils_checksum.CalculateChecksum(string(jsonBytes))
}

func (mm *ModuleManager) DumpDynamicEnabled() string {
dump := "["
for k, v := range mm.moduleScheduler.DumpExtender(extenders.DynamicallyEnabledExtender) {
for k, v := range mm.moduleScheduler.DumpExtender(dynamic_extender.Name) {
dump += fmt.Sprintf("%s(%t), ", k, v)
}
return dump + "]"
Expand Down
4 changes: 2 additions & 2 deletions pkg/module_manager/module_manager_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/modules"
dynamically_enabled_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled"
dynamic_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled"
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/shell-operator/pkg/hook/controller"
sh_op_types "github.com/flant/shell-operator/pkg/hook/types"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (mm *ModuleManager) registerGlobalModule(globalValues utils.Values, configB
log.Infof(gm.GetSchemaStorage().GlobalSchemasDescription())

// applies a scheduler extender to follow which modules get enabled/disabled by dynamic patches
dynamicExtender := dynamically_enabled_extender.NewExtender()
dynamicExtender := dynamic_extender.NewExtender()
if err := mm.moduleScheduler.AddExtender(dynamicExtender); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/flant/addon-operator/pkg/module_manager/scheduler/node"
)

const (
Name extenders.ExtenderName = "DynamicallyEnabled"
)

type Extender struct {
l sync.RWMutex
modulesStatus map[string]bool
Expand Down Expand Up @@ -41,7 +45,7 @@ func (e *Extender) UpdateStatus(moduleName, operation string, value bool) {
}

func (e *Extender) Name() extenders.ExtenderName {
return extenders.DynamicallyEnabledExtender
return Name
}

func (e *Extender) Filter(module node.ModuleInterface) (*bool, error) {
Expand All @@ -59,8 +63,5 @@ func (e *Extender) IsNotifier() bool {
return false
}

//func (e *Extender) SetNotifyChannel(_ context.Context, _ chan extenders.ExtenderEvent) {
//}

func (e *Extender) Order() {
}
7 changes: 0 additions & 7 deletions pkg/module_manager/scheduler/extenders/extenders.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,3 @@ type ResettableExtender interface {
// Reset resets the extender's cache
Reset()
}

const (
StaticExtender ExtenderName = "Static"
DynamicallyEnabledExtender ExtenderName = "DynamicallyEnabled"
KubeConfigExtender ExtenderName = "KubeConfig"
ScriptEnabledExtender ExtenderName = "ScriptEnabled"
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/flant/addon-operator/pkg/module_manager/scheduler/node"
)

const (
Name extenders.ExtenderName = "KubeConfig"
)

type kubeConfigManager interface {
IsModuleEnabled(moduleName string) *bool
KubeConfigEventCh() chan config.KubeConfigEvent
Expand All @@ -31,7 +35,7 @@ func (e Extender) Dump() map[string]bool {
}

func (e Extender) Name() extenders.ExtenderName {
return extenders.KubeConfigExtender
return Name
}

func (e Extender) Filter(module node.ModuleInterface) (*bool, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/flant/addon-operator/pkg/module_manager/scheduler/node"
)

const (
Name extenders.ExtenderName = "ScriptEnabled"
)

type Extender struct {
tmpDir string

Expand Down Expand Up @@ -45,7 +49,7 @@ func (e *Extender) Dump() map[string]bool {
}

func (e *Extender) Name() extenders.ExtenderName {
return extenders.ScriptEnabledExtender
return Name
}

func (e *Extender) IsShutter() bool {
Expand Down
9 changes: 5 additions & 4 deletions pkg/module_manager/scheduler/extenders/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/flant/addon-operator/pkg/utils"
)

const (
Name extenders.ExtenderName = "Static"
)

type Extender struct {
modulesStatus map[string]bool
}
Expand Down Expand Up @@ -72,7 +76,7 @@ func (e Extender) Dump() map[string]bool {
}

func (e Extender) Name() extenders.ExtenderName {
return extenders.StaticExtender
return Name
}

func (e Extender) Filter(module node.ModuleInterface) (*bool, error) {
Expand All @@ -87,8 +91,5 @@ func (e *Extender) IsNotifier() bool {
return false
}

//func (e *Extender) SetNotifyChannel(_ context.Context, _ chan extenders.ExtenderEvent) {
//}

func (e Extender) Order() {
}
Loading

0 comments on commit 5cc487c

Please sign in to comment.