Skip to content

Commit

Permalink
refactor fleet mode detection and storage (#40667)
Browse files Browse the repository at this point in the history
* refactor fleet mode detection and storage
  • Loading branch information
leehinman authored Sep 4, 2024
1 parent 2b5384a commit 3309620
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
24 changes: 24 additions & 0 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cfgfile

import (
"flag"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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]))
Expand All @@ -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
}

Expand Down
39 changes: 12 additions & 27 deletions libbeat/common/fleetmode/fleet_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3309620

Please sign in to comment.