fix(agent): watch for changes in configuration files in config directories (#12127)

This commit is contained in:
Ilya Arkhanhelsky 2022-11-07 21:54:52 +02:00 committed by GitHub
parent 56551db856
commit 9d18c973cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 31 deletions

View File

@ -62,6 +62,7 @@ type Telegraf struct {
inputFilters []string inputFilters []string
outputFilters []string outputFilters []string
configFiles []string
GlobalFlags GlobalFlags
WindowFlags WindowFlags
@ -86,7 +87,7 @@ func (t *Telegraf) reloadLoop() error {
signal.Notify(signals, os.Interrupt, syscall.SIGHUP, signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
syscall.SIGTERM, syscall.SIGINT) syscall.SIGTERM, syscall.SIGINT)
if t.watchConfig != "" { if t.watchConfig != "" {
for _, fConfig := range t.config { for _, fConfig := range t.configFiles {
if _, err := os.Stat(fConfig); err == nil { if _, err := os.Stat(fConfig); err == nil {
go t.watchLocalConfig(signals, fConfig) go t.watchLocalConfig(signals, fConfig)
} else { } else {
@ -161,30 +162,30 @@ func (t *Telegraf) watchLocalConfig(signals chan os.Signal, fConfig string) {
} }
func (t *Telegraf) runAgent(ctx context.Context) error { 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. // If no other options are specified, load the config file and run.
c := config.NewConfig() c := config.NewConfig()
c.OutputFilters = t.outputFilters c.OutputFilters = t.outputFilters
c.InputFilters = t.inputFilters 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 { t.configFiles = configFiles
err = c.LoadDirectory(fConfigDirectory) if err := c.LoadAll(configFiles...); err != nil {
if err != nil { return err
return err
}
} }
if !(t.test || t.testWait != 0) && len(c.Outputs) == 0 { 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, LogWithTimezone: c.Agent.LogWithTimezone,
} }
err = logger.SetupLogging(logConfig) err := logger.SetupLogging(logConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -301,8 +301,9 @@ func sliceContains(name string, list []string) bool {
return false return false
} }
// LoadDirectory loads all toml config files found in the specified path, recursively. // WalkDirectory collects all toml files that need to be loaded
func (c *Config) LoadDirectory(path string) error { func WalkDirectory(path string) ([]string, error) {
var files []string
walkfn := func(thispath string, info os.FileInfo, _ error) error { walkfn := func(thispath string, info os.FileInfo, _ error) error {
if info == nil { if info == nil {
log.Printf("W! Telegraf is not permitted to read %s", thispath) 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 info.IsDir() {
if strings.HasPrefix(info.Name(), "..") { 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 return filepath.SkipDir
} }
@ -321,13 +322,10 @@ func (c *Config) LoadDirectory(path string) error {
if len(name) < 6 || name[len(name)-5:] != ".conf" { if len(name) < 6 || name[len(name)-5:] != ".conf" {
return nil return nil
} }
err := c.LoadConfig(thispath) files = append(files, thispath)
if err != nil {
return err
}
return nil 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): // 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 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 // LoadConfigData loads TOML-formatted config data
func (c *Config) LoadConfigData(data []byte) error { func (c *Config) LoadConfigData(data []byte) error {
tbl, err := parseConfig(data) tbl, err := parseConfig(data)

View File

@ -140,8 +140,11 @@ func TestConfig_LoadSingleInput(t *testing.T) {
func TestConfig_LoadDirectory(t *testing.T) { func TestConfig_LoadDirectory(t *testing.T) {
c := NewConfig() 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 // Create the expected data
expectedPlugins := make([]*MockupInputPlugin, 4) expectedPlugins := make([]*MockupInputPlugin, 4)