Skip to content

Commit

Permalink
update RunModuleWithNewOpenAPISchema
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 87e644a commit 153e001
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 84 deletions.
1 change: 0 additions & 1 deletion pkg/addon-operator/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (op *AddonOperator) registerDefaultRoutes() {
<h1>Addon-operator</h1>
<pre>go tool pprof http://ADDON_OPERATOR_IP:%s/debug/pprof/profile</pre>
<p>
<a href="/graph">graph's pic</a>
<a href="/discovery">show all possible routes</a>
<a href="/metrics">prometheus metrics for built-in parameters</a>
<a href="/metrics/hooks">prometheus metrics for user hooks</a>
Expand Down
16 changes: 6 additions & 10 deletions pkg/module_manager/models/modules/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ func (bm *BasicModule) ResetState() {
}
}

// GetStaticValues returns the module's static values
func (bm *BasicModule) GetStaticValues() utils.Values {
return bm.valuesStorage.getStaticValues()
}

// ApplyNewStaticValues sets the module's static values and recalculate the resulting values
func (bm *BasicModule) ApplyNewStaticValues(values utils.Values) error {
return bm.valuesStorage.applyNewStaticValues(values)
}

// RegisterHooks find and registers all module hooks from a filesystem or GoHook Registry
func (bm *BasicModule) RegisterHooks(logger *log.Entry) ([]*hooks.ModuleHook, error) {
if bm.hooks.registered {
Expand Down Expand Up @@ -857,10 +847,16 @@ func (bm *BasicModule) GetValuesStorage() *ValuesStorage {
return bm.valuesStorage
}

// GetSchemaStorage returns current schema storage of the basic module
func (bm *BasicModule) GetSchemaStorage() *validation.SchemaStorage {
return bm.valuesStorage.schemaStorage
}

// ApplyNewSchemaStorage updates schema storage of the basic module
func (bm *BasicModule) ApplyNewSchemaStorage(schema *validation.SchemaStorage) error {
return bm.valuesStorage.applyNewSchemaStorage(schema)
}

func (bm *BasicModule) Validate() error {
valuesKey := utils.ModuleNameToValuesKey(bm.GetName())
restoredName := utils.ModuleNameFromValuesKey(valuesKey)
Expand Down
15 changes: 3 additions & 12 deletions pkg/module_manager/models/modules/values_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,9 @@ func (vs *ValuesStorage) validateValues(values utils.Values) error {
return vs.schemaStorage.ValidateValues(valuesModuleName, validatableValues)
}

// getStaticValues returns current static values of the module
func (vs *ValuesStorage) getStaticValues() utils.Values {
vs.lock.Lock()
defer vs.lock.Unlock()
return vs.staticValues
}

// applyNewStaticValues sets the module's static values and recalculate the resulting values
func (vs *ValuesStorage) applyNewStaticValues(values utils.Values) error {
vs.lock.Lock()
defer vs.lock.Unlock()
vs.staticValues = values
// applyNewSchemaStorage sets new schema storage
func (vs *ValuesStorage) applyNewSchemaStorage(schema *validation.SchemaStorage) error {
vs.schemaStorage = schema
return vs.calculateResultValues()
}

Expand Down
82 changes: 48 additions & 34 deletions pkg/module_manager/models/modules/values_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"github.com/stretchr/testify/require"

"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/addon-operator/pkg/values/validation"
)

func TestSetConfigValues(t *testing.T) {
func TestApplyNewSchemaStorage(t *testing.T) {
cfg := `
type: object
default: {}
Expand All @@ -20,14 +21,6 @@ properties:
type: string
highAvailability:
type: boolean
modules:
additionalProperties: false
default: {}
type: object
properties:
publicDomainTemplate:
type: string
pattern: '^(%s([-a-z0-9]*[a-z0-9])?|[a-z0-9]([-a-z0-9]*)?%s([-a-z0-9]*)?[a-z0-9]|[a-z0-9]([-a-z0-9]*)?%s)(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$'
`

vcfg := `
Expand All @@ -49,30 +42,37 @@ properties:
"xxx": "yyy",
}

st, err := NewValuesStorage("global", initial, []byte(cfg), []byte(vcfg))
valuesStorage, err := NewValuesStorage("global", initial, []byte(cfg), []byte(vcfg))
require.NoError(t, err)

configV := utils.Values{
"highAvailability": true,
"modules": map[string]interface{}{
"publicDomainTemplate": "%s.foo.bar",
},
}
newVcfg := `
x-extend:
schema: config-values.yaml
type: object
default: {}
properties:
internal:
type: object
default: {}
properties:
fooBar:
type: string
default: bar
`
schemaStorage, err := validation.NewSchemaStorage([]byte(cfg), []byte(newVcfg))
require.NoError(t, err)

err = valuesStorage.applyNewSchemaStorage(schemaStorage)
require.NoError(t, err)

newValues, err := st.GenerateNewConfigValues(configV, true)
assert.NoError(t, err)
st.SaveConfigValues(newValues)
assert.YAMLEq(t, `
highAvailability: true
internal:
fooBar: baz
modules:
publicDomainTemplate: '%s.foo.bar'
xxx: yyy
`, st.GetValues(false).AsString("yaml"))
internal:
fooBar: bar
`, valuesStorage.GetValues(false).AsString("yaml"))
}

func TestApplyNewStaticValues(t *testing.T) {
func TestSetConfigValues(t *testing.T) {
cfg := `
type: object
default: {}
Expand All @@ -82,6 +82,14 @@ properties:
type: string
highAvailability:
type: boolean
modules:
additionalProperties: false
default: {}
type: object
properties:
publicDomainTemplate:
type: string
pattern: '^(%s([-a-z0-9]*[a-z0-9])?|[a-z0-9]([-a-z0-9]*)?%s([-a-z0-9]*)?[a-z0-9]|[a-z0-9]([-a-z0-9]*)?%s)(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$'
`

vcfg := `
Expand All @@ -98,26 +106,32 @@ properties:
type: string
default: baz
`

initial := utils.Values{
"xxx": "yyy",
}

st, err := NewValuesStorage("global", initial, []byte(cfg), []byte(vcfg))
require.NoError(t, err)

newStatic := utils.Values{
"xxx": "zzz",
configV := utils.Values{
"highAvailability": true,
"modules": map[string]interface{}{
"publicDomainTemplate": "%s.foo.bar",
},
}

err = st.applyNewStaticValues(newStatic)
require.NoError(t, err)

v := st.GetValues(false)
newValues, err := st.GenerateNewConfigValues(configV, true)
assert.NoError(t, err)
st.SaveConfigValues(newValues)
assert.YAMLEq(t, `
highAvailability: true
internal:
fooBar: baz
xxx: zzz
`, v.AsString("yaml"))
modules:
publicDomainTemplate: '%s.foo.bar'
xxx: yyy
`, st.GetValues(false).AsString("yaml"))
}

func TestPatchValues(t *testing.T) {
Expand Down
30 changes: 3 additions & 27 deletions pkg/module_manager/module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,12 +926,6 @@ func (mm *ModuleManager) UpdateModuleLastErrorAndNotify(module *modules.BasicMod

// PushRunModuleTask pushes moduleRun task for a module into the main queue if there is no such a task for the module
func (mm *ModuleManager) PushRunModuleTask(moduleName string, doModuleStartup bool) error {
// update module's kube config
err := mm.UpdateModuleKubeConfig(moduleName)
if err != nil {
return err
}

// check if there is already moduleRun task in the main queue for the module
if queueHasPendingModuleRunTaskWithStartup(mm.dependencies.TaskQueues.GetMain(), moduleName) {
return nil
Expand All @@ -951,30 +945,13 @@ func (mm *ModuleManager) PushRunModuleTask(moduleName string, doModuleStartup bo
return nil
}

// UpdateModuleKubeConfig updates a module's kube config
func (mm *ModuleManager) UpdateModuleKubeConfig(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.ApplyNewKubeConfigValues(config, false)
})
if err != nil {
return fmt.Errorf("couldn't reload kube config: %s", err)
}

return nil
}

// AreModulesInited returns true if modulemanager's moduleset has already been initialized
func (mm *ModuleManager) AreModulesInited() bool {
return mm.modules.IsInited()
}

// RunModuleWithNewStaticValues updates the module's values by rebasing them from static values from modulePath directory and pushes RunModuleTask if the module is enabled
func (mm *ModuleManager) RunModuleWithNewStaticValues(moduleName, moduleSource, modulePath string) error {
// RunModuleWithNewOpenAPISchema updates the module's OpenAPI schema from modulePath directory and pushes RunModuleTask if the module is enabled
func (mm *ModuleManager) RunModuleWithNewOpenAPISchema(moduleName, moduleSource, modulePath string) error {
currentModule := mm.modules.Get(moduleName)
if currentModule == nil {
return fmt.Errorf("failed to get basic module - not found")
Expand All @@ -985,8 +962,7 @@ func (mm *ModuleManager) RunModuleWithNewStaticValues(moduleName, moduleSource,
return err
}

log.Debugf("new static values for %s module: %v", moduleName, basicModule.GetStaticValues())
err = currentModule.ApplyNewStaticValues(basicModule.GetStaticValues())
err = currentModule.ApplyNewSchemaStorage(basicModule.GetSchemaStorage())
if err != nil {
return err
}
Expand Down

0 comments on commit 153e001

Please sign in to comment.