Skip to content
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

fix: read harvest.yml from HARVEST_CONF when set #2367

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func doTLS(_ *cobra.Command, _ []string) {

func doAdmin(c *cobra.Command, _ []string) {
var configPath = c.Root().PersistentFlags().Lookup("config").Value.String()
err := conf.LoadHarvestConfig(configPath)
_, err := conf.LoadHarvestConfig(configPath)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/collectors/unix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (u *Unix) PollInstance() (map[string]*matrix.Matrix, error) {
currInstances := set.NewFrom(mat.GetInstanceKeys())
currSize := currInstances.Size()

err := conf.LoadHarvestConfig(u.Options.Config)
_, err := conf.LoadHarvestConfig(u.Options.Config)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/exporters/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func setupInfluxDB(t *testing.T, exporterName string) *InfluxDB {
opts := options.New()
opts.Debug = true

err := conf.LoadHarvestConfig("../../tools/doctor/testdata/testConfig.yml")
_, err := conf.LoadHarvestConfig("../../tools/doctor/testdata/testConfig.yml")
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/harvest/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func doManageCmd(cmd *cobra.Command, args []string) {

// cmd.DebugFlags() // uncomment to print flags

if err = conf.LoadHarvestConfig(opts.config); err != nil {
_, err = conf.LoadHarvestConfig(opts.config)
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("config [%s]: not found\n", opts.config)
}
Expand Down
23 changes: 17 additions & 6 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ type Poller struct {
// starts collectors and exporters
func (p *Poller) Init() error {

var err error
var (
err error
fileLoggingEnabled bool
consoleLoggingEnabled bool
configPath string
)

p.options = opts.SetDefaults()
p.name = opts.Poller

var fileLoggingEnabled bool
var consoleLoggingEnabled bool
zeroLogLevel := logging.GetZerologLevel(p.options.LogLevel)
// if we are a daemon, use file logging
if p.options.Daemon {
Expand All @@ -147,18 +151,24 @@ func (p *Poller) Init() error {
logFileName = "poller_" + p.name + ".log"
}

err = conf.LoadHarvestConfig(p.options.Config)
configPath, err = conf.LoadHarvestConfig(p.options.Config)
if err != nil {
// separate logger is not yet configured as it depends on setting logMaxMegaBytes, logMaxFiles later
// Using default instance of logger which logs below error to harvest.log
logging.Get().SubLogger("Poller", p.name).Error().
Str("config", p.options.Config).Err(err).Msg("Unable to read config")
Str("config", p.options.Config).
Str("configPath", configPath).
Err(err).
Msg("Unable to read config")
return err
}
p.params, err = conf.PollerNamed(p.name)
if err != nil {
logging.Get().SubLogger("Poller", p.name).Error().
Str("config", p.options.Config).Err(err).Msg("Failed to find poller")
Str("config", p.options.Config).
Str("configPath", configPath).
Err(err).
Msg("Failed to find poller")
return err
}

Expand Down Expand Up @@ -199,6 +209,7 @@ func (p *Poller) Init() error {

logger.Info().
Str("logLevel", zeroLogLevel.String()).
Str("configPath", configPath).
Str("version", strings.TrimSpace(version.String())).
EmbedObject(p.options).
Msg("Init")
Expand Down
6 changes: 3 additions & 3 deletions cmd/tools/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func generateDocker(path string, kind int) {
opts.grafanaPort,
opts.promPort,
}
err := conf.LoadHarvestConfig(path)
_, err := conf.LoadHarvestConfig(path)
if err != nil {
logErrAndExit(err)
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func silentClose(body io.ReadCloser) {

func generateSystemd(path string) {
var adminService string
err := conf.LoadHarvestConfig(path)
_, err := conf.LoadHarvestConfig(path)
if err != nil {
logErrAndExit(err)
}
Expand Down Expand Up @@ -430,7 +430,7 @@ func generateMetrics(path string) {
zapiClient *zapi.Client
)

err = conf.LoadHarvestConfig(path)
_, err = conf.LoadHarvestConfig(path)
if err != nil {
logErrAndExit(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tools/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func newLabelVar(label string) []byte {

func doImport(_ *cobra.Command, _ []string) {
opts.command = "import"
err := conf.LoadHarvestConfig(opts.config)
_, err := conf.LoadHarvestConfig(opts.config)
if err != nil {
return
}
Expand Down Expand Up @@ -731,7 +731,7 @@ func checkToken(opts *options, ignoreConfig bool, tries int) error {

configPath = opts.config

err = conf.LoadHarvestConfig(configPath)
_, err = conf.LoadHarvestConfig(configPath)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func doShow(_ *cobra.Command, a []string) {
if !c.isValid {
return
}
err := conf.LoadHarvestConfig(args.Config)
_, err := conf.LoadHarvestConfig(args.Config)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/zapi/zapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func doCmd(cmd string) {
connection *client.Client
)

err = conf.LoadHarvestConfig(args.Config)
_, err = conf.LoadHarvestConfig(args.Config)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion integration/test/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var skipDuplicates = map[string]bool{

func TestPollerMetrics(t *testing.T) {
utils.SkipIfMissing(t, utils.Regression)
err := conf.LoadHarvestConfig(installer.HarvestConfigFile)
_, err := conf.LoadHarvestConfig(installer.HarvestConfigFile)
if err != nil {
log.Fatal().Err(err).Msg("Unable to load harvest config")
}
Expand Down
2 changes: 1 addition & 1 deletion integration/test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func WriteToken(token string) {
var err error
filename := "harvest.yml"
abs, _ := filepath.Abs(filename)
err = conf.LoadHarvestConfig(filename)
_, err = conf.LoadHarvestConfig(filename)
PanicIfNotNil(err)
tools := conf.Config.Tools
if tools != nil {
Expand Down
28 changes: 14 additions & 14 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,44 @@ func TestLoadHarvestConfig(configPath string) {
configRead = false
Config = HarvestConfig{}
promPortRangeMapping = make(map[string]PortMap)
err := LoadHarvestConfig(configPath)
_, err := LoadHarvestConfig(configPath)
if err != nil {
log.Fatalf("Failed to load config at=[%s] err=%+v\n", configPath, err)
}
}

func ConfigPath(path string) string {
// Harvest uses the following precedence order. Each item takes precedence over the
// item below it:
// 1. --config command line flag
// 2. HARVEST_CONFIG environment variable
// item below it. All paths are relative to `HARVEST_CONF` environment variable
// 1. `--config` command line flag
// 2. `HARVEST_CONFIG` environment variable
// 3. no command line argument and no environment variable, use the default path (HarvestYML)
if path != HarvestYML && path != "./"+HarvestYML {
return path
return Path(path)
}
fp := os.Getenv("HARVEST_CONFIG")
if fp == "" {
return path
if fp != "" {
path = fp
}
return fp
return Path(path)
}

func LoadHarvestConfig(configPath string) error {
func LoadHarvestConfig(configPath string) (string, error) {
configPath = ConfigPath(configPath)
if configRead {
return nil
return configPath, nil
}
configPath = ConfigPath(configPath)
contents, err := os.ReadFile(configPath)

if err != nil {
return fmt.Errorf("error reading %s err=%w", configPath, err)
return "", fmt.Errorf("error reading %s err=%w", configPath, err)
}
err = DecodeConfig(contents)
if err != nil {
fmt.Printf("error unmarshalling config file=[%s] %+v\n", configPath, err)
return err
return "", err
}
return nil
return configPath, nil
}

func DecodeConfig(contents []byte) error {
Expand Down
20 changes: 20 additions & 0 deletions pkg/conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,23 @@ func TestNodeToPoller(t *testing.T) {
testArg(t, "30s", poller.ClientTimeout)
testArg(t, "true", strconv.FormatBool(*poller.UseInsecureTLS))
}

func TestReadHarvestConfigFromEnv(t *testing.T) {
t.Helper()
configRead = false
Config = HarvestConfig{}
t.Setenv(HomeEnvVar, "testdata")
cp, err := LoadHarvestConfig(HarvestYML)
if err != nil {
t.Errorf("Failed to load config at=[%s] err=%+v\n", HarvestYML, err)
return
}
wantCp := "testdata/harvest.yml"
if cp != wantCp {
t.Errorf("configPath got=%s want=%s", cp, wantCp)
}
poller := Config.Pollers["star"]
if poller == nil {
t.Errorf("check if star poller exists. got=nil want=poller")
}
}
6 changes: 6 additions & 0 deletions pkg/conf/testdata/harvest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Pollers:
star:
addr: localhost
collectors:
- Simple