Skip to content

Commit 929a133

Browse files
authored
Addon-operator method calls (#381)
1 parent f01c24e commit 929a133

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

cmd/addon-operator/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"math/rand"
67
"os"
@@ -44,12 +45,11 @@ func main() {
4445
// Init rand generator.
4546
rand.Seed(time.Now().UnixNano())
4647

47-
operator := addon_operator.NewAddonOperator()
48-
err := addon_operator.Bootstrap(operator)
48+
operator := addon_operator.NewAddonOperator(context.Background())
49+
err := operator.Start()
4950
if err != nil {
5051
os.Exit(1)
5152
}
52-
operator.Start()
5353

5454
// Block action by waiting signals from OS.
5555
utils_signal.WaitForProcessInterruption(func() {

pkg/addon-operator/bootstrap.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package addon_operator
22

33
import (
4-
"context"
54
"fmt"
65

76
log "github.com/sirupsen/logrus"
@@ -17,7 +16,7 @@ import (
1716
)
1817

1918
// Bootstrap inits all dependencies for a full-fledged AddonOperator instance.
20-
func Bootstrap(op *AddonOperator) error {
19+
func (op *AddonOperator) bootstrap() error {
2120
runtimeConfig := config.NewConfig()
2221
// Init logging subsystem.
2322
sh_app.SetupLogging(runtimeConfig)
@@ -40,8 +39,6 @@ func Bootstrap(op *AddonOperator) error {
4039

4140
log.Infof("Addon-operator namespace: %s", app.Namespace)
4241

43-
op.WithContext(context.Background())
44-
4542
// Debug server.
4643
debugServer, err := shell_operator.InitDefaultDebugServer()
4744
if err != nil {
@@ -55,7 +52,7 @@ func Bootstrap(op *AddonOperator) error {
5552
return err
5653
}
5754

58-
err = AssembleAddonOperator(op, app.ModulesDir, globalHooksDir, tempDir, debugServer, runtimeConfig)
55+
err = op.Assemble(app.ModulesDir, globalHooksDir, tempDir, debugServer, runtimeConfig)
5956
if err != nil {
6057
log.Errorf("Fatal: %s", err)
6158
return err
@@ -64,17 +61,17 @@ func Bootstrap(op *AddonOperator) error {
6461
return nil
6562
}
6663

67-
func AssembleAddonOperator(op *AddonOperator, modulesDir string, globalHooksDir string, tempDir string, debugServer *debug.Server, runtimeConfig *config.Config) (err error) {
68-
RegisterDefaultRoutes(op)
64+
func (op *AddonOperator) Assemble(modulesDir string, globalHooksDir string, tempDir string, debugServer *debug.Server, runtimeConfig *config.Config) (err error) {
65+
op.RegisterDefaultRoutes()
6966
RegisterAddonOperatorMetrics(op.MetricStorage)
7067
StartLiveTicksUpdater(op.MetricStorage)
7168
StartTasksQueueLengthUpdater(op.MetricStorage, op.TaskQueues)
7269

7370
// Register routes in debug server.
7471
shell_operator.RegisterDebugQueueRoutes(debugServer, op.ShellOperator)
7572
shell_operator.RegisterDebugConfigRoutes(debugServer, runtimeConfig)
76-
RegisterDebugGlobalRoutes(debugServer, op)
77-
RegisterDebugModuleRoutes(debugServer, op)
73+
op.RegisterDebugGlobalRoutes(debugServer)
74+
op.RegisterDebugModuleRoutes(debugServer)
7875

7976
// Helm client factory.
8077
op.Helm, err = helm.InitHelmClientFactory(op.KubeClient)
@@ -89,7 +86,7 @@ func AssembleAddonOperator(op *AddonOperator, modulesDir string, globalHooksDir
8986
return fmt.Errorf("initialize Helm resources manager: %s", err)
9087
}
9188

92-
SetupModuleManager(op, modulesDir, globalHooksDir, tempDir, runtimeConfig)
89+
op.SetupModuleManager(modulesDir, globalHooksDir, tempDir, runtimeConfig)
9390

9491
err = op.InitModuleManager()
9592
if err != nil {
@@ -99,7 +96,7 @@ func AssembleAddonOperator(op *AddonOperator, modulesDir string, globalHooksDir
9996
return nil
10097
}
10198

102-
func SetupModuleManager(op *AddonOperator, modulesDir string, globalHooksDir string, tempDir string, runtimeConfig *config.Config) {
99+
func (op *AddonOperator) SetupModuleManager(modulesDir string, globalHooksDir string, tempDir string, runtimeConfig *config.Config) {
103100
// Create manager to check values in ConfigMap.
104101
kcfg := kube_config_manager.Config{
105102
Namespace: app.Namespace,

pkg/addon-operator/debug_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/flant/shell-operator/pkg/hook/types"
1414
)
1515

16-
func RegisterDebugGlobalRoutes(dbgSrv *debug.Server, op *AddonOperator) {
16+
func (op *AddonOperator) RegisterDebugGlobalRoutes(dbgSrv *debug.Server) {
1717
dbgSrv.Route("/global/list.{format:(json|yaml)}", func(_ *http.Request) (interface{}, error) {
1818
return map[string]interface{}{
1919
"globalHooks": op.ModuleManager.GetGlobalHooksNames(),
@@ -44,7 +44,7 @@ func RegisterDebugGlobalRoutes(dbgSrv *debug.Server, op *AddonOperator) {
4444
})
4545
}
4646

47-
func RegisterDebugModuleRoutes(dbgSrv *debug.Server, op *AddonOperator) {
47+
func (op *AddonOperator) RegisterDebugModuleRoutes(dbgSrv *debug.Server) {
4848
dbgSrv.Route("/module/list.{format:(json|yaml|text)}", func(_ *http.Request) (interface{}, error) {
4949
return map[string][]string{"enabledModules": op.ModuleManager.GetEnabledModuleNames()}, nil
5050
})

pkg/addon-operator/http_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/prometheus/client_golang/prometheus/promhttp"
99
)
1010

11-
func RegisterDefaultRoutes(op *AddonOperator) {
11+
func (op *AddonOperator) RegisterDefaultRoutes() {
1212
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
1313
_, _ = writer.Write([]byte(`<html>
1414
<head><title>Addon-operator</title></head>

pkg/addon-operator/operator.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ type AddonOperator struct {
5555
InitialKubeConfig *kube_config_manager.KubeConfig
5656
}
5757

58-
func NewAddonOperator() *AddonOperator {
58+
func NewAddonOperator(ctx context.Context) *AddonOperator {
59+
cctx, cancel := context.WithCancel(ctx)
60+
so := shell_operator.NewShellOperator()
61+
so.WithContext(cctx)
62+
5963
return &AddonOperator{
60-
ShellOperator: &shell_operator.ShellOperator{},
64+
ctx: cctx,
65+
cancel: cancel,
66+
ShellOperator: so,
6167
ConvergeState: NewConvergeState(),
6268
}
6369
}
6470

65-
func (op *AddonOperator) WithContext(ctx context.Context) *AddonOperator {
66-
op.ctx, op.cancel = context.WithCancel(ctx)
67-
op.ShellOperator.WithContext(op.ctx)
68-
return op
69-
}
70-
7171
func (op *AddonOperator) Stop() {
7272
if op.cancel != nil {
7373
op.cancel()
@@ -280,7 +280,11 @@ func (op *AddonOperator) RegisterManagerEventsHandlers() {
280280
}
281281

282282
// Start runs all managers, event and queue handlers.
283-
func (op *AddonOperator) Start() {
283+
func (op *AddonOperator) Start() error {
284+
if err := op.bootstrap(); err != nil {
285+
return err
286+
}
287+
284288
log.Info("Start first converge for modules")
285289
// Loading the onStartup hooks into the queue and running all modules.
286290
// Turning tracking changes on only after startup ends.
@@ -303,6 +307,8 @@ func (op *AddonOperator) Start() {
303307
op.KubeConfigManager.Start()
304308
op.ModuleManager.Start()
305309
op.StartModuleManagerEventHandler()
310+
311+
return nil
306312
}
307313

308314
// BootstrapMainQueue adds tasks to initiate Startup sequence:

pkg/addon-operator/operator_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ func assembleTestAddonOperator(t *testing.T, configPath string) (*AddonOperator,
102102
g.Expect(err).ShouldNot(HaveOccurred(), "Should create ConfigMap/%s", result.cmName)
103103

104104
// Assemble AddonOperator.
105-
op := NewAddonOperator()
106-
op.WithContext(context.Background())
105+
op := NewAddonOperator(context.Background())
107106
op.KubeClient = kubeClient
108107
// Mock helm client for ModuleManager
109108
result.helmClient = &mockhelm.Client{}

0 commit comments

Comments
 (0)