diff --git a/pkg/alerting/alert_manager.go b/pkg/alerting/alert_manager.go index 5fb040243..0d836ce93 100644 --- a/pkg/alerting/alert_manager.go +++ b/pkg/alerting/alert_manager.go @@ -17,8 +17,6 @@ package alerting import ( "bytes" "fmt" - "io" - "os" "strings" "text/template" "time" @@ -82,12 +80,7 @@ func NewAlertManager(alertProfile string, prometheusClient *prometheus.Prometheu } func (a *AlertManager) readProfile(alertProfile string) error { - var f io.Reader - f, err := os.Open(alertProfile) - // If the alertProfile file does not exist we try to read it from an URL - if os.IsNotExist(err) { - f, err = util.ReadURL(alertProfile) - } + f, err := util.ReadConfig(alertProfile) if err != nil { log.Fatalf("Error reading alert profile %s: %s", alertProfile, err) } diff --git a/pkg/burner/create.go b/pkg/burner/create.go index b23569cda..9fe95ebdb 100644 --- a/pkg/burner/create.go +++ b/pkg/burner/create.go @@ -19,7 +19,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "strconv" "sync" "time" @@ -59,11 +58,7 @@ func setupCreateJob(jobConfig config.Job) Executor { continue } log.Debugf("Processing template: %s", o.ObjectTemplate) - f, err = os.Open(o.ObjectTemplate) - // If the template file does not exist we try to read it from an URL - if os.IsNotExist(err) { - f, err = util.ReadURL(o.ObjectTemplate) - } + f, err = util.ReadConfig(o.ObjectTemplate) if err != nil { log.Fatalf("Error reading template %s: %s", o.ObjectTemplate, err) } diff --git a/pkg/config/config.go b/pkg/config/config.go index a929aa4e7..bb3d2eb1d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -16,7 +16,6 @@ package config import ( "fmt" - "io" "os" "path/filepath" "time" @@ -67,12 +66,7 @@ func (j *Job) UnmarshalYAML(unmarshal func(interface{}) error) error { // Parse parses configuration file func Parse(c string, jobsRequired bool) error { - var f io.Reader - f, err := os.Open(c) - // If the config file does not exist we try to read it from an URL - if os.IsNotExist(err) { - f, err = util.ReadURL(c) - } + f, err := util.ReadConfig(c) if err != nil { return fmt.Errorf("Error reading configuration file %s: %s", c, err) } diff --git a/pkg/prometheus/prometheus.go b/pkg/prometheus/prometheus.go index 957207e4c..5169afa71 100644 --- a/pkg/prometheus/prometheus.go +++ b/pkg/prometheus/prometheus.go @@ -19,7 +19,6 @@ import ( "crypto/tls" "encoding/json" "fmt" - "io" "math" "net/http" "os" @@ -126,12 +125,7 @@ func (p *Prometheus) verifyConnection() error { // ReadProfile reads and parses metric profile configuration func (p *Prometheus) ReadProfile(metricsProfile string) error { - var f io.Reader - f, err := os.Open(metricsProfile) - // If the metricsProfile file does not exist we try to read it from an URL - if os.IsNotExist(err) { - f, err = util.ReadURL(metricsProfile) - } + f, err := util.ReadConfig(metricsProfile) if err != nil { log.Fatalf("Error reading metrics profile %s: %s", metricsProfile, err) } @@ -150,7 +144,7 @@ func (p *Prometheus) ScrapeMetrics(start, end time.Time, indexer *indexers.Index return err } -// ScrapeMetrics gets all prometheus metrics required and handles them +// ScrapeJobsMetrics gets all prometheus metrics required and handles them func (p *Prometheus) ScrapeJobsMetrics(jobList []burner.Executor, indexer *indexers.Indexer) error { start := jobList[0].Start end := jobList[len(jobList)-1].End diff --git a/pkg/util/url_reader.go b/pkg/util/url_reader.go index 9b2d63d7a..0cd1cae3a 100644 --- a/pkg/util/url_reader.go +++ b/pkg/util/url_reader.go @@ -19,12 +19,25 @@ import ( "io" "net/http" "net/url" + "os" + + "github.com/cloud-bulldozer/kube-burner/log" ) -// TODO: this function should open a file and fallback to URL if not found +// ReadConfig reads configuration from the given path or URL +func ReadConfig(configFile string) (io.Reader, error) { + var f io.Reader + f, err := os.Open(configFile) + // If the template file does not exist we try to read it from an URL + if os.IsNotExist(err) { + log.Warnf("Configuration file %s not found, trying to read from URL", configFile) + f, err = readURL(configFile) + } + return f, err +} -// ReadURL reads an URL and returns a reader -func ReadURL(stringURL string) (io.Reader, error) { +// readURL reads an URL and returns a reader +func readURL(stringURL string) (io.Reader, error) { var body io.Reader u, err := url.Parse(stringURL) if err != nil { @@ -40,6 +53,5 @@ func ReadURL(stringURL string) (io.Reader, error) { if r.StatusCode != http.StatusOK { return body, fmt.Errorf("Error requesting %s: %d", u, r.StatusCode) } - body = r.Body - return body, nil + return r.Body, nil }