Resolve relative paths on configuration loading

This moves the concern of resolving the files relative to the config
file into the configuration loading itself.
It also fixes #921 which did not load the cert and token files relatively.
This commit is contained in:
Fabian Reinartz 2015-08-05 18:04:34 +02:00
parent e324910ff2
commit 7a67472fc1
5 changed files with 41 additions and 16 deletions

View File

@ -21,7 +21,6 @@ import (
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"text/template"
@ -77,7 +76,6 @@ func Main() int {
NotificationHandler: notificationHandler,
QueryEngine: queryEngine,
ExternalURL: cfg.web.ExternalURL,
BaseDir: filepath.Dir(cfg.configFile),
})
flags := map[string]string{}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"regexp"
"strings"
"time"
@ -45,7 +46,12 @@ func LoadFromFile(filename string) (*Config, error) {
if err != nil {
return nil, err
}
return Load(string(content))
cfg, err := Load(string(content))
if err != nil {
return nil, err
}
resolveFilepaths(filepath.Dir(filename), cfg)
return cfg, nil
}
// The defaults applied before parsing the respective config sections.
@ -113,6 +119,30 @@ type Config struct {
original string
}
// resolveFilepaths joins all relative paths in a configuration
// with a given base directory.
func resolveFilepaths(baseDir string, cfg *Config) {
join := func(fp string) string {
if len(fp) > 0 && !filepath.IsAbs(fp) {
fp = filepath.Join(baseDir, fp)
}
return fp
}
for i, rf := range cfg.RuleFiles {
cfg.RuleFiles[i] = join(rf)
}
for _, scfg := range cfg.ScrapeConfigs {
scfg.BearerTokenFile = join(scfg.BearerTokenFile)
if scfg.ClientCert != nil {
scfg.ClientCert.Cert = join(scfg.ClientCert.Cert)
scfg.ClientCert.Key = join(scfg.ClientCert.Key)
}
}
}
func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string

View File

@ -27,9 +27,9 @@ var expectedConf = &Config{
},
RuleFiles: []string{
"first.rules",
"second.rules",
"my/*.rules",
"testdata/first.rules",
"/absolute/second.rules",
"testdata/my/*.rules",
},
ScrapeConfigs: []*ScrapeConfig{
@ -43,6 +43,8 @@ var expectedConf = &Config{
MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme,
BearerTokenFile: "testdata/valid_token_file",
TargetGroups: []*TargetGroup{
{
Targets: []clientmodel.LabelSet{
@ -167,8 +169,8 @@ var expectedConf = &Config{
Scheme: "http",
ClientCert: &ClientCert{
Cert: "valid_cert_file",
Key: "valid_key_file",
Cert: "testdata/valid_cert_file",
Key: "testdata/valid_key_file",
},
BearerToken: "avalidtoken",
},

View File

@ -10,7 +10,7 @@ global:
rule_files:
- "first.rules"
- "second.rules"
- "/absolute/second.rules"
- "my/*.rules"
scrape_configs:
@ -45,6 +45,8 @@ scrape_configs:
replacement: foo-${1}
# action defaults to 'replace'
bearer_token_file: valid_token_file
- job_name: service-x

View File

@ -104,7 +104,6 @@ type Manager struct {
notificationHandler *notification.NotificationHandler
externalURL *url.URL
baseDir string
}
// ManagerOptions bundles options for the Manager.
@ -116,7 +115,6 @@ type ManagerOptions struct {
SampleAppender storage.SampleAppender
ExternalURL *url.URL
BaseDir string
}
// NewManager returns an implementation of Manager, ready to be started
@ -131,7 +129,6 @@ func NewManager(o *ManagerOptions) *Manager {
queryEngine: o.QueryEngine,
notificationHandler: o.NotificationHandler,
externalURL: o.ExternalURL,
baseDir: o.BaseDir,
}
return manager
}
@ -330,10 +327,6 @@ func (m *Manager) ApplyConfig(conf *config.Config) bool {
var files []string
for _, pat := range conf.RuleFiles {
if !filepath.IsAbs(pat) {
pat = filepath.Join(m.baseDir, pat)
}
fs, err := filepath.Glob(pat)
if err != nil {
// The only error can be a bad pattern.