-
Notifications
You must be signed in to change notification settings - Fork 186
feat: add support for loading multiple config files and staleness check #908
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "sync" | ||
| "time" | ||
|
|
@@ -102,7 +103,9 @@ | |
| ServerName string `yaml:"server_name,omitempty"` | ||
|
|
||
| configFilePath string | ||
| configFilesPath []string | ||
| configLastModified time.Time | ||
| configFilesLastMod []time.Time | ||
| configRateLimitTime time.Time | ||
| stalenessCheckLock sync.Mutex | ||
| } | ||
|
|
@@ -124,7 +127,9 @@ | |
| }, | ||
| Logging: lo.New(), | ||
| Main: &MainConfig{ | ||
| ServerName: hn, | ||
| ServerName: hn, | ||
| configFilesPath: make([]string, 0), | ||
| configFilesLastMod: make([]time.Time, 0), | ||
| }, | ||
| MgmtConfig: mgmt.New(), | ||
| Metrics: mo.New(), | ||
|
|
@@ -143,6 +148,18 @@ | |
| } | ||
| } | ||
|
|
||
| func (c *Config) loadConfigs(flags *Flags) error { | ||
| ok, err := c.isDir(flags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !ok { | ||
| return c.loadFile(flags) | ||
| } | ||
| return c.loadAndMergeFiles(flags) | ||
| } | ||
|
|
||
| // loadFile loads application configuration from a YAML-formatted file. | ||
| func (c *Config) loadFile(flags *Flags) error { | ||
| b, err := os.ReadFile(flags.ConfigPath) | ||
|
|
@@ -154,10 +171,49 @@ | |
| return err | ||
| } | ||
| c.Main.configFilePath = flags.ConfigPath | ||
| c.Main.configLastModified = c.CheckFileLastModified() | ||
| c.Main.configLastModified = c.CheckFileLastModified("") | ||
| return nil | ||
| } | ||
|
|
||
| // loadAndMergeFiles loads application configuration from multiple YAML files | ||
| func (c *Config) loadAndMergeFiles(flags *Flags) error { | ||
| files, err := fs.Glob(os.DirFS(flags.ConfigPath), "*.yaml") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, file := range files { | ||
| path := flags.ConfigPath + "/" + file | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err = c.loadYAMLConfig(string(data)); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeated unmarshalling keeps distinct map keys, but a later file replaces an entire same-named backend. In a local case where the second fragment only overrode
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reporting this. You are absolutely right I verified the code and confirmed the behavior you described. The I agree that we need an explicit contract and among the options you suggested I propose implementing an explicit rejection. |
||
| return err | ||
| } | ||
|
|
||
| c.Main.configFilesPath = append( | ||
| c.Main.configFilesPath, | ||
| path, | ||
| ) | ||
| c.Main.configFilesLastMod = append( | ||
| c.Main.configFilesLastMod, | ||
| c.CheckFileLastModified(path), | ||
| ) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *Config) isDir(flags *Flags) (bool, error) { | ||
| finfo, err := os.Stat(flags.ConfigPath) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| return finfo.IsDir(), nil | ||
| } | ||
|
|
||
| // loadYAMLConfig loads application configuration from a YAML-formatted byte slice. | ||
| func (c *Config) loadYAMLConfig(yml string) error { | ||
| if err := c.detectListenerSections(yml); err != nil { | ||
|
|
@@ -186,15 +242,24 @@ | |
| } | ||
|
|
||
| // CheckFileLastModified returns the last modified date of the running config file, if present | ||
| func (c *Config) CheckFileLastModified() time.Time { | ||
| if c.Main == nil || c.Main.configFilePath == "" { | ||
| func (c *Config) CheckFileLastModified(confFile string) time.Time { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes an exported v2 method from |
||
| if c.Main == nil { | ||
| return time.Time{} | ||
| } | ||
| file, err := os.Stat(c.Main.configFilePath) | ||
|
|
||
| path := confFile | ||
| if path == "" { | ||
| path = c.Main.configFilePath | ||
| if path == "" { | ||
| return time.Time{} | ||
| } | ||
| } | ||
|
|
||
| fInfo, err := os.Stat(path) | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return file.ModTime() | ||
| return fInfo.ModTime() | ||
| } | ||
|
|
||
| // Process converts various raw config options into internal data structures | ||
|
|
@@ -252,7 +317,9 @@ | |
| nc.legacyMgmtUsed = c.legacyMgmtUsed | ||
|
|
||
| nc.Main.configFilePath = c.Main.configFilePath | ||
| nc.Main.configFilesPath = c.Main.configFilesPath | ||
| nc.Main.configLastModified = c.Main.configLastModified | ||
| nc.Main.configFilesLastMod = c.Main.configFilesLastMod | ||
| nc.Main.configRateLimitTime = c.Main.configRateLimitTime | ||
|
|
||
| nc.Metrics.ListenAddress = c.Metrics.ListenAddress | ||
|
|
@@ -311,8 +378,10 @@ | |
| c.Main.stalenessCheckLock.Lock() | ||
| defer c.Main.stalenessCheckLock.Unlock() | ||
|
|
||
| if c.Main == nil || c.Main.configFilePath == "" || | ||
| time.Now().Before(c.Main.configRateLimitTime) { | ||
| if c.Main == nil || | ||
| (len(c.Main.configFilesPath) == 0 && | ||
| (c.Main.configFilePath == "" || | ||
| time.Now().Before(c.Main.configRateLimitTime))) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For directory-backed configs, |
||
| return false | ||
| } | ||
|
|
||
|
|
@@ -321,7 +390,7 @@ | |
| } | ||
|
|
||
| c.Main.configRateLimitTime = time.Now().Add(time.Duration(c.MgmtConfig.ReloadRateLimit)) | ||
| t := c.CheckFileLastModified() | ||
|
Check failure on line 393 in pkg/config/config.go
|
||
| if t.IsZero() { | ||
| return false | ||
| } | ||
|
|
@@ -333,7 +402,8 @@ | |
| func (c *Config) CheckAndMarkReloadInProgress() bool { | ||
| c.Main.stalenessCheckLock.Lock() | ||
| defer c.Main.stalenessCheckLock.Unlock() | ||
| if c.Main == nil || c.Main.configFilePath == "" || | ||
| if c.Main == nil || | ||
| (c.Main.configFilePath == "" && len(c.Main.configFilesPath) == 0) || | ||
| time.Now().Before(c.Main.configRateLimitTime) { | ||
| return false | ||
| } | ||
|
|
@@ -341,7 +411,7 @@ | |
| c.MgmtConfig = mgmt.New() | ||
| } | ||
| c.Main.configRateLimitTime = time.Now().Add(time.Duration(c.MgmtConfig.ReloadRateLimit)) | ||
| t := c.CheckFileLastModified() | ||
|
Check failure on line 414 in pkg/config/config.go
|
||
| if t.IsZero() { | ||
| return false | ||
| } | ||
|
|
@@ -381,7 +451,10 @@ | |
| // ConfigFilePath returns the file path from which this configuration is based | ||
| func (c *Config) ConfigFilePath() string { | ||
| if c.Main != nil { | ||
| return c.Main.configFilePath | ||
| if len(c.Main.configFilesPath) == 0 { | ||
| return c.Main.configFilePath | ||
| } | ||
| return c.Flags.ConfigPath | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| return "" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only matches
.yaml. A directory containingtrickster.ymlloads no files and eventually returnsno valid backends configured, although the PR describes loading all YAML files. Is.ymlintentionally unsupported? If not, this should enumerate both extensions and return a specific error when no matching files are present.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out. This is indeed a bug and not intentional. I will push a commit as soon as possible to support both extensions and return a specific error when no matching files are found.