diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 495254b9d..214ba7d75 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -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{} diff --git a/config/config.go b/config/config.go index 136a4f898..dc0fdca7e 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/config_test.go b/config/config_test.go index 64436fb36..5284699fa 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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", }, diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index df8cf62c6..33475960f 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -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 diff --git a/rules/manager.go b/rules/manager.go index c862fe677..1dccfcf5c 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -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.