Skip to content

Commit

Permalink
Config retrieval fallback function
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 committed Jan 7, 2021
1 parent 87e28d5 commit 138f5f3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
9 changes: 1 addition & 8 deletions pkg/alerting/alert_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package alerting
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/burner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package config

import (
"fmt"
"io"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"os"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions pkg/util/url_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 138f5f3

Please sign in to comment.