Skip to content

Add env variables for make dev target #1069

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ PROTO_DIR := proto
BINARY_NAME := nginx-agent
PROJECT_DIR = cmd/agent
PROJECT_FILE = main.go
COLLECTOR_PATH ?= /etc/nginx-agent/opentelemetry-collector-agent.yaml
MANIFEST_DIR ?= /var/lib/nginx-agent
DIRS = $(BUILD_DIR) $(TEST_BUILD_DIR) $(BUILD_DIR)/$(DOCS_DIR) $(BUILD_DIR)/$(DOCS_DIR)/$(PROTO_DIR)
$(shell mkdir -p $(DIRS))

Expand Down Expand Up @@ -181,7 +183,7 @@ run: build ## Run code

dev: ## Run agent executable
@echo "🚀 Running App"
$(GORUN) -ldflags=$(DEBUG_LDFLAGS) $(PROJECT_DIR)/$(PROJECT_FILE)
NGINX_AGENT_COLLECTOR_CONFIG_PATH=$(COLLECTOR_PATH) NGINX_AGENT_MANIFEST_DIR=$(MANIFEST_DIR) $(GORUN) -ldflags=$(DEBUG_LDFLAGS) $(PROJECT_DIR)/$(PROJECT_FILE)

race-condition-dev: ## Run agent executable with race condition detection
@echo "🏎️ Running app with race condition detection enabled"
Expand Down
7 changes: 6 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func ResolveConfig() (*Config, error) {
Watchers: resolveWatchers(),
Features: viperInstance.GetStringSlice(FeaturesKey),
Labels: resolveLabels(),
ManifestDir: viperInstance.GetString(ManifestDirPathKey),
}

checkCollectorConfiguration(collector, config)
Expand Down Expand Up @@ -231,7 +232,11 @@ func registerFlags() {
"The path to output log messages to. "+
"If the default path doesn't exist, log messages are output to stdout/stderr.",
)

fs.String(
ManifestDirPathKey,
DefManifestDir,
"Specifies the path to the directory containing the manifest files",
)
fs.Duration(
NginxReloadMonitoringPeriodKey,
DefNginxReloadMonitoringPeriod,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (
DefCollectorExtensionsHealthTLSCAPath = ""
DefCollectorExtensionsHealthTLSSkipVerify = false
DefCollectorExtensionsHealthTLServerNameKey = ""

// File defaults
DefManifestDir = "/var/lib/nginx-agent"
)

func DefaultFeatures() []string {
Expand Down
1 change: 1 addition & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
InstanceWatcherMonitoringFrequencyKey = "watchers_instance_watcher_monitoring_frequency"
InstanceHealthWatcherMonitoringFrequencyKey = "watchers_instance_health_watcher_monitoring_frequency"
FileWatcherKey = "watchers_file_watcher"
ManifestDirPathKey = "manifest_dir"
)

var (
Expand Down
1 change: 1 addition & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type (
Version string `yaml:"-"`
Path string `yaml:"-"`
UUID string `yaml:"-"`
ManifestDir string `yaml:"-"`
AllowedDirectories []string `yaml:"allowed_directories" mapstructure:"allowed_directories"`
Features []string `yaml:"features" mapstructure:"features"`
}
Expand Down
17 changes: 7 additions & 10 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ const (
filePerm = 0o600
)

var (
manifestDirPath = "/var/lib/nginx-agent"
manifestFilePath = manifestDirPath + "/manifest.json"
)

type (
fileOperator interface {
Write(ctx context.Context, fileContent []byte, file *mpi.FileMeta) error
Expand Down Expand Up @@ -97,6 +92,7 @@ type FileManagerService struct {
rollbackFileContents map[string][]byte // key is file path
// map of the files currently on disk, used to determine the file action during config apply
currentFilesOnDisk map[string]*mpi.File // key is file path
manifestFilePath string
filesMutex sync.RWMutex
}

Expand All @@ -112,6 +108,7 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
rollbackFileContents: make(map[string][]byte),
currentFilesOnDisk: make(map[string]*mpi.File),
isConnected: isConnected,
manifestFilePath: agentConfig.ManifestDir + "/manifest.json",
}
}

Expand Down Expand Up @@ -853,12 +850,12 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
}

// 0755 allows read/execute for all, write for owner
if err = os.MkdirAll(manifestDirPath, dirPerm); err != nil {
return fmt.Errorf("unable to create directory %s: %w", manifestDirPath, err)
if err = os.MkdirAll(fms.agentConfig.ManifestDir, dirPerm); err != nil {
return fmt.Errorf("unable to create directory %s: %w", fms.agentConfig.ManifestDir, err)
}

// 0600 ensures only root can read/write
newFile, err := os.OpenFile(manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
newFile, err := os.OpenFile(fms.manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
if err != nil {
return fmt.Errorf("failed to read manifest file: %w", err)
}
Expand All @@ -873,11 +870,11 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
}

func (fms *FileManagerService) manifestFile() (map[string]*model.ManifestFile, map[string]*mpi.File, error) {
if _, err := os.Stat(manifestFilePath); err != nil {
if _, err := os.Stat(fms.manifestFilePath); err != nil {
return nil, nil, err
}

file, err := os.ReadFile(manifestFilePath)
file, err := os.ReadFile(fms.manifestFilePath)
if err != nil {
return nil, nil, fmt.Errorf("failed to read manifest file: %w", err)
}
Expand Down
44 changes: 29 additions & 15 deletions internal/file/file_manager_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {

overview := protos.FileOverview(filePath, fileHash)

manifestDirPath = tempDir
manifestFilePath = manifestDirPath + "/manifest.json"
manifestDirPath := tempDir
manifestFilePath := manifestDirPath + "/manifest.json"
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
Expand All @@ -194,7 +194,10 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
}, nil)
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

request := protos.CreateConfigApplyRequest(overview)
writeStatus, err := fileManagerService.ConfigApply(ctx, request)
Expand All @@ -218,10 +221,6 @@ func TestFileManagerService_ConfigApply_Add_LargeFile(t *testing.T) {

overview := protos.FileOverviewLargeFile(filePath, fileHash)

manifestDirPath = tempDir
manifestFilePath = manifestDirPath + "/manifest.json"
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
fakeFileServiceClient.GetOverviewReturns(&mpi.GetOverviewResponse{
Overview: overview,
Expand All @@ -237,10 +236,15 @@ func TestFileManagerService_ConfigApply_Add_LargeFile(t *testing.T) {
fakeServerStreamingClient.chunks[uint32(i)] = []byte{fileContent[i]}
}

manifestDirPath := tempDir
manifestFilePath := manifestDirPath + "/manifest.json"

fakeFileServiceClient.GetFileStreamReturns(fakeServerStreamingClient, nil)
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

request := protos.CreateConfigApplyRequest(overview)
writeStatus, err := fileManagerService.ConfigApply(ctx, request)
Expand Down Expand Up @@ -279,8 +283,8 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
},
}

manifestDirPath = tempDir
manifestFilePath = manifestDirPath + "/manifest.json"
manifestDirPath := tempDir
manifestFilePath := manifestDirPath + "/manifest.json"
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

overview := protos.FileOverview(tempFile.Name(), fileHash)
Expand All @@ -296,12 +300,14 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
}, nil)
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
require.NoError(t, err)

request := protos.CreateConfigApplyRequest(overview)

writeStatus, err := fileManagerService.ConfigApply(ctx, request)
require.NoError(t, err)
assert.Equal(t, model.OK, writeStatus)
Expand Down Expand Up @@ -336,14 +342,17 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
},
}

manifestDirPath = tempDir
manifestFilePath = manifestDirPath + "/manifest.json"
manifestDirPath := tempDir
manifestFilePath := manifestDirPath + "/manifest.json"
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
require.NoError(t, err)

Expand Down Expand Up @@ -462,8 +471,8 @@ func TestFileManagerService_Rollback(t *testing.T) {
_, writeErr = updateFile.Write(newFileContent)
require.NoError(t, writeErr)

manifestDirPath = tempDir
manifestFilePath = manifestDirPath + "/manifest.json"
manifestDirPath := tempDir
manifestFilePath := manifestDirPath + "/manifest.json"
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

filesCache := map[string]*model.FileCache{
Expand Down Expand Up @@ -529,6 +538,8 @@ func TestFileManagerService_Rollback(t *testing.T) {
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
fileManagerService.rollbackFileContents = fileContentCache
fileManagerService.fileActions = filesCache
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

err := fileManagerService.Rollback(ctx, instanceID)
require.NoError(t, err)
Expand Down Expand Up @@ -690,11 +701,14 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
// Delete manifest file if it already exists
manifestFile := CreateTestManifestFile(t, tempDir, test.currentFiles)
defer manifestFile.Close()
manifestDirPath = tempDir
manifestFilePath = manifestFile.Name()
manifestDirPath := tempDir
manifestFilePath := manifestFile.Name()

fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
fileManagerService.agentConfig.ManifestDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

require.NoError(tt, err)

diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,
Expand Down
Loading