Skip to content

Commit

Permalink
Revert "refactor fleet mode detection and storage" (#41151)
Browse files Browse the repository at this point in the history
* Revert "refactor fleet mode detection and storage (#40667)"

This reverts commit 3309620.

* fix import
  • Loading branch information
leehinman authored Oct 7, 2024
1 parent b7d7fec commit 60bdc6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
23 changes: 0 additions & 23 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ 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]))
Expand All @@ -131,27 +129,6 @@ 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
}

Expand Down
39 changes: 27 additions & 12 deletions libbeat/common/fleetmode/fleet_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,33 @@

package fleetmode

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
}
import (
"flag"

"github.com/elastic/elastic-agent-libs/config"
)

// Enabled returns true if the Beat is running under Elastic Agent.
// 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"
func Enabled() bool {
return managementEnabled
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
}

0 comments on commit 60bdc6a

Please sign in to comment.