Skip to content

Commit

Permalink
Merge pull request #187 from flant/fix-go-hooks-registration
Browse files Browse the repository at this point in the history
Fixed Go hook registration
  • Loading branch information
diafour authored Apr 5, 2021
2 parents 5a3d3d4 + 44a6223 commit 06119d4
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ func (h *GoHook) Config() *go_hook.HookConfig {
}
}

func (h *GoHook) Metadata() *go_hook.HookMetadata {
return &go_hook.HookMetadata{
Name: "go_hook.go",
Path: "001-module-go-hooks/hooks/go_hook.go",
Module: true,
ModuleName: "module-go-hooks",
}
}

func (h *GoHook) Run(input *go_hook.HookInput) error {
for _, o := range input.Snapshots["pods"] {
podSpec := o.(*podSpecFilteredObj)
Expand Down
1 change: 1 addition & 0 deletions pkg/module_manager/global_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (g *GlobalHook) WithGoConfig(config *go_hook.HookConfig) (err error) {

// Make HookController and GetConfigDescription work.
g.Hook.Config = &g.Config.HookConfig
g.Hook.RateLimiter = hook.CreateRateLimiter(g.Hook.Config)
return nil
}

Expand Down
1 change: 0 additions & 1 deletion pkg/module_manager/go_hook/go_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

type GoHook interface {
Metadata() *HookMetadata
Config() *HookConfig
Run(input *HookInput) error
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/module_manager/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ func SearchGlobalGoHooks() (hooks []*GlobalHook, err error) {
hooks = make([]*GlobalHook, 0)
goHooks := sdk.Registry().Hooks()
for _, h := range goHooks {
m := h.Metadata()
m := h.Metadata
if !m.Global {
continue
}

globalHook := NewGlobalHook(m.Name, m.Path)
globalHook.WithGoHook(h)
globalHook.WithGoHook(h.Hook)
hooks = append(hooks, globalHook)
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func SearchModuleGoHooks(module *Module) (hooks []*ModuleHook, err error) {
hooks = make([]*ModuleHook, 0)
goHooks := sdk.Registry().Hooks()
for _, h := range goHooks {
m := h.Metadata()
m := h.Metadata
if !m.Module {
continue
}
Expand All @@ -254,7 +254,7 @@ func SearchModuleGoHooks(module *Module) (hooks []*ModuleHook, err error) {

moduleHook := NewModuleHook(m.Name, m.Path)
moduleHook.WithModule(module)
moduleHook.WithGoHook(h)
moduleHook.WithGoHook(h.Hook)

hooks = append(hooks, moduleHook)
}
Expand Down
42 changes: 12 additions & 30 deletions pkg/module_manager/hook_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,31 @@ package module_manager
import (
"testing"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/metric_storage/operation"
. "github.com/onsi/gomega"

. "github.com/flant/shell-operator/pkg/hook/binding_context"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
_ "github.com/flant/addon-operator/pkg/module_manager/test/go_hooks/global-hooks"
)

type SimpleHook struct {
}

func (s *SimpleHook) Metadata() *go_hook.HookMetadata {
return &go_hook.HookMetadata{
Name: "simple",
Path: "simple",
Global: true,
}
}

func (s *SimpleHook) Config() (config *go_hook.HookConfig) {
return &go_hook.HookConfig{
OnStartup: &go_hook.OrderedConfig{Order: 10},
}
}

func (s *SimpleHook) Run(input *go_hook.HookInput) error {
*input.Metrics = append(*input.Metrics, operation.MetricOperation{})

return nil
}

func Test_Config_GoHook(t *testing.T) {
g := NewWithT(t)

goHook := &SimpleHook{}
moduleManager := NewMainModuleManager()

goHookRegistry := sdk.Registry()
goHookRegistry.Add(goHook)
var goHook go_hook.GoHook
gh := NewGlobalHook("simple.go", "simple.go")
for _, hookWithMeta := range sdk.Registry().Hooks() {
if hookWithMeta.Metadata.Name == "simple.go" && hookWithMeta.Metadata.Path == "simple.go" {
goHook = hookWithMeta.Hook
break
}
}

moduleManager := NewMainModuleManager()
g.Expect(goHook).ToNot(BeNil())

gh := NewGlobalHook("simple", "simple")
gh.WithGoHook(goHook)
err := gh.WithGoConfig(goHook.Config())
g.Expect(err).ShouldNot(HaveOccurred())
Expand Down
1 change: 1 addition & 0 deletions pkg/module_manager/module_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (m *ModuleHook) WithGoConfig(config *go_hook.HookConfig) (err error) {

// Make HookController and GetConfigDescription work.
m.Hook.Config = &m.Config.HookConfig
m.Hook.RateLimiter = hook.CreateRateLimiter(m.Hook.Config)
return nil
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/module_manager/module_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"

. "github.com/flant/addon-operator/pkg/hook/types"
Expand All @@ -24,6 +24,8 @@ import (
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/shell-operator/pkg/kube"
utils_file "github.com/flant/shell-operator/pkg/utils/file"

_ "github.com/flant/addon-operator/pkg/module_manager/test/go_hooks/global-hooks"
)

// initModuleManager is a test version of an Init method
Expand Down Expand Up @@ -1032,6 +1034,7 @@ func Test_MainModuleManager_Get_GlobalHooksInOrder(t *testing.T) {
"000-before-all-binding-hooks/b",
"000-before-all-binding-hooks/c",
"000-before-all-binding-hooks/a",
"simple.go",
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package global_go_hook
package global_hooks

import (
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/metric_storage/operation"
)

var _ = sdk.Register(&Simple{})

type Simple struct {
}

func (s *Simple) Metadata() *go_hook.HookMetadata {
return &go_hook.HookMetadata{
Name: "simple",
Path: "global-hooks/simple",
Global: true,
}
}

func (s *Simple) Config() *go_hook.HookConfig {
return &go_hook.HookConfig{
OnStartup: &go_hook.OrderedConfig{Order: 1},
OnAfterAll: &go_hook.OrderedConfig{Order: 5},
}
}

func (s *Simple) Run(input *go_hook.HookInput) error {
input.Values.Set("test", "test")
*input.Metrics = append(*input.Metrics, operation.MetricOperation{Name: "test"})

return nil
}
44 changes: 0 additions & 44 deletions pkg/module_manager/testdata/go_hooks/jq_filter_func.go

This file was deleted.

72 changes: 69 additions & 3 deletions sdk/registry.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package sdk

import (
"regexp"
"runtime"
"sync"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
)

// /path/.../global-hooks/a/b/c/Hook-name.go
// $1 - Hook path
// $3 - Hook name
var globalRe = regexp.MustCompile(`/global[\/\-]hooks/(([^/]+/)*([^/]+))$`)

// /path/.../modules/module-name/hooks/a/b/c/Hook-name.go
// $1 - Hook path
// $2 - module name
// $4 - Hook name
var moduleRe = regexp.MustCompile(`/modules/(([^/]+)/hooks/([^/]+/)*([^/]+))$`)

// TODO: This regexp should be changed. We shouldn't force users to name modules with a number prefix.
var moduleNameRe = regexp.MustCompile(`^[0-9][0-9][0-9]-(.*)$`)

var _ = initRegistry()

func initRegistry() bool {
Expand All @@ -22,8 +38,13 @@ func initRegistry() bool {
return true
}

type HookWithMetadata struct {
Hook go_hook.GoHook
Metadata *go_hook.HookMetadata
}

type HookRegistry struct {
hooks []go_hook.GoHook
hooks []HookWithMetadata
m sync.Mutex
}

Expand All @@ -37,12 +58,57 @@ func Registry() *HookRegistry {
return instance
}

func (h *HookRegistry) Hooks() []go_hook.GoHook {
func (h *HookRegistry) Hooks() []HookWithMetadata {
return h.hooks
}

func (h *HookRegistry) Add(hook go_hook.GoHook) {
h.m.Lock()
defer h.m.Unlock()
h.hooks = append(h.hooks, hook)

hookMeta := &go_hook.HookMetadata{}

pc := make([]uintptr, 50)
n := runtime.Callers(0, pc)
if n == 0 {
panic("runtime.Callers is empty")
}
pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
frames := runtime.CallersFrames(pc)

for {
frame, more := frames.Next()
matches := globalRe.FindStringSubmatch(frame.File)
if matches != nil {
hookMeta.Global = true
hookMeta.Name = matches[3]
hookMeta.Path = matches[1]
break
}

matches = moduleRe.FindStringSubmatch(frame.File)
if matches != nil {
hookMeta.Module = true
hookMeta.Name = matches[4]
hookMeta.Path = matches[1]
modNameMatches := moduleNameRe.FindStringSubmatch(matches[2])
if modNameMatches != nil {
hookMeta.ModuleName = modNameMatches[1]
}
break
}

if !more {
break
}
}

if len(hookMeta.Name) == 0 {
panic("cannot extract metadata from GoHook")
}

h.hooks = append(h.hooks, HookWithMetadata{
Hook: hook,
Metadata: hookMeta,
})
}
Loading

0 comments on commit 06119d4

Please sign in to comment.