Skip to content

Commit 9f24720

Browse files
john-david3dhurley
andauthored
Add env variables for make dev target (#1069)
* Add env variables for make dev target * Update config struct * Fix manifest var names --------- Co-authored-by: Donal Hurley <d.hurley@f5.com>
1 parent 850e0da commit 9f24720

File tree

7 files changed

+50
-27
lines changed

7 files changed

+50
-27
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ PROTO_DIR := proto
4343
BINARY_NAME := nginx-agent
4444
PROJECT_DIR = cmd/agent
4545
PROJECT_FILE = main.go
46+
COLLECTOR_PATH ?= /etc/nginx-agent/opentelemetry-collector-agent.yaml
47+
MANIFEST_DIR ?= /var/lib/nginx-agent
4648
DIRS = $(BUILD_DIR) $(TEST_BUILD_DIR) $(BUILD_DIR)/$(DOCS_DIR) $(BUILD_DIR)/$(DOCS_DIR)/$(PROTO_DIR)
4749
$(shell mkdir -p $(DIRS))
4850

@@ -181,7 +183,7 @@ run: build ## Run code
181183

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

186188
race-condition-dev: ## Run agent executable with race condition detection
187189
@echo "🏎️ Running app with race condition detection enabled"

internal/config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func ResolveConfig() (*Config, error) {
124124
Watchers: resolveWatchers(),
125125
Features: viperInstance.GetStringSlice(FeaturesKey),
126126
Labels: resolveLabels(),
127+
ManifestDir: viperInstance.GetString(ManifestDirPathKey),
127128
}
128129

129130
checkCollectorConfiguration(collector, config)
@@ -231,7 +232,11 @@ func registerFlags() {
231232
"The path to output log messages to. "+
232233
"If the default path doesn't exist, log messages are output to stdout/stderr.",
233234
)
234-
235+
fs.String(
236+
ManifestDirPathKey,
237+
DefManifestDir,
238+
"Specifies the path to the directory containing the manifest files",
239+
)
235240
fs.Duration(
236241
NginxReloadMonitoringPeriodKey,
237242
DefNginxReloadMonitoringPeriod,

internal/config/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const (
7474
DefCollectorExtensionsHealthTLSCAPath = ""
7575
DefCollectorExtensionsHealthTLSSkipVerify = false
7676
DefCollectorExtensionsHealthTLServerNameKey = ""
77+
78+
// File defaults
79+
DefManifestDir = "/var/lib/nginx-agent"
7780
)
7881

7982
func DefaultFeatures() []string {

internal/config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
InstanceWatcherMonitoringFrequencyKey = "watchers_instance_watcher_monitoring_frequency"
2424
InstanceHealthWatcherMonitoringFrequencyKey = "watchers_instance_health_watcher_monitoring_frequency"
2525
FileWatcherKey = "watchers_file_watcher"
26+
ManifestDirPathKey = "manifest_dir"
2627
)
2728

2829
var (

internal/config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type (
4242
Version string `yaml:"-"`
4343
Path string `yaml:"-"`
4444
UUID string `yaml:"-"`
45+
ManifestDir string `yaml:"-"`
4546
AllowedDirectories []string `yaml:"allowed_directories" mapstructure:"allowed_directories"`
4647
Features []string `yaml:"features" mapstructure:"features"`
4748
}

internal/file/file_manager_service.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ const (
4848
filePerm = 0o600
4949
)
5050

51-
var (
52-
manifestDirPath = "/var/lib/nginx-agent"
53-
manifestFilePath = manifestDirPath + "/manifest.json"
54-
)
55-
5651
type (
5752
fileOperator interface {
5853
Write(ctx context.Context, fileContent []byte, file *mpi.FileMeta) error
@@ -98,6 +93,7 @@ type FileManagerService struct {
9893
// map of the files currently on disk, used to determine the file action during config apply
9994
currentFilesOnDisk map[string]*mpi.File // key is file path
10095
previousManifestFiles map[string]*model.ManifestFile
96+
manifestFilePath string
10197
filesMutex sync.RWMutex
10298
}
10399

@@ -113,6 +109,7 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
113109
rollbackFileContents: make(map[string][]byte),
114110
currentFilesOnDisk: make(map[string]*mpi.File),
115111
previousManifestFiles: make(map[string]*model.ManifestFile),
112+
manifestFilePath: agentConfig.ManifestDir + "/manifest.json",
116113
isConnected: isConnected,
117114
}
118115
}
@@ -851,12 +848,12 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
851848
}
852849

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

858855
// 0600 ensures only root can read/write
859-
newFile, err := os.OpenFile(manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
856+
newFile, err := os.OpenFile(fms.manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
860857
if err != nil {
861858
return fmt.Errorf("failed to read manifest file: %w", err)
862859
}
@@ -875,11 +872,11 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
875872
}
876873

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

882-
file, err := os.ReadFile(manifestFilePath)
879+
file, err := os.ReadFile(fms.manifestFilePath)
883880
if err != nil {
884881
return nil, nil, fmt.Errorf("failed to read manifest file: %w", err)
885882
}

internal/file/file_manager_service_test.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
179179

180180
overview := protos.FileOverview(filePath, fileHash)
181181

182-
manifestDirPath = tempDir
183-
manifestFilePath = manifestDirPath + "/manifest.json"
182+
manifestDirPath := tempDir
183+
manifestFilePath := manifestDirPath + "/manifest.json"
184184
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")
185185

186186
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
@@ -194,7 +194,10 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
194194
}, nil)
195195
agentConfig := types.AgentConfig()
196196
agentConfig.AllowedDirectories = []string{tempDir}
197+
197198
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
199+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
200+
fileManagerService.manifestFilePath = manifestFilePath
198201

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

219222
overview := protos.FileOverviewLargeFile(filePath, fileHash)
220223

221-
manifestDirPath = tempDir
222-
manifestFilePath = manifestDirPath + "/manifest.json"
223-
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")
224-
225224
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
226225
fakeFileServiceClient.GetOverviewReturns(&mpi.GetOverviewResponse{
227226
Overview: overview,
@@ -237,10 +236,15 @@ func TestFileManagerService_ConfigApply_Add_LargeFile(t *testing.T) {
237236
fakeServerStreamingClient.chunks[uint32(i)] = []byte{fileContent[i]}
238237
}
239238

239+
manifestDirPath := tempDir
240+
manifestFilePath := manifestDirPath + "/manifest.json"
241+
240242
fakeFileServiceClient.GetFileStreamReturns(fakeServerStreamingClient, nil)
241243
agentConfig := types.AgentConfig()
242244
agentConfig.AllowedDirectories = []string{tempDir}
243245
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
246+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
247+
fileManagerService.manifestFilePath = manifestFilePath
244248

245249
request := protos.CreateConfigApplyRequest(overview)
246250
writeStatus, err := fileManagerService.ConfigApply(ctx, request)
@@ -279,8 +283,8 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
279283
},
280284
}
281285

282-
manifestDirPath = tempDir
283-
manifestFilePath = manifestDirPath + "/manifest.json"
286+
manifestDirPath := tempDir
287+
manifestFilePath := manifestDirPath + "/manifest.json"
284288
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")
285289

286290
overview := protos.FileOverview(tempFile.Name(), fileHash)
@@ -296,12 +300,14 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
296300
}, nil)
297301
agentConfig := types.AgentConfig()
298302
agentConfig.AllowedDirectories = []string{tempDir}
303+
299304
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
305+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
306+
fileManagerService.manifestFilePath = manifestFilePath
300307
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
301308
require.NoError(t, err)
302309

303310
request := protos.CreateConfigApplyRequest(overview)
304-
305311
writeStatus, err := fileManagerService.ConfigApply(ctx, request)
306312
require.NoError(t, err)
307313
assert.Equal(t, model.OK, writeStatus)
@@ -336,14 +342,17 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
336342
},
337343
}
338344

339-
manifestDirPath = tempDir
340-
manifestFilePath = manifestDirPath + "/manifest.json"
345+
manifestDirPath := tempDir
346+
manifestFilePath := manifestDirPath + "/manifest.json"
341347
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")
342348

343349
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
344350
agentConfig := types.AgentConfig()
345351
agentConfig.AllowedDirectories = []string{tempDir}
352+
346353
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
354+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
355+
fileManagerService.manifestFilePath = manifestFilePath
347356
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
348357
require.NoError(t, err)
349358

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

465-
manifestDirPath = tempDir
466-
manifestFilePath = manifestDirPath + "/manifest.json"
474+
manifestDirPath := tempDir
475+
manifestFilePath := manifestDirPath + "/manifest.json"
467476
helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")
468477

469478
filesCache := map[string]*model.FileCache{
@@ -529,6 +538,8 @@ func TestFileManagerService_Rollback(t *testing.T) {
529538
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
530539
fileManagerService.rollbackFileContents = fileContentCache
531540
fileManagerService.fileActions = filesCache
541+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
542+
fileManagerService.manifestFilePath = manifestFilePath
532543

533544
err := fileManagerService.Rollback(ctx, instanceID)
534545
require.NoError(t, err)
@@ -690,11 +701,14 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
690701
// Delete manifest file if it already exists
691702
manifestFile := CreateTestManifestFile(t, tempDir, test.currentFiles)
692703
defer manifestFile.Close()
693-
manifestDirPath = tempDir
694-
manifestFilePath = manifestFile.Name()
704+
manifestDirPath := tempDir
705+
manifestFilePath := manifestFile.Name()
695706

696707
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
697708
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
709+
fileManagerService.agentConfig.ManifestDir = manifestDirPath
710+
fileManagerService.manifestFilePath = manifestFilePath
711+
698712
require.NoError(tt, err)
699713

700714
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,

0 commit comments

Comments
 (0)