fix(agent): watch for changes in configuration files in config directories (#12127)
This commit is contained in:
parent
56551db856
commit
9d18c973cf
|
|
@ -62,6 +62,7 @@ type Telegraf struct {
|
|||
|
||||
inputFilters []string
|
||||
outputFilters []string
|
||||
configFiles []string
|
||||
|
||||
GlobalFlags
|
||||
WindowFlags
|
||||
|
|
@ -86,7 +87,7 @@ func (t *Telegraf) reloadLoop() error {
|
|||
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
||||
syscall.SIGTERM, syscall.SIGINT)
|
||||
if t.watchConfig != "" {
|
||||
for _, fConfig := range t.config {
|
||||
for _, fConfig := range t.configFiles {
|
||||
if _, err := os.Stat(fConfig); err == nil {
|
||||
go t.watchLocalConfig(signals, fConfig)
|
||||
} else {
|
||||
|
|
@ -161,30 +162,30 @@ func (t *Telegraf) watchLocalConfig(signals chan os.Signal, fConfig string) {
|
|||
}
|
||||
|
||||
func (t *Telegraf) runAgent(ctx context.Context) error {
|
||||
var configFiles []string
|
||||
// providing no "config" flag should load default config
|
||||
if len(t.config) == 0 {
|
||||
configFiles = append(configFiles, "")
|
||||
} else {
|
||||
configFiles = append(configFiles, t.config...)
|
||||
}
|
||||
|
||||
for _, fConfigDirectory := range t.configDir {
|
||||
files, err := config.WalkDirectory(fConfigDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
configFiles = append(configFiles, files...)
|
||||
}
|
||||
|
||||
// If no other options are specified, load the config file and run.
|
||||
c := config.NewConfig()
|
||||
c.OutputFilters = t.outputFilters
|
||||
c.InputFilters = t.inputFilters
|
||||
var err error
|
||||
// providing no "config" flag should load default config
|
||||
if len(t.config) == 0 {
|
||||
err = c.LoadConfig("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, fConfig := range t.config {
|
||||
err = c.LoadConfig(fConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, fConfigDirectory := range t.configDir {
|
||||
err = c.LoadDirectory(fConfigDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.configFiles = configFiles
|
||||
if err := c.LoadAll(configFiles...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !(t.test || t.testWait != 0) && len(c.Outputs) == 0 {
|
||||
|
|
@ -215,7 +216,7 @@ func (t *Telegraf) runAgent(ctx context.Context) error {
|
|||
LogWithTimezone: c.Agent.LogWithTimezone,
|
||||
}
|
||||
|
||||
err = logger.SetupLogging(logConfig)
|
||||
err := logger.SetupLogging(logConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -301,8 +301,9 @@ func sliceContains(name string, list []string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// LoadDirectory loads all toml config files found in the specified path, recursively.
|
||||
func (c *Config) LoadDirectory(path string) error {
|
||||
// WalkDirectory collects all toml files that need to be loaded
|
||||
func WalkDirectory(path string) ([]string, error) {
|
||||
var files []string
|
||||
walkfn := func(thispath string, info os.FileInfo, _ error) error {
|
||||
if info == nil {
|
||||
log.Printf("W! Telegraf is not permitted to read %s", thispath)
|
||||
|
|
@ -311,7 +312,7 @@ func (c *Config) LoadDirectory(path string) error {
|
|||
|
||||
if info.IsDir() {
|
||||
if strings.HasPrefix(info.Name(), "..") {
|
||||
// skip Kubernetes mounts, prevening loading the same config twice
|
||||
// skip Kubernetes mounts, preventing loading the same config twice
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
|
|
@ -321,13 +322,10 @@ func (c *Config) LoadDirectory(path string) error {
|
|||
if len(name) < 6 || name[len(name)-5:] != ".conf" {
|
||||
return nil
|
||||
}
|
||||
err := c.LoadConfig(thispath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files = append(files, thispath)
|
||||
return nil
|
||||
}
|
||||
return filepath.Walk(path, walkfn)
|
||||
return files, filepath.Walk(path, walkfn)
|
||||
}
|
||||
|
||||
// Try to find a default config file at these locations (in order):
|
||||
|
|
@ -386,6 +384,16 @@ func (c *Config) LoadConfig(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) LoadAll(configFiles ...string) error {
|
||||
for _, fConfig := range configFiles {
|
||||
if err := c.LoadConfig(fConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConfigData loads TOML-formatted config data
|
||||
func (c *Config) LoadConfigData(data []byte) error {
|
||||
tbl, err := parseConfig(data)
|
||||
|
|
|
|||
|
|
@ -140,8 +140,11 @@ func TestConfig_LoadSingleInput(t *testing.T) {
|
|||
|
||||
func TestConfig_LoadDirectory(t *testing.T) {
|
||||
c := NewConfig()
|
||||
require.NoError(t, c.LoadConfig("./testdata/single_plugin.toml"))
|
||||
require.NoError(t, c.LoadDirectory("./testdata/subconfig"))
|
||||
|
||||
files, err := WalkDirectory("./testdata/subconfig")
|
||||
files = append([]string{"./testdata/single_plugin.toml"}, files...)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, c.LoadAll(files...))
|
||||
|
||||
// Create the expected data
|
||||
expectedPlugins := make([]*MockupInputPlugin, 4)
|
||||
|
|
|
|||
Loading…
Reference in New Issue