-
Notifications
You must be signed in to change notification settings - Fork 172
Optimize roxagent binary size by 68% through package splitting #19804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "syscall" | ||
| "time" | ||
|
|
||
|
|
@@ -230,6 +231,15 @@ | |
| "github.com/stackrox/rox/pkg/sync" | ||
| "github.com/stackrox/rox/pkg/utils" | ||
| pkgVersion "github.com/stackrox/rox/pkg/version" | ||
|
|
||
| // BusyBox-style consolidation - import app packages | ||
| complianceapp "github.com/stackrox/rox/compliance/cmd/compliance/app" | ||
|
Check failure on line 236 in central/main.go
|
||
| roxagentapp "github.com/stackrox/rox/compliance/virtualmachines/roxagent/app" | ||
|
Check failure on line 237 in central/main.go
|
||
| configcontrollerapp "github.com/stackrox/rox/config-controller/app" | ||
| migratorapp "github.com/stackrox/rox/migrator/app" | ||
| admissioncontrolapp "github.com/stackrox/rox/sensor/admission-control/app" | ||
|
Check failure on line 240 in central/main.go
|
||
| kubernetessensorapp "github.com/stackrox/rox/sensor/kubernetes/app" | ||
| sensorupgraderapp "github.com/stackrox/rox/sensor/upgrader/app" | ||
| ) | ||
|
Comment on lines
+234
to
243
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix import formatting: pipeline failure due to extra blank line. The CI style check failed because of an extra blank line before the BusyBox imports block. Suggested fix pkgVersion "github.com/stackrox/rox/pkg/version"
-
// BusyBox-style consolidation - import app packages
complianceapp "github.com/stackrox/rox/compliance/cmd/compliance/app"🧰 Tools🪛 GitHub Actions: Style[error] 234-234: style-slim failed: Too many blank lines in imports (imports formatting check) 🤖 Prompt for AI Agents |
||
|
|
||
| var ( | ||
|
|
@@ -278,7 +288,8 @@ | |
| log.Info("Central terminated") | ||
| } | ||
|
|
||
| func main() { | ||
| // Main is the exported entry point for the central binary. | ||
| func Main() { | ||
| defer utils.IgnoreError(log.InnerLogger.Sync) | ||
|
|
||
| premain.StartMain() | ||
|
|
@@ -1061,3 +1072,59 @@ | |
| } | ||
| log.Info("Central terminated") | ||
| } | ||
|
|
||
| // Dispatcher wrapper functions for BusyBox-style invocation | ||
| func migratorMain() { | ||
| migratorapp.Run() | ||
| } | ||
|
|
||
| func complianceMain() { | ||
| complianceapp.Run() | ||
| } | ||
|
|
||
| func kubernetesSensorMain() { | ||
| kubernetessensorapp.Run() | ||
| } | ||
|
|
||
| func sensorUpgraderMain() { | ||
| sensorupgraderapp.Run() | ||
| } | ||
|
|
||
| func admissionControlMain() { | ||
| admissioncontrolapp.Run() | ||
| } | ||
|
|
||
| func configControllerMain() { | ||
| configcontrollerapp.Run() | ||
| } | ||
|
|
||
| func roxagentMain() { | ||
| roxagentapp.Run() | ||
| } | ||
|
|
||
| func main() { | ||
| // BusyBox-style dispatcher: check how we were called | ||
| binaryName := filepath.Base(os.Args[0]) | ||
|
|
||
| switch binaryName { | ||
| case "central": | ||
| Main() | ||
| case "migrator": | ||
| migratorMain() | ||
| case "compliance": | ||
| complianceMain() | ||
| case "kubernetes-sensor": | ||
|
Comment on lines
+1107
to
+1116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Defaulting to central on unknown binary names may hide misconfigurations. Because any unexpected Suggested implementation: func main() {
// BusyBox-style dispatcher: check how we were called
binaryName := filepath.Base(os.Args[0])
switch binaryName {
case "central":
Main()
case "migrator":
migratorMain()
case "compliance":
complianceMain()
case "kubernetes-sensor":
kubernetesSensorMain()
case "sensor-upgrader":
sensorUpgraderMain()
case "admission-control":
admissionControlMain()
case "config-controller":
configControllerMain()
case "roxagent":
roxagentMain()
default:
log.Errorf("unknown binary name %q; expected one of [central, migrator, compliance, kubernetes-sensor, sensor-upgrader, admission-control, config-controller, roxagent]", binaryName)
os.Exit(1)
}
}You will also need to:
|
||
| kubernetesSensorMain() | ||
| case "sensor-upgrader": | ||
| sensorUpgraderMain() | ||
| case "admission-control": | ||
| admissionControlMain() | ||
| case "config-controller": | ||
| configControllerMain() | ||
| case "roxagent": | ||
| roxagentMain() | ||
| default: | ||
| // Default to central if called with unknown name | ||
| Main() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/stackrox/rox/compliance" | ||
| "github.com/stackrox/rox/compliance/node" | ||
| "github.com/stackrox/rox/compliance/node/index" | ||
| "github.com/stackrox/rox/compliance/node/inventory" | ||
| "github.com/stackrox/rox/pkg/continuousprofiling" | ||
| "github.com/stackrox/rox/pkg/env" | ||
| "github.com/stackrox/rox/pkg/logging" | ||
| "github.com/stackrox/rox/pkg/memlimit" | ||
| "github.com/stackrox/rox/pkg/retry/handler" | ||
| ) | ||
|
|
||
| func init() { | ||
| memlimit.SetMemoryLimit() | ||
| } | ||
|
|
||
| var ( | ||
| log = logging.LoggerForModule() | ||
| ) | ||
|
|
||
| // Run is the main entry point for the compliance application. | ||
| func Run() { | ||
| if err := continuousprofiling.SetupClient(continuousprofiling.DefaultConfig()); err != nil { | ||
| log.Errorf("unable to start continuous profiling: %v", err) | ||
| } | ||
|
|
||
| np := &node.EnvNodeNameProvider{} | ||
| cfg := index.DefaultNodeIndexerConfig() | ||
|
|
||
| scanner := inventory.NewNodeInventoryComponentScanner(np) | ||
| scanner.Connect(env.NodeScanningEndpoint.Setting()) | ||
| cachedNodeIndexer := index.NewCachingNodeIndexer(cfg, env.NodeIndexCacheDuration.DurationSetting(), env.NodeIndexCachePath.Setting()) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| umhNodeInv := handler.NewUnconfirmedMessageHandler(ctx, "node-inventory", env.NodeScanningAckDeadlineBase.DurationSetting()) | ||
| umhNodeIndex := handler.NewUnconfirmedMessageHandler(ctx, "node-index", env.NodeScanningAckDeadlineBase.DurationSetting()) | ||
| c := compliance.NewComplianceApp(np, scanner, cachedNodeIndexer, umhNodeInv, umhNodeIndex) | ||
| c.Start() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,7 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/stackrox/rox/compliance" | ||
| "github.com/stackrox/rox/compliance/node" | ||
| "github.com/stackrox/rox/compliance/node/index" | ||
| "github.com/stackrox/rox/compliance/node/inventory" | ||
| "github.com/stackrox/rox/pkg/continuousprofiling" | ||
| "github.com/stackrox/rox/pkg/env" | ||
| "github.com/stackrox/rox/pkg/logging" | ||
| "github.com/stackrox/rox/pkg/memlimit" | ||
| "github.com/stackrox/rox/pkg/retry/handler" | ||
| ) | ||
|
|
||
| func init() { | ||
| memlimit.SetMemoryLimit() | ||
| } | ||
|
|
||
| var ( | ||
| log = logging.LoggerForModule() | ||
| ) | ||
| import "github.com/stackrox/rox/compliance/cmd/compliance/app" | ||
|
|
||
| func main() { | ||
| if err := continuousprofiling.SetupClient(continuousprofiling.DefaultConfig()); err != nil { | ||
| log.Errorf("unable to start continuous profiling: %v", err) | ||
| } | ||
|
|
||
| np := &node.EnvNodeNameProvider{} | ||
| cfg := index.DefaultNodeIndexerConfig() | ||
|
|
||
| scanner := inventory.NewNodeInventoryComponentScanner(np) | ||
| scanner.Connect(env.NodeScanningEndpoint.Setting()) | ||
| cachedNodeIndexer := index.NewCachingNodeIndexer(cfg, env.NodeIndexCacheDuration.DurationSetting(), env.NodeIndexCachePath.Setting()) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| umhNodeInv := handler.NewUnconfirmedMessageHandler(ctx, "node-inventory", env.NodeScanningAckDeadlineBase.DurationSetting()) | ||
| umhNodeIndex := handler.NewUnconfirmedMessageHandler(ctx, "node-index", env.NodeScanningAckDeadlineBase.DurationSetting()) | ||
| c := compliance.NewComplianceApp(np, scanner, cachedNodeIndexer, umhNodeInv, umhNodeIndex) | ||
| c.Start() | ||
| app.Run() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/stackrox/rox/compliance/virtualmachines/roxagent/cmd" | ||
| "github.com/stackrox/rox/pkg/logging" | ||
| ) | ||
|
|
||
| var log = logging.LoggerForModule() | ||
|
|
||
| // Run is the main entry point for the roxagent application. | ||
| func Run() { | ||
| // Create a context that is cancellable on the usual command line signals. Double | ||
| // signal forcefully exits. | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go func() { | ||
| sigC := make(chan os.Signal, 1) | ||
| signal.Notify(sigC, syscall.SIGINT, syscall.SIGTERM) | ||
| sig := <-sigC | ||
| log.Errorf("%s caught, shutting down...", sig) | ||
| // Cancel the main context. | ||
| cancel() | ||
| go func() { | ||
| // A second signal will forcefully quit. | ||
| <-sigC | ||
| os.Exit(1) | ||
| }() | ||
| }() | ||
| if err := cmd.RootCmd(ctx).Execute(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,7 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/stackrox/rox/compliance/virtualmachines/roxagent/cmd" | ||
| "github.com/stackrox/rox/pkg/logging" | ||
| ) | ||
|
|
||
| var log = logging.LoggerForModule() | ||
| import "github.com/stackrox/rox/compliance/virtualmachines/roxagent/app" | ||
|
|
||
| func main() { | ||
| // Create a context that is cancellable on the usual command line signals. Double | ||
| // signal forcefully exits. | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go func() { | ||
| sigC := make(chan os.Signal, 1) | ||
| signal.Notify(sigC, syscall.SIGINT, syscall.SIGTERM) | ||
| sig := <-sigC | ||
| log.Errorf("%s caught, shutting down...", sig) | ||
| // Cancel the main context. | ||
| cancel() | ||
| go func() { | ||
| // A second signal will forcefully quit. | ||
| <-sigC | ||
| os.Exit(1) | ||
| }() | ||
| }() | ||
| if err := cmd.RootCmd(ctx).Execute(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| app.Run() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Importing app packages into central causes their init() side effects (like memlimit.SetMemoryLimit) to run in the central process as well.
Some of these
.../apppackages (e.g. compliance, admission-control, k8s sensor, roxagent) callmemlimit.SetMemoryLimit()or other side-effectful logic ininit(). With these imports now always pulled into the central binary, that logic also runs when central starts, not just for the symlinked binaries. To avoid unintended impact on central’s own resource configuration, consider moving such calls out ofinit()and into the respectiveRun()paths so they only execute when that app is actually invoked.