Skip to content

Commit

Permalink
bu
Browse files Browse the repository at this point in the history
  • Loading branch information
yalosev committed Nov 10, 2023
1 parent 8b817bf commit 9d0f9ee
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/module_manager/models/modules/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ func (bm *BasicModule) valuesForEnabledScript(precedingEnabledModules []string)

// TODO: ??????
res = mergeLayers(
utils.Values{},
res,
utils.Values{
"global": map[string]interface{}{
Expand Down Expand Up @@ -716,7 +717,7 @@ func (bm *BasicModule) executeHook(h *hooks.ModuleHook, bindingType sh_op_types.
if valuesPatchResult.ValuesChanged {
logEntry.Debugf("Module hook '%s': validate module values before update", h.GetName())
// Validate schema for updated module values
validationErr := bm.valuesStorage.PreCommitValues(bm.Name, valuesPatchResult.Values)
validationErr := bm.valuesStorage.PreCommitValues(valuesPatchResult.Values)
if validationErr != nil {
return multierror.Append(
fmt.Errorf("cannot apply values patch for module values"),
Expand Down
4 changes: 3 additions & 1 deletion pkg/module_manager/models/modules/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ func (gm *GlobalModule) executeHook(h *hooks.GlobalHook, bindingType sh_op_types
// and no patches for 'global' section — valuesPatchResult will be nil in this case.
if valuesPatchResult != nil && valuesPatchResult.ValuesChanged {
logEntry.Debugf("Global hook '%s': validate global values before update", h.GetName())
validationErr := gm.valuesStorage.PreCommitValues(gm.GetName(), valuesPatchResult.Values)
validationErr := gm.valuesStorage.PreCommitValues(valuesPatchResult.Values)
if validationErr != nil {
fmt.Println("BEFORE VALUES", gm.GetValues())
fmt.Println("PATCH", valuesPatchResult.Values)
return fmt.Errorf("cannot apply values patch for global values: %w", validationErr)
}

Expand Down
25 changes: 20 additions & 5 deletions pkg/module_manager/models/modules/values_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type ValuesStorage struct {
dirtyConfigValues utils.Values
dirtyMergedConfigValues utils.Values

// TODO: acutally, we don't need the validator with all Schemas here,
// we can put a single openapi schema for the specified module
validator validator
moduleName string
}
Expand Down Expand Up @@ -97,6 +99,7 @@ func (vs *ValuesStorage) openapiDefaultsTransformer(schemaType validation.Schema

func (vs *ValuesStorage) calculateResultValues() error {
merged := mergeLayers(
utils.Values{},
// Init static values (from modules/values.yaml and modules/XXX/values.yaml)
vs.staticConfigValues,

Expand Down Expand Up @@ -138,6 +141,7 @@ func (vs *ValuesStorage) PreCommitConfigValues(configV utils.Values) error {
fmt.Println("STATIC ", vs.staticConfigValues)
fmt.Println("NEW ", configV)
merged := mergeLayers(
utils.Values{},
// Init static values
vs.staticConfigValues,

Expand All @@ -151,6 +155,8 @@ func (vs *ValuesStorage) PreCommitConfigValues(configV utils.Values) error {
vs.dirtyMergedConfigValues = merged
vs.dirtyConfigValues = configV

fmt.Println("AFTER", vs.staticConfigValues)

return vs.validateConfigValues(merged)
}

Expand Down Expand Up @@ -188,14 +194,23 @@ func (vs *ValuesStorage) dirtyConfigValuesHasDiff() bool {
return false
}

func (vs *ValuesStorage) PreCommitValues(moduleName string, v utils.Values) error {
fmt.Println("SET NEW VALUES", moduleName, v)
func (vs *ValuesStorage) PreCommitValues(v utils.Values) error {
if vs.mergedConfigValues == nil {
vs.mergedConfigValues = mergeLayers(
utils.Values{},
// Init static values
vs.staticConfigValues,

fmt.Println("STATIC ", vs.staticConfigValues)
fmt.Println("CONFIG ", vs.configValues)
fmt.Println("NEW ", v)
// defaults from openapi
vs.openapiDefaultsTransformer(validation.ConfigValuesSchema),

// User configured values (ConfigValues)
vs.configValues,
)
}

merged := mergeLayers(
utils.Values{},
// Init static values (from modules/values.yaml)
vs.mergedConfigValues,

Expand Down
20 changes: 18 additions & 2 deletions pkg/module_manager/models/modules/values_storage_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package modules

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,6 +17,8 @@ type: object
default: {}
additionalProperties: false
properties:
xxx:
type: string
highAvailability:
type: boolean
modules:
Expand All @@ -33,9 +36,19 @@ x-extend:
schema: config-values.yaml
type: object
default: {}
properties:
internal:
type: object
default: {}
properties:
fooBar:
type: string
default: baz
`

initial := utils.Values{}
initial := utils.Values{
"xxx": "yyy",
}
vv := validation.NewValuesValidator()
err := vv.SchemaStorage.AddGlobalValuesSchemas([]byte(cfg), []byte(vcfg))
require.NoError(t, err)
Expand All @@ -48,6 +61,9 @@ default: {}
},
}

err = st.PreCommitConfigValues("global", configV)
err = st.PreCommitConfigValues(configV)
assert.NoError(t, err)
st.CommitConfigValues()
st.calculateResultValues()
fmt.Println(st.GetValues())
}

0 comments on commit 9d0f9ee

Please sign in to comment.