Skip to content

Commit

Permalink
yay
Browse files Browse the repository at this point in the history
  • Loading branch information
yalosev committed Nov 10, 2023
1 parent 6715e5b commit 94d9bd5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 49 deletions.
22 changes: 11 additions & 11 deletions pkg/module_manager/models/modules/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
"time"

hooks2 "github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"

sh_app "github.com/flant/shell-operator/pkg/app"
Expand Down Expand Up @@ -54,7 +54,7 @@ type BasicModule struct {
hooks *HooksStorage

// dependency
dc *hooks2.HookExecutionDependencyContainer
dc *hooks.HookExecutionDependencyContainer
}

func NewBasicModule(name, path string, order uint32, enabled bool, initialValues utils.Values, validator validator) *BasicModule {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (bm *BasicModule) GetName() string {
return bm.Name
}

func (bm *BasicModule) GetHooks(bt ...sh_op_types.BindingType) []*hooks2.ModuleHook {
func (bm *BasicModule) GetHooks(bt ...sh_op_types.BindingType) []*hooks.ModuleHook {
return bm.hooks.getHooks(bt...)
}

Expand All @@ -111,7 +111,7 @@ func (bm *BasicModule) ResetState() {
// bm.configValues = newValues
//}

func (bm *BasicModule) RegisterHooks(logger *log.Entry) ([]*hooks2.ModuleHook, error) {
func (bm *BasicModule) RegisterHooks(logger *log.Entry) ([]*hooks.ModuleHook, error) {
if bm.hooks.registered {
logger.Debugf("Module hooks already registered")
return nil, nil
Expand Down Expand Up @@ -192,23 +192,23 @@ func (bm *BasicModule) RegisterHooks(logger *log.Entry) ([]*hooks2.ModuleHook, e
// },
// )

func (bm *BasicModule) searchModuleHooks() ([]*hooks2.ModuleHook, error) {
func (bm *BasicModule) searchModuleHooks() ([]*hooks.ModuleHook, error) {
shellHooks, err := bm.searchModuleShellHooks()
if err != nil {
return nil, err
}

goHooks := bm.searchModuleGoHooks()

mHooks := make([]*hooks2.ModuleHook, 0, len(shellHooks)+len(goHooks))
mHooks := make([]*hooks.ModuleHook, 0, len(shellHooks)+len(goHooks))

for _, sh := range shellHooks {
mh := hooks2.NewModuleHook(sh)
mh := hooks.NewModuleHook(sh)
mHooks = append(mHooks, mh)
}

for _, gh := range goHooks {
mh := hooks2.NewModuleHook(gh)
mh := hooks.NewModuleHook(gh)
mHooks = append(mHooks, mh)
}

Expand Down Expand Up @@ -262,7 +262,7 @@ func (bm *BasicModule) searchModuleGoHooks() (hks []*kind.GoHook) {
return hks
}

func (bm *BasicModule) searchAndRegisterHooks(logger *log.Entry) ([]*hooks2.ModuleHook, error) {
func (bm *BasicModule) searchAndRegisterHooks(logger *log.Entry) ([]*hooks.ModuleHook, error) {
hks, err := bm.searchModuleHooks()
if err != nil {
return nil, fmt.Errorf("search module hooks failed: %w", err)
Expand Down Expand Up @@ -619,7 +619,7 @@ func (bm *BasicModule) prepareConfigValuesJsonFile(tmpDir string) (string, error
}

// instead on ModuleHook.Run
func (bm *BasicModule) executeHook(h *hooks2.ModuleHook, bindingType sh_op_types.BindingType, context []binding_context.BindingContext, logLabels map[string]string, metricLabels map[string]string) error {
func (bm *BasicModule) executeHook(h *hooks.ModuleHook, bindingType sh_op_types.BindingType, context []binding_context.BindingContext, logLabels map[string]string, metricLabels map[string]string) error {
logLabels = utils.MergeLabels(logLabels, map[string]string{
"hook": h.GetName(),
"hook.type": "module",
Expand Down Expand Up @@ -839,7 +839,7 @@ func (bm *BasicModule) HasKubernetesHooks() bool {
return len(hks) > 0
}

func (bm *BasicModule) GetHookByName(name string) *hooks2.ModuleHook {
func (bm *BasicModule) GetHookByName(name string) *hooks.ModuleHook {
return bm.hooks.getHookByName(name)
}

Expand Down
37 changes: 19 additions & 18 deletions pkg/module_manager/models/modules/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/shell-operator/pkg/hook/binding_context"

hooks2 "github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
"github.com/flant/addon-operator/sdk"
sh_op_types "github.com/flant/shell-operator/pkg/hook/types"
Expand All @@ -24,28 +24,28 @@ type GlobalModule struct {
hooksDir string

// probably we can use HookStorage here, but we have to add generics then
byBinding map[sh_op_types.BindingType][]*hooks2.GlobalHook
byName map[string]*hooks2.GlobalHook
byBinding map[sh_op_types.BindingType][]*hooks.GlobalHook
byName map[string]*hooks.GlobalHook

valuesStorage *ValuesStorage

// dependency
// DEPRECATED: move to values storage
valuesValidator validator
dc *hooks2.HookExecutionDependencyContainer
dc *hooks.HookExecutionDependencyContainer
}

func NewGlobalModule(hooksDir string, validator validator, dc *hooks2.HookExecutionDependencyContainer) *GlobalModule {
func NewGlobalModule(hooksDir string, validator validator, dc *hooks.HookExecutionDependencyContainer) *GlobalModule {
return &GlobalModule{
hooksDir: hooksDir,
byBinding: make(map[sh_op_types.BindingType][]*hooks2.GlobalHook),
byName: make(map[string]*hooks2.GlobalHook),
byBinding: make(map[sh_op_types.BindingType][]*hooks.GlobalHook),
byName: make(map[string]*hooks.GlobalHook),
valuesStorage: NewValuesStorage(nil, validator), // TODO(yalosev): initial
dc: dc,
}
}

func (gm *GlobalModule) RegisterHooks() ([]*hooks2.GlobalHook, error) {
func (gm *GlobalModule) RegisterHooks() ([]*hooks.GlobalHook, error) {
log.Debugf("Search and register global hooks")

hks, err := gm.searchAndRegisterHooks()
Expand All @@ -56,16 +56,16 @@ func (gm *GlobalModule) RegisterHooks() ([]*hooks2.GlobalHook, error) {
return hks, nil
}

func (gm *GlobalModule) GetHookByName(name string) *hooks2.GlobalHook {
func (gm *GlobalModule) GetHookByName(name string) *hooks.GlobalHook {
return gm.byName[name]
}

func (gm *GlobalModule) GetHooks(bt ...sh_op_types.BindingType) []*hooks2.GlobalHook {
func (gm *GlobalModule) GetHooks(bt ...sh_op_types.BindingType) []*hooks.GlobalHook {
if len(bt) > 0 {
t := bt[0]
res, ok := gm.byBinding[t]
if !ok {
return []*hooks2.GlobalHook{}
return []*hooks.GlobalHook{}
}
sort.Slice(res, func(i, j int) bool {
return res[i].Order(t) < res[j].Order(t)
Expand All @@ -75,7 +75,7 @@ func (gm *GlobalModule) GetHooks(bt ...sh_op_types.BindingType) []*hooks2.Global
}

// return all hooks
res := make([]*hooks2.GlobalHook, 0, len(gm.byName))
res := make([]*hooks.GlobalHook, 0, len(gm.byName))
for _, h := range gm.byName {
res = append(res, h)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (gm *GlobalModule) GetName() string {
return utils.GlobalValuesKey
}

func (gm *GlobalModule) executeHook(h *hooks2.GlobalHook, bindingType sh_op_types.BindingType, bc []binding_context.BindingContext, logLabels map[string]string) error {
func (gm *GlobalModule) executeHook(h *hooks.GlobalHook, bindingType sh_op_types.BindingType, bc []binding_context.BindingContext, logLabels map[string]string) error {
// Convert bindingContext for version
// versionedContextList := ConvertBindingContextList(h.Config.Version, bindingContext)
logEntry := log.WithFields(utils.LabelsToLogFields(logLabels))
Expand Down Expand Up @@ -254,6 +254,7 @@ func (gm *GlobalModule) executeHook(h *hooks2.GlobalHook, bindingType sh_op_type
return nil
}

// TODO(yalosev): change name, because we don't save values here, just store them as dirty
func (gm *GlobalModule) ValidateAndSaveConfigValues(v utils.Values) error {
return gm.valuesStorage.SetNewConfigValues(gm.GetName(), v)
}
Expand Down Expand Up @@ -320,7 +321,7 @@ func (gm *GlobalModule) handlePatch(currentValues utils.Values, valuesPatch util
return result, nil
}

func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks2.GlobalHook, error) {
func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks.GlobalHook, error) {
hks, err := gm.searchGlobalHooks()
if err != nil {
return nil, fmt.Errorf("search module hooks failed: %w", err)
Expand Down Expand Up @@ -369,7 +370,7 @@ func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks2.GlobalHook, error) {
}

// searchGlobalHooks recursively find all executables in hooksDir. Absent hooksDir is not an error.
func (gm *GlobalModule) searchGlobalHooks() (hks []*hooks2.GlobalHook, err error) {
func (gm *GlobalModule) searchGlobalHooks() (hks []*hooks.GlobalHook, err error) {
if gm.hooksDir == "" {
log.Warnf("Global hooks directory path is empty! No global hooks to load.")
return nil, nil
Expand All @@ -385,15 +386,15 @@ func (gm *GlobalModule) searchGlobalHooks() (hks []*hooks2.GlobalHook, err error
return nil, err
}

hks = make([]*hooks2.GlobalHook, 0, len(shellHooks)+len(goHooks))
hks = make([]*hooks.GlobalHook, 0, len(shellHooks)+len(goHooks))

for _, sh := range shellHooks {
gh := hooks2.NewGlobalHook(sh, gm.dc)
gh := hooks.NewGlobalHook(sh, gm.dc)
hks = append(hks, gh)
}

for _, gh := range goHooks {
glh := hooks2.NewGlobalHook(gh, gm.dc)
glh := hooks.NewGlobalHook(gh, gm.dc)
hks = append(hks, glh)
}

Expand Down
29 changes: 13 additions & 16 deletions pkg/module_manager/models/modules/values_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ func (vs *ValuesStorage) SetNewConfigValues(moduleName string, configV utils.Val
configV,
)

var err error
if moduleName == utils.GlobalValuesKey {
err = vs.validator.ValidateGlobalConfigValues(merged)
} else {
err = vs.validator.ValidateModuleConfigValues(moduleName, merged)
}

vs.dirtyConfigValues = merged

return err
return vs.validateValues(moduleName, merged)

//return modules.mergeLayers(
// // Init global section.
Expand All @@ -82,6 +75,17 @@ func (vs *ValuesStorage) SetNewConfigValues(moduleName string, configV utils.Val
//)
}

func (vs *ValuesStorage) validateValues(moduleName string, values utils.Values) error {
valuesModuleName := utils.ModuleNameToValuesKey(moduleName)
validatableValues := utils.Values{valuesModuleName: values}

if moduleName == utils.GlobalValuesKey {
return vs.validator.ValidateGlobalConfigValues(validatableValues)
}

return vs.validator.ValidateModuleConfigValues(valuesModuleName, validatableValues)
}

func (vs *ValuesStorage) dirtyConfigValuesHasDiff() bool {
if vs.dirtyConfigValues == nil {
return false
Expand Down Expand Up @@ -115,16 +119,9 @@ func (vs *ValuesStorage) SetNewValues(moduleName string, v utils.Values) error {

fmt.Println("MERGED", merged)

var err error
if moduleName == utils.GlobalValuesKey {
err = vs.validator.ValidateGlobalValues(merged)
} else {
err = vs.validator.ValidateModuleValues(moduleName, merged)
}

vs.dirtyValues = v

return err
return vs.validateValues(moduleName, merged)
}

func (vs *ValuesStorage) ApplyDirtyConfigValues() {
Expand Down
35 changes: 32 additions & 3 deletions pkg/module_manager/models/modules/values_storage_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
package modules

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"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) {
cfg := `
type: object
default: {}
additionalProperties: false
properties:
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 := `
x-extend:
schema: config-values.yaml
type: object
default: {}
`

initial := utils.Values{}
vv := validation.NewValuesValidator()
err := vv.SchemaStorage.AddGlobalValuesSchemas([]byte(cfg), []byte(vcfg))
require.NoError(t, err)
st := NewValuesStorage(initial, vv)

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

fmt.Println(st.SetNewConfigValues("global", configV))
err = st.SetNewConfigValues("global", configV)
assert.NoError(t, err)
}
13 changes: 12 additions & 1 deletion pkg/values/validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (v *ValuesValidator) GetSchema(schemaType SchemaType, valuesType SchemaType
func (v *ValuesValidator) ValidateValues(schemaType SchemaType, valuesType SchemaType, moduleName string, values utils.Values) error {
s := v.GetSchema(schemaType, valuesType, moduleName)
if s == nil {
log.Debugf("%s schema (%s) for '%s' values is not found", schemaType, moduleName, valuesType)
log.Warnf("%s schema (%s) for '%s' values is not found", schemaType, moduleName, valuesType)
return nil
}

Expand Down Expand Up @@ -93,6 +93,17 @@ func validateObject(dataObj interface{}, s *spec.Schema, rootName string) (multi

validator := validate.NewSchemaValidator(s, nil, rootName, strfmt.Default) // , validate.DisableObjectArrayTypeCheck(true)

switch v := dataObj.(type) {
case utils.Values:
dataObj = map[string]interface{}(v)

case map[string]interface{}:
// pass

default:
return fmt.Errorf("validated data object have to be utils.Values or map[string]interface{}, got %v instead", reflect.TypeOf(v))
}

result := validator.Validate(dataObj)
if result.IsValid() {
return nil
Expand Down

0 comments on commit 94d9bd5

Please sign in to comment.