From 597e427642f2501811d742b5816f24edd205dc36 Mon Sep 17 00:00:00 2001 From: Chris Grindstaff Date: Mon, 18 Sep 2023 09:02:22 -0400 Subject: [PATCH] fix: read templates from `HARVEST_CONF` when set --- cmd/poller/collector/helpers.go | 11 +++++++---- cmd/poller/collector/helpers_test.go | 14 ++++++++++++++ cmd/poller/collector/testdata/conf/test/test.yaml | 2 ++ pkg/conf/conf.go | 3 ++- 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 cmd/poller/collector/testdata/conf/test/test.yaml diff --git a/cmd/poller/collector/helpers.go b/cmd/poller/collector/helpers.go index f3524afc0..7e8dc6609 100644 --- a/cmd/poller/collector/helpers.go +++ b/cmd/poller/collector/helpers.go @@ -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" @@ -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 @@ -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 } @@ -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 diff --git a/cmd/poller/collector/helpers_test.go b/cmd/poller/collector/helpers_test.go index 9a8aee592..4b0a7897e 100644 --- a/cmd/poller/collector/helpers_test.go +++ b/cmd/poller/collector/helpers_test.go @@ -2,6 +2,7 @@ package collector import ( "github.com/hashicorp/go-version" + "github.com/netapp/harvest/v2/pkg/conf" "sort" "testing" ) @@ -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) + } +} diff --git a/cmd/poller/collector/testdata/conf/test/test.yaml b/cmd/poller/collector/testdata/conf/test/test.yaml new file mode 100644 index 000000000..68a3e6ee1 --- /dev/null +++ b/cmd/poller/collector/testdata/conf/test/test.yaml @@ -0,0 +1,2 @@ + +collector: Test diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index ae3ee2d9e..f44068822 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -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 @@ -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...) }