Skip to content

Commit b7c9701

Browse files
ldmonsterEvsyukov Denis
andauthored
[addon-operator] refactoring (#574)
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com> Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com> Co-authored-by: Evsyukov Denis <denis.evsyukov@flant.com> Co-authored-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
1 parent f6bf50b commit b7c9701

File tree

14 files changed

+1272
-891
lines changed

14 files changed

+1272
-891
lines changed

cmd/addon-operator/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,25 @@ func start(logger *log.Logger) func(_ *kingpin.ParseContext) error {
9090
}
9191
}
9292

93+
// run initializes and starts the addon operator, blocking until it receives an OS signal to shutdown.
9394
func run(ctx context.Context, operator *addon_operator.AddonOperator) error {
95+
// Create a ConfigMap-based backend for storing configuration
9496
bk := configmap.New(operator.KubeClient(), operator.DefaultNamespace, app.ConfigMapName, operator.Logger.Named("kube-config-manager"))
97+
// Configure the operator to use this backend for configuration management
9598
operator.SetupKubeConfigManager(bk)
9699

100+
// Setup the operator (initialize controllers, register hooks, etc.)
97101
if err := operator.Setup(); err != nil {
98102
operator.Logger.Fatal("setup failed", log.Err(err))
99103
}
100104

105+
// Start the operator (begin watching for events and executing hooks)
101106
if err := operator.Start(ctx); err != nil {
102107
operator.Logger.Fatal("start failed", log.Err(err))
103108
}
104109

105-
// Block action by waiting signals from OS.
110+
// Block the main goroutine until the process receives a termination signal
111+
// When a signal is received, stop the operator gracefully before exiting
106112
utils_signal.WaitForProcessInterruption(func() {
107113
operator.Stop()
108114
os.Exit(0)

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/go-openapi/strfmt v0.19.5
1717
github.com/go-openapi/swag v0.22.5
1818
github.com/go-openapi/validate v0.19.12
19-
github.com/goccy/go-graphviz v0.1.3
19+
github.com/goccy/go-graphviz v0.2.9
2020
github.com/gofrs/uuid/v5 v5.3.2
2121
github.com/hashicorp/go-multierror v1.1.1
2222
github.com/kennygrant/sanitize v1.2.4
@@ -60,6 +60,7 @@ require (
6060
github.com/containerd/log v0.1.0 // indirect
6161
github.com/containerd/platforms v0.2.1 // indirect
6262
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
63+
github.com/disintegration/imaging v1.6.2 // indirect
6364
github.com/distribution/reference v0.6.0 // indirect
6465
github.com/docker/cli v27.1.1+incompatible // indirect
6566
github.com/docker/distribution v2.8.3+incompatible // indirect
@@ -74,6 +75,7 @@ require (
7475
github.com/fatih/camelcase v1.0.0 // indirect
7576
github.com/fatih/color v1.13.0 // indirect
7677
github.com/felixge/httpsnoop v1.0.4 // indirect
78+
github.com/flopp/go-findfont v0.1.0 // indirect
7779
github.com/fogleman/gg v1.3.0 // indirect
7880
github.com/fvbommel/sortorder v1.1.0 // indirect
7981
github.com/go-errors/errors v1.4.2 // indirect
@@ -152,6 +154,7 @@ require (
152154
github.com/spf13/cast v1.5.0 // indirect
153155
github.com/spf13/cobra v1.8.1 // indirect
154156
github.com/spf13/pflag v1.0.5 // indirect
157+
github.com/tetratelabs/wazero v1.8.1 // indirect
155158
github.com/tidwall/match v1.1.1 // indirect
156159
github.com/tidwall/pretty v1.2.0 // indirect
157160
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
@@ -166,7 +169,7 @@ require (
166169
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
167170
golang.org/x/crypto v0.36.0 // indirect
168171
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
169-
golang.org/x/image v0.18.0 // indirect
172+
golang.org/x/image v0.21.0 // indirect
170173
golang.org/x/net v0.38.0 // indirect
171174
golang.org/x/oauth2 v0.21.0 // indirect
172175
golang.org/x/sync v0.12.0 // indirect

go.sum

Lines changed: 77 additions & 8 deletions
Large diffs are not rendered by default.

pkg/addon-operator/bootstrap.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ import (
1616
)
1717

1818
// Bootstrap inits all dependencies for a full-fledged AddonOperator instance.
19+
// It initializes the debug server and assembles all components needed for operation.
1920
func (op *AddonOperator) bootstrap() error {
21+
// Log application startup message
2022
log.Info(shapp.AppStartMessage)
2123

24+
// Log the path where modules will be searched
2225
log.Info("Search modules",
2326
slog.String("path", app.ModulesDir))
2427

28+
// Log the namespace in which the operator will work
2529
log.Info("Addon-operator namespace",
2630
slog.String("namespace", op.DefaultNamespace))
2731

28-
// Debug server.
32+
// Initialize the debug server for troubleshooting and monitoring
2933
// TODO: rewrite shapp global variables to the addon-operator ones
3034
var err error
3135
op.DebugServer, err = shell_operator.RunDefaultDebugServer(shapp.DebugUnixSocket, shapp.DebugHttpServerAddr, op.Logger.Named("debug-server"))
@@ -34,6 +38,7 @@ func (op *AddonOperator) bootstrap() error {
3438
return fmt.Errorf("start Debug server: %w", err)
3539
}
3640

41+
// Assemble all operator components including routes, admission server, metrics updaters, and module management
3742
err = op.Assemble(op.DebugServer)
3843
if err != nil {
3944
log.Error("Fatal", log.Err(err))
@@ -43,25 +48,34 @@ func (op *AddonOperator) bootstrap() error {
4348
return nil
4449
}
4550

51+
// Assemble initializes and connects components of the AddonOperator.
52+
// It sets up debugging http endpoints, starts netrics services, and initializes the module manager.
4653
func (op *AddonOperator) Assemble(debugServer *debug.Server) error {
54+
// Register default HTTP routes for the operator
4755
op.registerDefaultRoutes()
56+
57+
// Start the admission server if enabled in application configuration
4858
if app.AdmissionServerEnabled {
4959
op.AdmissionServer.start(op.ctx)
5060
}
61+
62+
// Start background updaters for metrics
5163
StartLiveTicksUpdater(op.engine.MetricStorage)
5264
StartTasksQueueLengthUpdater(op.engine.MetricStorage, op.engine.TaskQueues)
5365

54-
// Register routes in debug server.
66+
// Register debug HTTP endpoints to inspect internal state
5567
op.engine.RegisterDebugQueueRoutes(debugServer)
5668
op.engine.RegisterDebugConfigRoutes(debugServer, op.runtimeConfig)
5769
op.RegisterDebugGlobalRoutes(debugServer)
5870
op.RegisterDebugModuleRoutes(debugServer)
5971
op.RegisterDiscoveryRoute(debugServer)
6072

73+
// Initialize the module manager which handles modules lifecycle
6174
if err := op.InitModuleManager(); err != nil {
6275
return err
6376
}
6477

78+
// Register graph visualization routes after module manager is initialized
6579
op.RegisterDebugGraphRoutes(debugServer)
6680

6781
return nil
@@ -86,6 +100,7 @@ func (op *AddonOperator) SetupModuleManager(modulesDir string, globalHooksDir st
86100
TempDir: tempDir,
87101
ChrootDir: app.ShellChrootDir,
88102
}
103+
89104
deps := module_manager.ModuleManagerDependencies{
90105
KubeObjectPatcher: op.engine.ObjectPatcher,
91106
KubeEventsManager: op.engine.KubeEventsManager,

0 commit comments

Comments
 (0)