Skip to content

Commit

Permalink
Add sdk temp dir (NR-219553) (#1808)
Browse files Browse the repository at this point in the history
* Add passthrough of Integrations tempDir folder
* Add to tmpDir to new manager and executor
* Add tmpDir to integrations enabled by FFlag
  • Loading branch information
alvarocabanas committed Feb 16, 2024
1 parent d05f4eb commit 359a7c8
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cmd/newrelic-infra/newrelic-infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func initializeAgentAndRun(c *config.Config, logFwCfg config.LogForward) error {

v4ManagerConfig := v4.NewManagerConfig(
c.Log.VerboseEnabled(),
c.DefaultIntegrationsTempDir,
c.Features,
c.PassthroughEnvironment,
c.PluginInstanceDirs,
Expand Down Expand Up @@ -526,6 +527,7 @@ func newInstancesLookup(cfg v4.ManagerConfig) integration.InstancesLookup {
legacyDefinedCommands := v3legacy.NewDefinitionsRepo(v3legacy.LegacyConfig{
DefinitionFolders: cfg.DefinitionFolders,
Verbose: cfg.Verbose,
TempDir: cfg.TempDir,
})
return integration.InstancesLookup{
Legacy: legacyDefinedCommands.NewDefinitionCommand,
Expand Down Expand Up @@ -705,6 +707,7 @@ func executeIntegrationsDryRunMode(configPath string, ac *config.Config) {

v4ManagerConfig := v4.NewManagerConfig(
ac.Log.VerboseEnabled(),
ac.DefaultIntegrationsTempDir,
ac.Features,
ac.PassthroughEnvironment,
integrationConfigPaths,
Expand Down
1 change: 1 addition & 0 deletions internal/integrations/v4/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ package constants

const EnableVerbose = "enable_verbose"
const HostID = "host_id"
const TmpDir = "tmp_dir"
6 changes: 6 additions & 0 deletions internal/integrations/v4/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func (r *Executor) buildCommand(ctx context.Context) *exec.Cmd {
cmd.Env = append(cmd.Env, "NRI_HOST_ID="+hostID)
}

tmpDir, ok := ctx.Value(constants.TmpDir).(string)

if ok && tmpDir != "" {
cmd.Env = append(cmd.Env, "TEMP_DIR="+tmpDir)
}

cmd.Dir = r.Cfg.Directory
return cmd
}
Expand Down
3 changes: 2 additions & 1 deletion internal/integrations/v4/v3legacy/definitions_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DefinitionContext struct {
type LegacyConfig struct {
DefinitionFolders []string
Verbose int
TempDir string
}

// DefinitionsRepo stores all the definitions from all the files
Expand Down Expand Up @@ -125,7 +126,7 @@ func (dcb *DefinitionsRepo) NewDefinitionCommand(dcc integration.DefinitionComma
// Legacy integrations don't allow setting the environment, as it will be used
// for passing the "arguments" entry.
dcc.Common.ExecutorConfig.Environment =
legacy.ArgumentsToEnvVars(dcb.Config.Verbose, dcc.Arguments)
legacy.ArgumentsToEnvVars(dcb.Config.Verbose, dcb.Config.TempDir, dcc.Arguments)

// We need to set the Working Directory to the folder where the definition file is placed
dcc.Common.ExecutorConfig.Directory = definition.Dir
Expand Down
5 changes: 3 additions & 2 deletions pkg/integrations/legacy/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@ func (ep *externalPlugin) detailedLogFields() logrus.Fields {
// ArgumentsToEnvVars returns the environment variables that will be passed to the
// external plugin command. This implies that the plugin arguments are
// passed as environment variables to the integrations.
func ArgumentsToEnvVars(verbose int, arguments map[string]string) map[string]string {
func ArgumentsToEnvVars(verbose int, tempDir string, arguments map[string]string) map[string]string {
envVars := make(map[string]string)
envVars["VERBOSE"] = fmt.Sprintf("%v", verbose)
envVars["TEMP_DIR"] = fmt.Sprintf("%s", tempDir)

Check failure on line 378 in pkg/integrations/legacy/runner.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)

Check failure on line 378 in pkg/integrations/legacy/runner.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)

Check failure on line 378 in pkg/integrations/legacy/runner.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)

// Pass the integration arguments as environment variables to the command
for k, v := range arguments {
Expand Down Expand Up @@ -430,7 +431,7 @@ func (ep *externalPlugin) appendEnvPassthrough(envVars map[string]string) {
// `PassthroughEnvironment`, the value from the environment takes precedence.
func (ep *externalPlugin) envVars() map[string]string {
cfg := ep.Context.Config()
envVars := ArgumentsToEnvVars(cfg.Log.VerboseEnabled(), ep.pluginInstance.Arguments)
envVars := ArgumentsToEnvVars(cfg.Log.VerboseEnabled(), cfg.DefaultIntegrationsTempDir, ep.pluginInstance.Arguments)
ep.appendEnvPassthrough(envVars)
return envVars
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/integrations/legacy/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/newrelic/infrastructure-agent/internal/agent/types"
"io"
"io/ioutil"
"os"
Expand All @@ -20,6 +19,8 @@ import (
"sync"
"time"

"github.com/newrelic/infrastructure-agent/internal/agent/types"

Check failure on line 22 in pkg/integrations/legacy/runner_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

import 'github.com/newrelic/infrastructure-agent/internal/agent/types' is not allowed from list 'Main' (depguard)

Check failure on line 22 in pkg/integrations/legacy/runner_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

import 'github.com/newrelic/infrastructure-agent/internal/agent/types' is not allowed from list 'Main' (depguard)

Check failure on line 22 in pkg/integrations/legacy/runner_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

import 'github.com/newrelic/infrastructure-agent/internal/agent/types' is not allowed from list 'Main' (depguard)

"github.com/newrelic/infrastructure-agent/pkg/databind/pkg/data"
"github.com/newrelic/infrastructure-agent/pkg/databind/pkg/databind"
"github.com/newrelic/infrastructure-agent/pkg/entity/host"
Expand Down Expand Up @@ -516,6 +517,7 @@ func newFakePlugin(ctx customContext, pluginVersion int) externalPlugin {
Arguments: map[string]string{
"PATH": "Path should be replaced by path env var",
"CUSTOM_ARG": "testValue",
"TEMP_DIR": "a/path",
},
},
pluginRunner: &PluginRunner{
Expand Down Expand Up @@ -1230,6 +1232,7 @@ func TestGenerateExecCmdWithDatabind(t *testing.T) {
Environment: map[string]string{
"PATH": os.Getenv("PATH"), // The config has a PATH set as well, but the env var should take precedence.
"VERBOSE": "0",
"TEMP_DIR": "a/path",
"CUSTOM_ARG": "testValue",
"ComSpec": os.Getenv("ComSpec"),
"SystemRoot": os.Getenv("SystemRoot"),
Expand Down Expand Up @@ -1262,6 +1265,7 @@ func TestGenerateExecCmdWithDatabind(t *testing.T) {
}
assert.Equal(t, os.Getenv("PATH"), envVarMap["PATH"]) // The config has a PATH set as well, but the env var should take precedence
assert.Equal(t, "0", envVarMap["VERBOSE"]) // Should be set based on config
assert.Equal(t, "a/path", envVarMap["TEMP_DIR"]) // Should be set based on config
assert.Equal(t, "testValue", envVarMap["CUSTOM_ARG"]) // Should match the config on the plugin instance

// In config.go, these environment variables are also configured to pass through
Expand Down Expand Up @@ -2182,6 +2186,7 @@ func TestLogFields(t *testing.T) {

envVars := map[string]string{
"VERBOSE": "0",
"TEMP_DIR": "a/path",
"PATH": pathEnv,
"CUSTOM_ARG": "testValue",
}
Expand All @@ -2199,6 +2204,7 @@ func TestLogFields(t *testing.T) {
assert.Equal(t, fields["arguments"], map[string]string{
"PATH": "Path should be replaced by path env var",
"CUSTOM_ARG": "testValue",
"TEMP_DIR": "a/path",
})
assert.Equal(t, fields["labels"], map[string]string{
"role": "fileserver",
Expand Down Expand Up @@ -2232,6 +2238,7 @@ func TestLogFields(t *testing.T) {
"windir",
//We add the ones defined above
"VERBOSE",
"TEMP_DIR",
"CUSTOM_ARG",
}
//We check that env vars are present as we cannot assert the content
Expand Down
17 changes: 14 additions & 3 deletions pkg/integrations/v4/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ package v4
import (
"context"
"errors"
"github.com/newrelic/infrastructure-agent/pkg/entity/host"
"os"
"path/filepath"
"strings"
"sync"

"github.com/newrelic/infrastructure-agent/internal/integrations/v4/constants"
"github.com/newrelic/infrastructure-agent/pkg/entity/host"

Check failure on line 15 in pkg/integrations/v4/manager.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

import 'github.com/newrelic/infrastructure-agent/pkg/entity/host' is not allowed from list 'Main' (depguard)

Check failure on line 15 in pkg/integrations/v4/manager.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

import 'github.com/newrelic/infrastructure-agent/pkg/entity/host' is not allowed from list 'Main' (depguard)

Check failure on line 15 in pkg/integrations/v4/manager.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

import 'github.com/newrelic/infrastructure-agent/pkg/entity/host' is not allowed from list 'Main' (depguard)
"github.com/newrelic/infrastructure-agent/pkg/integrations/cmdrequest"
"github.com/newrelic/infrastructure-agent/pkg/integrations/configrequest"
"github.com/newrelic/infrastructure-agent/pkg/integrations/track"
v4Config "github.com/newrelic/infrastructure-agent/pkg/integrations/v4/config"

"github.com/newrelic/infrastructure-agent/pkg/integrations/v4/fs"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -157,16 +156,19 @@ type ManagerConfig struct {
DefinitionFolders []string
// Defines verbosity level in v3 legacy integrations
Verbose int
// Defines TempDir folder in v3 legacy integrations
TempDir string
// PassthroughEnvironment holds a copy of its homonym in config.Config.
PassthroughEnvironment []string
}

func NewManagerConfig(verbose int, features map[string]bool, passthroughEnvs, configFolders, definitionFolders []string) ManagerConfig {
func NewManagerConfig(verbose int, tempDir string, features map[string]bool, passthroughEnvs, configFolders, definitionFolders []string) ManagerConfig {
return ManagerConfig{
ConfigPaths: configFolders,
AgentFeatures: features,
DefinitionFolders: definitionFolders,
Verbose: verbose,
TempDir: tempDir,
PassthroughEnvironment: passthroughEnvs,
}
}
Expand Down Expand Up @@ -251,6 +253,8 @@ func NewManager(

// Start in background the v4 integrations lifecycle management, including hot reloading, interval and timeout management
func (mgr *Manager) Start(ctx context.Context) {
ctx = contextWithTmpDir(ctx, mgr.managerConfig.TempDir)

for path, rc := range mgr.runners.List() {
illog.WithField("file", path).Debug("Starting integrations group.")
rc.start(contextWithVerbose(ctx, mgr.managerConfig.Verbose))
Expand All @@ -269,6 +273,7 @@ func (mgr *Manager) RunOnce(ctx context.Context) {

wg.Add(1)
go func(g *groupContext) {
ctx = contextWithTmpDir(ctx, mgr.managerConfig.TempDir)
g.runOnce(contextWithVerbose(ctx, mgr.managerConfig.Verbose))
wg.Done()
}(group)
Expand All @@ -292,6 +297,8 @@ func (mgr *Manager) EnableOHIFromFF(ctx context.Context, featureFlag string) err
Enabled: true,
}

ctx = contextWithTmpDir(ctx, mgr.managerConfig.TempDir)

mgr.runIntegrationFromPath(ctx, cfgPath, false, &illog, &cmdFF)

return nil
Expand Down Expand Up @@ -522,3 +529,7 @@ func foundFilesLogFields(configs v4Config.YAMLMap) func() logrus.Fields {
func contextWithVerbose(ctx context.Context, verbose int) context.Context {
return context.WithValue(ctx, constants.EnableVerbose, verbose)
}

func contextWithTmpDir(ctx context.Context, tmpDir string) context.Context {
return context.WithValue(ctx, constants.TmpDir, tmpDir)
}
62 changes: 62 additions & 0 deletions pkg/integrations/v4/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,68 @@ func TestManager_StartWithVerboseFalse(t *testing.T) {
})
}

func TestManager_StartWithTempDir(t *testing.T) {

Check failure on line 991 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

Function TestManager_StartWithTempDir missing the call to method parallel (paralleltest)

Check failure on line 991 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

Function TestManager_StartWithTempDir missing the call to method parallel (paralleltest)

Check failure on line 991 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

Function TestManager_StartWithTempDir missing the call to method parallel (paralleltest)
// GIVEN a configuration file for an OHI with feature in it
dir, err := tempFiles(map[string]string{
"foo.yaml": getV4VerboseCheckYAML(t),
})
require.NoError(t, err)
defer removeTempFiles(t, dir)

Check failure on line 997 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

only one cuddle assignment allowed before defer statement (wsl)

Check failure on line 997 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

only one cuddle assignment allowed before defer statement (wsl)

Check failure on line 997 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

only one cuddle assignment allowed before defer statement (wsl)

// AND an integrations manager and with feature enabled within agent config
emitter := &testemit.RecordEmitter{}
mgr := NewManager(ManagerConfig{

Check failure on line 1001 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose, TempDir (exhaustruct)

Check failure on line 1001 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose, TempDir (exhaustruct)

Check failure on line 1001 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose, TempDir (exhaustruct)
ConfigPaths: []string{dir},
PassthroughEnvironment: passthroughEnv,
}, config.NewPathLoader(), emitter, integration.ErrLookup, definitionQ, configEntryQ, track.NewTracker(nil), host.IDLookup{})

// AND the manager starts
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go mgr.Start(ctx)

Check failure on line 1009 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

only one cuddle assignment allowed before go statement (wsl)

Check failure on line 1009 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

only one cuddle assignment allowed before go statement (wsl)

Check failure on line 1009 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

only one cuddle assignment allowed before go statement (wsl)

d := getEmittedData(t, emitter, "verbose-check")
assert.Contains(t, d.DataSet.Metrics, protocol.MetricData{
"value": "true",
"event_type": "THIS_IS_A_TEST",
})
assert.NotContains(t, d.DataSet.Metrics, protocol.MetricData{
"event_type": "TEMP_DIR",
})
}

func TestManager_StartWithoutTempDir(t *testing.T) {
// GIVEN a configuration file for an OHI with feature in it
dir, err := tempFiles(map[string]string{
"foo.yaml": getV4VerboseCheckYAML(t),
})
require.NoError(t, err)
defer removeTempFiles(t, dir)

Check failure on line 1027 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

only one cuddle assignment allowed before defer statement (wsl)

Check failure on line 1027 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

only one cuddle assignment allowed before defer statement (wsl)

Check failure on line 1027 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

only one cuddle assignment allowed before defer statement (wsl)

// AND an integrations manager and with feature enabled within agent config
emitter := &testemit.RecordEmitter{}
mgr := NewManager(ManagerConfig{

Check failure on line 1031 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose (exhaustruct)

Check failure on line 1031 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose (exhaustruct)

Check failure on line 1031 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

v4.ManagerConfig is missing fields AgentFeatures, DefinitionFolders, Verbose (exhaustruct)
ConfigPaths: []string{dir},
PassthroughEnvironment: passthroughEnv,
TempDir: "/my-custom/path",
}, config.NewPathLoader(), emitter, integration.ErrLookup, definitionQ, configEntryQ, track.NewTracker(nil), host.IDLookup{})

// AND the manager starts
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go mgr.Start(ctx)

Check failure on line 1040 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

only one cuddle assignment allowed before go statement (wsl)

Check failure on line 1040 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

only one cuddle assignment allowed before go statement (wsl)

Check failure on line 1040 in pkg/integrations/v4/manager_test.go

View workflow job for this annotation

GitHub Actions / linter-windows / Lint tests

only one cuddle assignment allowed before go statement (wsl)

d := getEmittedData(t, emitter, "verbose-check")
assert.Contains(t, d.DataSet.Metrics, protocol.MetricData{
"value": "true",
"event_type": "THIS_IS_A_TEST",
})
assert.Contains(t, d.DataSet.Metrics, protocol.MetricData{
"value": "/my-custom/path",
"event_type": "TEMP_DIR",
})
}

func getV4VerboseCheckYAML(t *testing.T) string {
// GOTMPDIR: %s
// GOCACHE: %s
Expand Down
2 changes: 2 additions & 0 deletions test/cfgprotocol/agent/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func New(configsDir, tempBinDir string) *Emulator {
cfg := ag.Context.Config()
integrationCfg := v4.NewManagerConfig(
cfg.Log.VerboseEnabled(),
cfg.DefaultIntegrationsTempDir,
cfg.Features,
cfg.PassthroughEnvironment,
[]string{configsDir},
Expand Down Expand Up @@ -155,6 +156,7 @@ func newInstancesLookup(cfg v4.ManagerConfig) integration.InstancesLookup {
legacyDefinedCommands := v3legacy.NewDefinitionsRepo(v3legacy.LegacyConfig{
DefinitionFolders: cfg.DefinitionFolders,
Verbose: cfg.Verbose,
TempDir: cfg.TempDir,
})
return integration.InstancesLookup{
Legacy: legacyDefinedCommands.NewDefinitionCommand,
Expand Down

0 comments on commit 359a7c8

Please sign in to comment.