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

Remove deprecated Init(), pass instance.Settings around. #10721

Merged
merged 6 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions libbeat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
"github.com/elastic/beats/libbeat/cmd/instance"
)

func genExportCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func genExportCmd(settings instance.Settings) *cobra.Command {
exportCmd := &cobra.Command{
Use: "export",
Short: "Export current config or index template",
}

exportCmd.AddCommand(export.GenExportConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenTemplateConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenIndexPatternConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenDashboardCmd(name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenGetILMPolicyCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenExportConfigCmd(settings))
exportCmd.AddCommand(export.GenTemplateConfigCmd(settings))
exportCmd.AddCommand(export.GenIndexPatternConfigCmd(settings))
exportCmd.AddCommand(export.GenDashboardCmd(settings))
exportCmd.AddCommand(export.GenGetILMPolicyCmd(settings))

return exportCmd
}
8 changes: 4 additions & 4 deletions libbeat/cmd/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (
)

// GenExportConfigCmd write to stdout the current configuration in the YAML format.
func GenExportConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenExportConfigCmd(settings instance.Settings) *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Export current config to stdout",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
return exportConfig(settings, name, idxPrefix, beatVersion)
return exportConfig(settings)
}),
}
}

func exportConfig(settings instance.Settings, name, idxPrefix, beatVersion string) error {
b, err := instance.NewBeat(name, idxPrefix, beatVersion)
func exportConfig(settings instance.Settings) error {
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
return fmt.Errorf("error initializing beat: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions libbeat/cmd/export/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// GenDashboardCmd is the command used to export a dashboard.
func GenDashboardCmd(name, idxPrefix, beatVersion string) *cobra.Command {
func GenDashboardCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "dashboard",
Short: "Export defined dashboard to stdout",
Expand All @@ -40,12 +40,12 @@ func GenDashboardCmd(name, idxPrefix, beatVersion string) *cobra.Command {
yml, _ := cmd.Flags().GetString("yml")
decode, _ := cmd.Flags().GetBool("decode")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating beat: %s\n", err)
os.Exit(1)
}
err = b.Init()
err = b.InitWithSettings(settings)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions libbeat/cmd/export/ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
)

// GenGetILMPolicyCmd is the command used to export the ilm policy.
func GenGetILMPolicyCmd(settings instance.Settings, name, idxPrefix, version string) *cobra.Command {
func GenGetILMPolicyCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "ilm-policy",
Short: "Export ILM policy",
Run: func(cmd *cobra.Command, args []string) {
b, err := instance.NewBeat(name, idxPrefix, version)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
Expand Down
9 changes: 4 additions & 5 deletions libbeat/cmd/export/index_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
)

// GenIndexPatternConfigCmd generates an index pattern for Kibana
func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenIndexPatternConfigCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "index-pattern",
Short: "Export kibana index pattern to stdout",
Run: func(cmd *cobra.Command, args []string) {
version, _ := cmd.Flags().GetString("es.version")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fatalf("Error initializing beat: %+v", err)
}
Expand All @@ -63,7 +63,7 @@ func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatV
if err != nil {
fatalf("Error creating version: %+v", err)
}
indexPattern, err := kibana.NewGenerator(b.Info.IndexPrefix, b.Info.Beat, b.Fields, beatVersion, *v, withMigration)
indexPattern, err := kibana.NewGenerator(b.Info.IndexPrefix, b.Info.Beat, b.Fields, settings.Version, *v, withMigration)
if err != nil {
log.Fatal(err)
}
Expand All @@ -80,8 +80,7 @@ func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatV
},
}

genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", idxPrefix, "Base index name")
genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")

return genTemplateConfigCmd
}
8 changes: 4 additions & 4 deletions libbeat/cmd/export/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/elastic/beats/libbeat/template"
)

func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenTemplateConfigCmd(settings instance.Settings) *cobra.Command {
simitt marked this conversation as resolved.
Show resolved Hide resolved
genTemplateConfigCmd := &cobra.Command{
Use: "template",
Short: "Export index template to stdout",
Expand All @@ -40,7 +40,7 @@ func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersi
index, _ := cmd.Flags().GetString("index")
noILM, _ := cmd.Flags().GetBool("noilm")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fatalf("Error initializing beat: %+v", err)
}
Expand Down Expand Up @@ -106,8 +106,8 @@ func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersi
},
}

genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", idxPrefix, "Base index name")
genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", settings.IndexPrefix, "Base index name")
genTemplateConfigCmd.Flags().Bool("noilm", false, "Generate template with ILM disabled")

return genTemplateConfigCmd
Expand Down
19 changes: 10 additions & 9 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ func (b *Beat) launch(settings Settings, bt beat.Creator) error {
}

// TestConfig check all settings are ok and the beat can be run
func (b *Beat) TestConfig(bt beat.Creator) error {
func (b *Beat) TestConfig(settings Settings, bt beat.Creator) error {
return handleError(func() error {
err := b.Init()
err := b.InitWithSettings(settings)
if err != nil {
return err
}
Expand All @@ -422,9 +422,9 @@ type SetupSettings struct {
}

// Setup registers ES index template, kibana dashboards, ml jobs and pipelines.
func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) error {
return handleError(func() error {
err := b.Init()
err := b.InitWithSettings(settings)
if err != nil {
return err
}
Expand All @@ -438,7 +438,7 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
return err
}

if settings.Template || settings.ILMPolicy {
if setup.Template || setup.ILMPolicy {
outCfg := b.Config.Output

if outCfg.Name() != "elasticsearch" {
Expand All @@ -455,15 +455,15 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
// prepare index by loading templates, lifecycle policies and write aliases

m := b.index.Manager(esClient, idxmgmt.BeatsAssets(b.Fields))
err = m.Setup(settings.Template, settings.ILMPolicy)
err = m.Setup(setup.Template, setup.ILMPolicy)
if err != nil {
return err
}
}
fmt.Println("Index setup complete.")
}

if settings.Dashboard {
if setup.Dashboard {
fmt.Println("Loading dashboards (Kibana must be running and reachable)")
err = b.loadDashboards(context.Background(), true)

Expand All @@ -479,15 +479,15 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
}
}

if settings.MachineLearning && b.SetupMLCallback != nil {
if setup.MachineLearning && b.SetupMLCallback != nil {
err = b.SetupMLCallback(&b.Beat, b.Config.Kibana)
if err != nil {
return err
}
fmt.Println("Loaded machine learning job configurations")
}

if settings.Pipeline && b.OverwritePipelinesCallback != nil {
if setup.Pipeline && b.OverwritePipelinesCallback != nil {
esConfig := b.Config.Output.Config()
err = b.OverwritePipelinesCallback(esConfig)
if err != nil {
Expand Down Expand Up @@ -681,6 +681,7 @@ func (b *Beat) loadDashboards(ctx context.Context, force bool) error {
}

if b.Config.Dashboards.Enabled() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line.

var withMigration bool
if b.RawConfig.HasField("migration") {
sub, err := b.RawConfig.Child("migration", -1)
Expand Down
41 changes: 19 additions & 22 deletions libbeat/cmd/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ import (
"github.com/elastic/beats/libbeat/keystore"
)

func getKeystore(name, version string) (keystore.Keystore, error) {
b, err := instance.NewBeat(name, "", version)
func getKeystore(settings instance.Settings) (keystore.Keystore, error) {
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)

if err != nil {
return nil, fmt.Errorf("error initializing beat: %s", err)
}

if err = b.Init(); err != nil {
if err = b.InitWithSettings(settings); err != nil {
return nil, fmt.Errorf("error initializing beat: %s", err)
}

Expand All @@ -56,44 +56,41 @@ func getKeystore(name, version string) (keystore.Keystore, error) {
// - add
// - remove
// - list
func genKeystoreCmd(
name, idxPrefix, version string,
runFlags *pflag.FlagSet,
) *cobra.Command {
func genKeystoreCmd(settings instance.Settings, runFlags *pflag.FlagSet) *cobra.Command {
keystoreCmd := cobra.Command{
Use: "keystore",
Short: "Manage secrets keystore",
}

keystoreCmd.AddCommand(genCreateKeystoreCmd(name, version))
keystoreCmd.AddCommand(genAddKeystoreCmd(name, version))
keystoreCmd.AddCommand(genRemoveKeystoreCmd(name, version))
keystoreCmd.AddCommand(genListKeystoreCmd(name, version))
keystoreCmd.AddCommand(genCreateKeystoreCmd(settings))
keystoreCmd.AddCommand(genAddKeystoreCmd(settings))
keystoreCmd.AddCommand(genRemoveKeystoreCmd(settings))
keystoreCmd.AddCommand(genListKeystoreCmd(settings))

return &keystoreCmd
}

func genCreateKeystoreCmd(name, version string) *cobra.Command {
func genCreateKeystoreCmd(settings instance.Settings) *cobra.Command {
var flagForce bool
command := &cobra.Command{
Use: "create",
Short: "Create keystore",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
return createKeystore(name, version, flagForce)
return createKeystore(settings, flagForce)
}),
}
command.Flags().BoolVar(&flagForce, "force", false, "override the existing keystore")
return command
}

func genAddKeystoreCmd(name, version string) *cobra.Command {
func genAddKeystoreCmd(settings instance.Settings) *cobra.Command {
var flagForce bool
var flagStdin bool
command := &cobra.Command{
Use: "add",
Short: "Add secret",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
store, err := getKeystore(settings)
if err != nil {
return err
}
Expand All @@ -105,12 +102,12 @@ func genAddKeystoreCmd(name, version string) *cobra.Command {
return command
}

func genRemoveKeystoreCmd(name, version string) *cobra.Command {
func genRemoveKeystoreCmd(settings instance.Settings) *cobra.Command {
return &cobra.Command{
Use: "remove",
Short: "remove secret",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
store, err := getKeystore(settings)
if err != nil {
return err
}
Expand All @@ -119,12 +116,12 @@ func genRemoveKeystoreCmd(name, version string) *cobra.Command {
}
}

func genListKeystoreCmd(name, version string) *cobra.Command {
func genListKeystoreCmd(settings instance.Settings) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List keystore",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
store, err := getKeystore(name, version)
store, err := getKeystore(settings)
if err != nil {
return err
}
Expand All @@ -133,8 +130,8 @@ func genListKeystoreCmd(name, version string) *cobra.Command {
}
}

func createKeystore(name, version string, force bool) error {
store, err := getKeystore(name, version)
func createKeystore(settings instance.Settings, force bool) error {
store, err := getKeystore(settings)
if err != nil {
return err
}
Expand All @@ -156,7 +153,7 @@ func createKeystore(name, version string, force bool) error {
return fmt.Errorf("Error creating the keystore: %s", err)
}
}
fmt.Printf("Created %s keystore\n", name)
fmt.Printf("Created %s keystore\n", settings.Name)
return nil
}

Expand Down
Loading