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

Cherry-pick #8449 to 6.x: Allow to pass config overrides via the Settings Struct #8515

Merged
merged 1 commit into from
Oct 4, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG-developer.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ The list below covers the major changes between 6.3.0 and master only.
- Libbeat provides a new function `cmd.GenRootCmdWithSettings` that should be preferred over deprecated functions
`cmd.GenRootCmd`, `cmd.GenRootCmdWithRunFlags`, and `cmd.GenRootCmdWithIndexPrefixWithRunFlags`. {pull}7850[7850]
- Set current year in generator templates. {pull}8396[8396]
- You can now override default settings of libbeat by using instance.Settings. {pull}8449[8449]
37 changes: 19 additions & 18 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
testConfig = flag.Bool("configtest", false, "Test configuration and exit.")

// Additional default settings, that must be available for variable expansion
defaults = mustNewConfigFrom(map[string]interface{}{
defaults = common.MustNewConfigFrom(map[string]interface{}{
"path": map[string]interface{}{
"home": ".", // to be initialized by beat
"config": "${path.home}",
Expand All @@ -63,14 +63,6 @@ func init() {
makePathFlag("path.logs", "Logs path")
}

func mustNewConfigFrom(from interface{}) *common.Config {
cfg, err := common.NewConfigFrom(from)
if err != nil {
panic(err)
}
return cfg
}

// ChangeDefaultCfgfileFlag replaces the value and default value for the `-c`
// flag so that it reflects the beat name.
func ChangeDefaultCfgfileFlag(beatName string) error {
Expand Down Expand Up @@ -105,7 +97,7 @@ func HandleFlags() error {
// structure. If path is empty this method reads from the configuration
// file specified by the '-c' command line flag.
func Read(out interface{}, path string) error {
config, err := Load(path)
config, err := Load(path, nil)
if err != nil {
return err
}
Expand All @@ -116,7 +108,7 @@ func Read(out interface{}, path string) error {
// Load reads the configuration from a YAML file structure. If path is empty
// this method reads from the configuration file specified by the '-c' command
// line flag.
func Load(path string) (*common.Config, error) {
func Load(path string, beatOverrides *common.Config) (*common.Config, error) {
var config *common.Config
var err error

Expand All @@ -142,13 +134,22 @@ func Load(path string) (*common.Config, error) {
return nil, err
}

config, err = common.MergeConfigs(
defaults,
config,
overwrites,
)
if err != nil {
return nil, err
if beatOverrides != nil {
config, err = common.MergeConfigs(
defaults,
beatOverrides,
config,
overwrites,
)
if err != nil {
return nil, err
}
} else {
config, err = common.MergeConfigs(
defaults,
config,
overwrites,
)
}

config.PrintDebugf("Complete configuration loaded:")
Expand Down
19 changes: 13 additions & 6 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ func NewBeat(name, indexPrefix, v string) (*Beat, error) {
return &Beat{Beat: b}, nil
}

// init does initialization of things common to all actions (read confs, flags)
func (b *Beat) Init() error {
// InitWithSettings does initialization of things common to all actions (read confs, flags)
func (b *Beat) InitWithSettings(settings Settings) error {
err := b.handleFlags()
if err != nil {
return err
Expand All @@ -239,13 +239,20 @@ func (b *Beat) Init() error {
return err
}

if err := b.configure(); err != nil {
if err := b.configure(settings); err != nil {
return err
}

return nil
}

// Init does initialization of things common to all actions (read confs, flags)
//
// Deprecated: use InitWithSettings
func (b *Beat) Init() error {
return b.InitWithSettings(Settings{})
}

// BeatConfig returns config section for this beat
func (b *Beat) BeatConfig() (*common.Config, error) {
configName := strings.ToLower(b.Info.Beat)
Expand Down Expand Up @@ -315,7 +322,7 @@ func (b *Beat) launch(settings Settings, bt beat.Creator) error {
defer logp.Sync()
defer logp.Info("%s stopped.", b.Info.Beat)

err := b.Init()
err := b.InitWithSettings(settings)
if err != nil {
return err
}
Expand Down Expand Up @@ -496,10 +503,10 @@ func (b *Beat) handleFlags() error {
// config reads the configuration file from disk, parses the common options
// defined in BeatConfig, initializes logging, and set GOMAXPROCS if defined
// in the config. Lastly it invokes the Config method implemented by the beat.
func (b *Beat) configure() error {
func (b *Beat) configure(settings Settings) error {
var err error

cfg, err := cfgfile.Load("")
cfg, err := cfgfile.Load("", settings.ConfigOverrides)
if err != nil {
return fmt.Errorf("error loading config file: %v", err)
}
Expand Down
12 changes: 7 additions & 5 deletions libbeat/cmd/instance/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ package instance
import (
"github.com/spf13/pflag"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/monitoring/report"
)

// Settings contains basic settings for any beat to pass into GenRootCmd
type Settings struct {
Name string
IndexPrefix string
Version string
Monitoring report.Settings
RunFlags *pflag.FlagSet
Name string
IndexPrefix string
Version string
Monitoring report.Settings
RunFlags *pflag.FlagSet
ConfigOverrides *common.Config
}