Skip to content

Commit 8ab6d62

Browse files
authored
Feat/load config interface (#544)
Signed-off-by: Pavel Okhlopkov <[email protected]> Co-authored-by: Pavel Okhlopkov <[email protected]>
1 parent 95c8f23 commit 8ab6d62

14 files changed

+1202
-1174
lines changed

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
GO=$(shell which go)
2+
GIT=$(shell which git)
3+
4+
.PHONY: go-check
5+
go-check:
6+
$(call error-if-empty,$(GO),go)
7+
8+
.PHONY: git-check
9+
git-check:
10+
$(call error-if-empty,$(GIT),git)
11+
12+
.PHONY: go-module-version
13+
go-module-version: go-check git-check
14+
@echo "go get $(shell $(GO) list ./cmd/addon-operator)@$(shell $(GIT) rev-parse HEAD)"
15+
16+
.PHONY: test
17+
test: go-check
18+
@$(GO) test --race --cover ./...
19+
20+
define error-if-empty
21+
@if [[ -z $(1) ]]; then echo "$(2) not installed"; false; fi
22+
endef

pkg/module_manager/go_hook/go_hook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/flant/addon-operator/pkg/module_manager/go_hook/metrics"
1515
"github.com/flant/addon-operator/pkg/utils"
16+
"github.com/flant/shell-operator/pkg/hook/config"
1617
objectpatch "github.com/flant/shell-operator/pkg/kube/object_patch"
1718
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
1819
)
@@ -22,6 +23,14 @@ type GoHook interface {
2223
Run(input *HookInput) error
2324
}
2425

26+
type HookConfigLoader interface {
27+
GetConfigForModule(moduleKind string) (*config.HookConfig, error)
28+
GetOnStartup() *float64
29+
GetBeforeAll() *float64
30+
GetAfterAll() *float64
31+
GetAfterDeleteHelm() *float64
32+
}
33+
2534
// MetricsCollector collects metric's records for exporting them as a batch
2635
type MetricsCollector interface {
2736
// Inc increments the specified Counter metric

pkg/module_manager/models/hooks/dependency.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hooks
33
import (
44
"context"
55

6+
gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook"
67
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
78
"github.com/flant/addon-operator/pkg/utils"
89
bindingcontext "github.com/flant/shell-operator/pkg/hook/binding_context"
@@ -59,3 +60,5 @@ type executableHook interface {
5960
BackportHookConfig(cfg *config.HookConfig)
6061
GetHookConfigDescription() string
6162
}
63+
64+
type hookConfigLoader gohook.HookConfigLoader

pkg/module_manager/models/hooks/global_hook.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package hooks
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76

87
addon_op_types "github.com/flant/addon-operator/pkg/hook/types"
@@ -14,53 +13,33 @@ import (
1413
// GlobalHook is a representation of the hook, which not belongs to any module
1514
type GlobalHook struct {
1615
executableHook
16+
hookConfigLoader
1717
config *GlobalHookConfig
1818
}
1919

20+
type executableHookWithLoad interface {
21+
executableHook
22+
hookConfigLoader
23+
}
24+
2025
// NewGlobalHook constructs a new global hook
2126
//
2227
// ex - is an executable hook instance (GoHook or ShellHook)
23-
func NewGlobalHook(ex executableHook) *GlobalHook {
28+
func NewGlobalHook(ex executableHookWithLoad) *GlobalHook {
2429
return &GlobalHook{
25-
executableHook: ex,
26-
config: &GlobalHookConfig{},
30+
executableHook: ex,
31+
hookConfigLoader: ex,
32+
config: &GlobalHookConfig{},
2733
}
2834
}
2935

3036
// InitializeHookConfig initializes the global hook config
3137
// for GoHook config is precompiled, so we just have to fetch it
3238
// for ShellHook, that hook will be run with `--config` flag, returns and parses the config
33-
func (h *GlobalHook) InitializeHookConfig() (err error) {
34-
switch hk := h.executableHook.(type) {
35-
case *kind.GoHook:
36-
cfg := hk.GetConfig()
37-
err := h.config.LoadAndValidateGoConfig(cfg)
38-
if err != nil {
39-
return err
40-
}
41-
42-
case *kind.ShellHook:
43-
cfg, err := hk.GetConfig()
44-
if err != nil {
45-
return err
46-
}
47-
err = h.config.LoadAndValidateShellConfig(cfg)
48-
if err != nil {
49-
return err
50-
}
51-
52-
case *kind.BatchHook:
53-
cfg, err := hk.GetConfig()
54-
if err != nil {
55-
return err
56-
}
57-
err = h.config.LoadAndValidateBatchConfig(&cfg[hk.ID])
58-
if err != nil {
59-
return err
60-
}
61-
62-
default:
63-
return fmt.Errorf("unknown hook kind: %s", reflect.TypeOf(hk))
39+
func (h *GlobalHook) InitializeHookConfig() error {
40+
err := h.config.LoadHookConfig(h.hookConfigLoader)
41+
if err != nil {
42+
return fmt.Errorf("load and validate hook config: %w", err)
6443
}
6544

6645
// Make HookController and GetConfigDescription work.

0 commit comments

Comments
 (0)