Skip to content

Commit

Permalink
Feat/load config interface (#544)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
Co-authored-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
ldmonster and Pavel Okhlopkov authored Dec 23, 2024
1 parent 95c8f23 commit 8ab6d62
Show file tree
Hide file tree
Showing 14 changed files with 1,202 additions and 1,174 deletions.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
GO=$(shell which go)
GIT=$(shell which git)

.PHONY: go-check
go-check:
$(call error-if-empty,$(GO),go)

.PHONY: git-check
git-check:
$(call error-if-empty,$(GIT),git)

.PHONY: go-module-version
go-module-version: go-check git-check
@echo "go get $(shell $(GO) list ./cmd/addon-operator)@$(shell $(GIT) rev-parse HEAD)"

.PHONY: test
test: go-check
@$(GO) test --race --cover ./...

define error-if-empty
@if [[ -z $(1) ]]; then echo "$(2) not installed"; false; fi
endef
9 changes: 9 additions & 0 deletions pkg/module_manager/go_hook/go_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/flant/addon-operator/pkg/module_manager/go_hook/metrics"
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/shell-operator/pkg/hook/config"
objectpatch "github.com/flant/shell-operator/pkg/kube/object_patch"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
)
Expand All @@ -22,6 +23,14 @@ type GoHook interface {
Run(input *HookInput) error
}

type HookConfigLoader interface {
GetConfigForModule(moduleKind string) (*config.HookConfig, error)
GetOnStartup() *float64
GetBeforeAll() *float64
GetAfterAll() *float64
GetAfterDeleteHelm() *float64
}

// MetricsCollector collects metric's records for exporting them as a batch
type MetricsCollector interface {
// Inc increments the specified Counter metric
Expand Down
3 changes: 3 additions & 0 deletions pkg/module_manager/models/hooks/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hooks
import (
"context"

gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind"
"github.com/flant/addon-operator/pkg/utils"
bindingcontext "github.com/flant/shell-operator/pkg/hook/binding_context"
Expand Down Expand Up @@ -59,3 +60,5 @@ type executableHook interface {
BackportHookConfig(cfg *config.HookConfig)
GetHookConfigDescription() string
}

type hookConfigLoader gohook.HookConfigLoader
49 changes: 14 additions & 35 deletions pkg/module_manager/models/hooks/global_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hooks

import (
"fmt"
"reflect"
"strings"

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

type executableHookWithLoad interface {
executableHook
hookConfigLoader
}

// NewGlobalHook constructs a new global hook
//
// ex - is an executable hook instance (GoHook or ShellHook)
func NewGlobalHook(ex executableHook) *GlobalHook {
func NewGlobalHook(ex executableHookWithLoad) *GlobalHook {
return &GlobalHook{
executableHook: ex,
config: &GlobalHookConfig{},
executableHook: ex,
hookConfigLoader: ex,
config: &GlobalHookConfig{},
}
}

// InitializeHookConfig initializes the global hook config
// for GoHook config is precompiled, so we just have to fetch it
// for ShellHook, that hook will be run with `--config` flag, returns and parses the config
func (h *GlobalHook) InitializeHookConfig() (err error) {
switch hk := h.executableHook.(type) {
case *kind.GoHook:
cfg := hk.GetConfig()
err := h.config.LoadAndValidateGoConfig(cfg)
if err != nil {
return err
}

case *kind.ShellHook:
cfg, err := hk.GetConfig()
if err != nil {
return err
}
err = h.config.LoadAndValidateShellConfig(cfg)
if err != nil {
return err
}

case *kind.BatchHook:
cfg, err := hk.GetConfig()
if err != nil {
return err
}
err = h.config.LoadAndValidateBatchConfig(&cfg[hk.ID])
if err != nil {
return err
}

default:
return fmt.Errorf("unknown hook kind: %s", reflect.TypeOf(hk))
func (h *GlobalHook) InitializeHookConfig() error {
err := h.config.LoadHookConfig(h.hookConfigLoader)
if err != nil {
return fmt.Errorf("load and validate hook config: %w", err)
}

// Make HookController and GetConfigDescription work.
Expand Down
Loading

0 comments on commit 8ab6d62

Please sign in to comment.