Skip to content

Commit

Permalink
fix: read templates from HARVEST_CONF when set
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds committed Sep 18, 2023
1 parent bdde464 commit 9212296
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
11 changes: 7 additions & 4 deletions cmd/poller/collector/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/netapp/harvest/v2/cmd/poller/plugin/labelagent"
"github.com/netapp/harvest/v2/cmd/poller/plugin/max"
"github.com/netapp/harvest/v2/cmd/poller/plugin/metricagent"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/tree"
"github.com/netapp/harvest/v2/pkg/tree/node"
Expand All @@ -29,8 +30,9 @@ import (
// ImportTemplate looks for a collector's template by searching confPaths for the first template that exists in
// confPath/collectorName/templateName
func ImportTemplate(confPaths []string, templateName, collectorName string) (*node.Node, error) {
homePath := conf.Path()
for _, confPath := range confPaths {
fp := filepath.Join(confPath, strings.ToLower(collectorName), templateName)
fp := filepath.Join(homePath, confPath, strings.ToLower(collectorName), templateName)
_, err := os.Stat(fp)
if errors.Is(err, os.ErrNotExist) {
continue
Expand Down Expand Up @@ -67,11 +69,12 @@ func (c *AbstractCollector) ImportSubTemplate(model, filename string, ver [3]int
if err != nil {
return nil, "", fmt.Errorf("no best-fit template found due to err=%w", err)
}
homePath := conf.Path()

nextFile:
for _, f := range filenames {
for _, confPath := range c.Options.ConfPaths {
selectedVersion, err = c.findBestFit(confPath, f, model, ontapVersion)
selectedVersion, err = c.findBestFit(homePath, confPath, f, model, ontapVersion)
if err != nil || selectedVersion == "" {
continue
}
Expand Down Expand Up @@ -112,13 +115,13 @@ nextFile:
return finalTemplate, templatePath, err
}

func (c *AbstractCollector) findBestFit(confPath string, name string, model string, ontapVersion *version.Version) (string, error) {
func (c *AbstractCollector) findBestFit(homePath string, confPath string, name string, model string, ontapVersion *version.Version) (string, error) {
var (
selectedVersion string
availableVersions []string
)

pathPrefix := filepath.Join(confPath, strings.ToLower(c.Name), model)
pathPrefix := filepath.Join(homePath, confPath, strings.ToLower(c.Name), model)
c.Logger.Debug().Str("pathPrefix", pathPrefix).Msg("Looking for best-fitting template in pathPrefix")

// check for available versions, these are the subdirectories with matching filenames
Expand Down
14 changes: 14 additions & 0 deletions cmd/poller/collector/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collector

import (
"github.com/hashicorp/go-version"
"github.com/netapp/harvest/v2/pkg/conf"
"sort"
"testing"
)
Expand Down Expand Up @@ -47,3 +48,16 @@ func Test_getClosestIndex(t *testing.T) {
})
}
}

func Test_HARVEST_CONF(t *testing.T) {
t.Setenv(conf.HomeEnvVar, "testdata")
template, err := ImportTemplate([]string{"conf"}, "test.yaml", "test")
if err != nil {
t.Errorf(`got err="%v", want no err`, err)
return
}
name := template.GetChildContentS("collector")
if name != "Test" {
t.Errorf("collectorName got=%s, want=Test", name)
}
}
2 changes: 2 additions & 0 deletions cmd/poller/collector/testdata/conf/test/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

collector: Test
3 changes: 2 additions & 1 deletion pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
HarvestYML = "harvest.yml"
BasicAuth = "basic_auth"
CertificateAuth = "certificate_auth"
HomeEnvVar = "HARVEST_CONF"
)

// TestLoadHarvestConfig is used by testing code to reload a new config
Expand Down Expand Up @@ -172,7 +173,7 @@ func PollerNamed(name string) (*Poller, error) {
// The final path will be relative to the HARVEST_CONF environment variable
// or ./ when the environment variable is not set
func Path(elem ...string) string {
home := os.Getenv("HARVEST_CONF")
home := os.Getenv(HomeEnvVar)
paths := append([]string{home}, elem...)
return filepath.Join(paths...)
}
Expand Down

0 comments on commit 9212296

Please sign in to comment.