From 33096202ff90c79daeb59447e61ff3430a6b0ea2 Mon Sep 17 00:00:00 2001 From: Lee E Hinman <57081003+leehinman@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:54:29 -0500 Subject: [PATCH] refactor fleet mode detection and storage (#40667) * refactor fleet mode detection and storage --- libbeat/cfgfile/cfgfile.go | 24 ++++++++++++++++ libbeat/common/fleetmode/fleet_mode.go | 39 ++++++++------------------ 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index ca19af8cb9f..21d052d64da 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -18,6 +18,7 @@ package cfgfile import ( + "flag" "fmt" "os" "path/filepath" @@ -97,6 +98,8 @@ func GetDefaultCfgfile() string { } // HandleFlags adapts default config settings based on command line flags. +// This also stores if -E management.enabled=true was set on command line +// to determine if running the Beat under agent. func HandleFlags() error { // default for the home path is the binary location home, err := filepath.Abs(filepath.Dir(os.Args[0])) @@ -114,6 +117,27 @@ func HandleFlags() error { common.PrintConfigDebugf(overwrites, "CLI setting overwrites (-E flag):") } + // Enable check to see if beat is running under Agent + // This is stored in a package so the modules which don't have + // access to the config can check this value. + type management struct { + Enabled bool `config:"management.enabled"` + } + var managementSettings management + cfgFlag := flag.Lookup("E") + if cfgFlag == nil { + fleetmode.SetAgentMode(false) + return nil + } + cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) + cliCfg := cfgObject.Config() + + err = cliCfg.Unpack(&managementSettings) + if err != nil { + fleetmode.SetAgentMode(false) + return nil //nolint:nilerr // unpacking failing isn't an error for this case + } + fleetmode.SetAgentMode(managementSettings.Enabled) return nil } diff --git a/libbeat/common/fleetmode/fleet_mode.go b/libbeat/common/fleetmode/fleet_mode.go index af179b887ea..97a17804f64 100644 --- a/libbeat/common/fleetmode/fleet_mode.go +++ b/libbeat/common/fleetmode/fleet_mode.go @@ -17,33 +17,18 @@ package fleetmode -import ( - "flag" - - "github.com/elastic/elastic-agent-libs/config" -) +var managementEnabled bool + +// SetAgentMode stores if the Beat is running under Elastic Agent. +// Normally this is called when the command line flags are parsed. +// This is stored as a package level variable because some components +// (like filebeat/metricbeat modules) don't have access to the +// configuration information to determine this on their own. +func SetAgentMode(enabled bool) { + managementEnabled = enabled +} -// Enabled checks to see if filebeat/metricbeat is running under Agent -// The management setting is stored in the main Beat runtime object, but we can't see that from a module -// So instead we check the CLI flags, since Agent starts filebeat/metricbeat with "-E", "management.enabled=true" +// Enabled returns true if the Beat is running under Elastic Agent. func Enabled() bool { - type management struct { - Enabled bool `config:"management.enabled"` - } - var managementSettings management - - cfgFlag := flag.Lookup("E") - if cfgFlag == nil { - return false - } - - cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) - cliCfg := cfgObject.Config() - - err := cliCfg.Unpack(&managementSettings) - if err != nil { - return false - } - - return managementSettings.Enabled + return managementEnabled }