Skip to content

Commit 07360ca

Browse files
authored
Don't register cfgfile flags on flag.CommandLine in func init() (#40993)
1 parent 40743f8 commit 07360ca

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

libbeat/cfgfile/cfgfile.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25+
"sync"
2526

2627
"github.com/elastic/beats/v7/libbeat/common"
2728
"github.com/elastic/beats/v7/libbeat/common/fleetmode"
@@ -34,8 +35,10 @@ var (
3435
// The default config cannot include the beat name as it is not initialized
3536
// when this variable is created. See ChangeDefaultCfgfileFlag which should
3637
// be called prior to flags.Parse().
37-
configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config")
38-
overwrites = config.SettingFlag(nil, "E", "Configuration overwrite")
38+
commandLine flag.FlagSet
39+
commandLineOnce sync.Once
40+
configfiles = config.StringArrFlag(&commandLine, "c", "beat.yml", "Configuration file, relative to path.config")
41+
overwrites = config.SettingFlag(&commandLine, "E", "Configuration overwrite")
3942

4043
// Additional default settings, that must be available for variable expansion
4144
defaults = config.MustNewConfigFrom(map[string]interface{}{
@@ -55,7 +58,7 @@ var (
5558
func init() {
5659
// add '-path.x' options overwriting paths in 'overwrites' config
5760
makePathFlag := func(name, usage string) *string {
58-
return config.ConfigOverwriteFlag(nil, overwrites, name, name, "", usage)
61+
return config.ConfigOverwriteFlag(&commandLine, overwrites, name, name, "", usage)
5962
}
6063

6164
homePath = makePathFlag("path.home", "Home path")
@@ -64,6 +67,17 @@ func init() {
6467
makePathFlag("path.logs", "Logs path")
6568
}
6669

70+
// InitFlags is for explicitly initializing the flags.
71+
// It may get called repeatedly for different flagsets, but not
72+
// twice for the same one.
73+
func InitFlags() {
74+
commandLineOnce.Do(func() {
75+
commandLine.VisitAll(func(f *flag.Flag) {
76+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
77+
})
78+
})
79+
}
80+
6781
// OverrideChecker checks if a config should be overwritten.
6882
type OverrideChecker func(*config.C) bool
6983

libbeat/cmd/instance/beat_integration_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package instance_test
1919

2020
import (
21+
"context"
2122
"encoding/json"
2223
"flag"
2324
"net/http"
@@ -26,6 +27,7 @@ import (
2627
"time"
2728

2829
"github.com/elastic/beats/v7/libbeat/beat"
30+
"github.com/elastic/beats/v7/libbeat/cfgfile"
2931
"github.com/elastic/beats/v7/libbeat/cmd/instance"
3032
"github.com/elastic/beats/v7/libbeat/mock"
3133
"github.com/elastic/elastic-agent-libs/config"
@@ -77,6 +79,7 @@ func (mb mockbeat) Stop() {
7779
}
7880

7981
func TestMonitoringNameFromConfig(t *testing.T) {
82+
8083
mockBeat := mockbeat{
8184
done: make(chan struct{}),
8285
initDone: make(chan struct{}),
@@ -90,9 +93,11 @@ func TestMonitoringNameFromConfig(t *testing.T) {
9093
go func() {
9194
defer wg.Done()
9295

96+
// Initialize cfgfile flags
97+
cfgfile.InitFlags()
9398
// Set the configuration file path flag so the beat can read it
94-
flag.Set("c", "testdata/mockbeat.yml")
95-
instance.Run(mock.Settings, func(_ *beat.Beat, _ *config.C) (beat.Beater, error) {
99+
_ = flag.Set("c", "testdata/mockbeat.yml")
100+
_ = instance.Run(mock.Settings, func(_ *beat.Beat, _ *config.C) (beat.Beater, error) {
96101
return &mockBeat, nil
97102
})
98103
}()
@@ -109,9 +114,13 @@ func TestMonitoringNameFromConfig(t *testing.T) {
109114
// the HTTP server goroutine
110115
time.Sleep(10 * time.Millisecond)
111116

112-
resp, err := http.Get("http://localhost:5066/state")
117+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost:5066/state", nil)
118+
if err != nil {
119+
t.Fatalf("error creating request: %v", err)
120+
}
121+
resp, err := http.DefaultClient.Do(req)
113122
if err != nil {
114-
t.Fatal("calling state endpoint: ", err.Error())
123+
t.Fatalf("calling state endpoint: %v", err)
115124
}
116125
defer resp.Body.Close()
117126

libbeat/cmd/root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type BeatsRootCmd struct {
6161
func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings) *BeatsRootCmd {
6262
// Add global Elasticsearch license endpoint check.
6363
// Check we are actually talking with Elasticsearch, to ensure that used features actually exist.
64-
elasticsearch.RegisterGlobalCallback(licenser.FetchAndVerify)
64+
_, _ = elasticsearch.RegisterGlobalCallback(licenser.FetchAndVerify)
6565

6666
if err := platformcheck.CheckNativePlatformCompat(); err != nil {
6767
fmt.Fprintf(os.Stderr, "Failed to initialize: %v\n", err)
@@ -78,9 +78,12 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings
7878
// Due to a dependence upon the beat name, the default config file path
7979
err := cfgfile.ChangeDefaultCfgfileFlag(settings.Name)
8080
if err != nil {
81-
panic(fmt.Errorf("failed to set default config file path: %v", err))
81+
panic(fmt.Errorf("failed to set default config file path: %w", err))
8282
}
8383

84+
// Initialize the configuration flags.
85+
cfgfile.InitFlags()
86+
8487
// must be updated prior to CLI flag handling.
8588

8689
rootCmd.RunCmd = genRunCmd(settings, beatCreator)

0 commit comments

Comments
 (0)