Skip to content
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

Revert "refactor fleet mode detection and storage" #41151

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading