Skip to content

Commit

Permalink
Fixed Go hook registration
Browse files Browse the repository at this point in the history
  • Loading branch information
zuzzas committed Apr 3, 2021
1 parent 5a3d3d4 commit ec29765
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 86 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: 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
8 changes: 0 additions & 8 deletions pkg/module_manager/hook_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ import (
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},
Expand Down
3 changes: 2 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 Down Expand Up @@ -1032,6 +1032,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",
"jq_filter_func.go",
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package go_hooks
package global_hooks

import (
"fmt"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

const moduleName = "node_manager"

type dummyFilterable struct{}

func (f dummyFilterable) ApplyFilter(_ *unstructured.Unstructured) (go_hook.FilterResult, error) {
return &dummyFilterable{}, nil
}

var _ = sdk.RegisterFunc(&go_hook.HookConfig{
Queue: fmt.Sprintf("/modules/%s/handle_node_templates", moduleName),
Schedule: []go_hook.ScheduleConfig{
Expand All @@ -28,7 +35,7 @@ var _ = sdk.RegisterFunc(&go_hook.HookConfig{
FieldSelector: nil,
ExecuteHookOnSynchronization: go_hook.Bool(false),
WaitForSynchronization: go_hook.Bool(false),
Filterable: nil,
Filterable: &dummyFilterable{},
},
},
OnStartup: &go_hook.OrderedConfig{Order: 20},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global_go_hook
package global_hooks

import (
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
Expand All @@ -10,14 +10,6 @@ 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},
Expand Down
77 changes: 74 additions & 3 deletions sdk/registry.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
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/([^/]+/)*([^/]+))$`)

var moduleNameRe = regexp.MustCompile(`^[0-9][0-9][0-9]-(.*)$`)

var _ = initRegistry()

func initRegistry() bool {
Expand All @@ -22,8 +37,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 +57,63 @@ 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{
Name: "",
Path: "",
Global: false,
Module: false,
ModuleName: "",
}

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,
})
}
48 changes: 0 additions & 48 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package sdk

import (
"regexp"
"runtime"

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

Expand All @@ -15,19 +12,6 @@ var RegisterFunc = func(config *go_hook.HookConfig, reconcileFunc reconcileFunc)

var _ go_hook.GoHook = (*commonGoHook)(nil)

// /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/([^/]+/)*([^/]+))$`)

var moduleNameRe = regexp.MustCompile(`^[0-9][0-9][0-9]-(.*)$`)

type reconcileFunc func(input *go_hook.HookInput) error

type commonGoHook struct {
Expand All @@ -43,38 +27,6 @@ func (h *commonGoHook) Config() *go_hook.HookConfig {
return h.config
}

func (*commonGoHook) Metadata() *go_hook.HookMetadata {
hookMeta := &go_hook.HookMetadata{
Name: "",
Path: "",
Global: false,
Module: false,
ModuleName: "",
}

_, f, _, _ := runtime.Caller(1)

matches := globalRe.FindStringSubmatch(f)
if matches != nil {
hookMeta.Global = true
hookMeta.Name = matches[3]
hookMeta.Path = matches[1]
} else {
matches = moduleRe.FindStringSubmatch(f)
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]
}
}
}

return hookMeta
}

func (h *commonGoHook) Run(input *go_hook.HookInput) error {
return h.reconcileFunc(input)
}
2 changes: 1 addition & 1 deletion sdk/test/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_HookMetadata_from_runtime(t *testing.T) {
hooks := map[string]go_hook.HookMetadata{}

for _, h := range hookList {
hm := h.Metadata()
hm := h.Metadata
hooks[hm.Name] = *hm
}

Expand Down

0 comments on commit ec29765

Please sign in to comment.