Skip to content
Merged
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
12 changes: 6 additions & 6 deletions api/grpc/mpi/v1/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions api/grpc/mpi/v1/files_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions internal/resource/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type ResourceService struct {
agentConfig *config.Config
instanceOperators map[string]instanceOperator // key is instance ID
info host.InfoInterface
manifestFilePath string
resourceMutex sync.Mutex
operatorsMutex sync.Mutex
}
Expand All @@ -108,6 +109,7 @@ func NewResourceService(ctx context.Context, agentConfig *config.Config) *Resour
instanceOperators: make(map[string]instanceOperator),
nginxConfigParser: parser.NewNginxConfigParser(agentConfig),
agentConfig: agentConfig,
manifestFilePath: agentConfig.LibDir + "/manifest.json",
}

resourceService.updateResourceInfo(ctx)
Expand Down Expand Up @@ -216,6 +218,8 @@ func (r *ResourceService) ApplyConfig(ctx context.Context, instanceID string) (*
return nil, fmt.Errorf("failed to parse config %w", parseErr)
}

nginxConfigContext = r.updateConfigContextFiles(ctx, nginxConfigContext)

datasource.UpdateNginxInstanceRuntime(instance, nginxConfigContext)

slog.DebugContext(ctx, "Updated Instance Runtime after parsing config", "instance", instance.GetInstanceRuntime())
Expand Down Expand Up @@ -332,6 +336,51 @@ func (r *ResourceService) UpdateHTTPUpstreamServers(ctx context.Context, instanc
return added, updated, deleted, createPlusAPIError(updateError)
}

func (r *ResourceService) updateConfigContextFiles(ctx context.Context,
nginxConfigContext *model.NginxConfigContext,
) *model.NginxConfigContext {
manifestFiles, manifestErr := r.manifestFile()
if manifestErr != nil {
slog.ErrorContext(ctx, "Error getting manifest files", "error", manifestErr)
}

for _, manifestFile := range manifestFiles {
if manifestFile.ManifestFileMeta.Unmanaged {
for _, configFile := range nginxConfigContext.Files {
if configFile.GetFileMeta().GetName() == manifestFile.ManifestFileMeta.Name {
configFile.Unmanaged = true
}
}
}
}

return nginxConfigContext
}

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

file, err := os.ReadFile(r.manifestFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read manifest file: %w", err)
}

var manifestFiles map[string]*model.ManifestFile

err = json.Unmarshal(file, &manifestFiles)
if err != nil {
if len(file) == 0 {
return nil, fmt.Errorf("manifest file is empty: %w", err)
}

return nil, fmt.Errorf("failed to parse manifest file: %w", err)
}

return manifestFiles, nil
}

func convertToUpstreamServer(upstreams []*structpb.Struct) []client.UpstreamServer {
var servers []client.UpstreamServer
res, err := json.Marshal(upstreams)
Expand Down
85 changes: 81 additions & 4 deletions internal/resource/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ package resource

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/nginx/agent/v3/pkg/host/hostfakes"

"github.com/nginx/agent/v3/internal/datasource/config/configfakes"
"github.com/nginx/agent/v3/internal/model"
"github.com/nginx/agent/v3/pkg/host/hostfakes"
"github.com/nginx/agent/v3/test/helpers"
"github.com/nginx/nginx-plus-go-client/v3/client"

"google.golang.org/protobuf/types/known/structpb"

"github.com/nginx/agent/v3/internal/resource/resourcefakes"
Expand Down Expand Up @@ -399,12 +399,89 @@ func TestResourceService_ApplyConfig(t *testing.T) {
}
resourceService.resource.Instances = instances

_, reloadError := resourceService.ApplyConfig(ctx, test.instanceID)
configContext, reloadError := resourceService.ApplyConfig(ctx, test.instanceID)
assert.Equal(t, test.expected, reloadError)
t.Log("configContext:", configContext)
})
}
}

func Test_updateConfigContextFiles(t *testing.T) {
ctx := t.Context()
resourceService := NewResourceService(ctx, types.AgentConfig())

manifestFileContents := map[string]*model.ManifestFile{
"/etc/nginx/nginx.conf": {
ManifestFileMeta: &model.ManifestFileMeta{
Name: "/etc/nginx/nginx.conf",
Referenced: true,
Unmanaged: false,
},
},
"/etc/nginx/unmanaged.conf": {
ManifestFileMeta: &model.ManifestFileMeta{
Name: "/etc/nginx/unmanaged.conf",
Unmanaged: true,
},
},
}

tempDir := t.TempDir()
manifestDirPath := tempDir
manifestFile := helpers.CreateFileWithErrorCheck(t, manifestDirPath, "manifest.json")

resourceService.manifestFilePath = manifestFile.Name()
manifestJSON, err := json.MarshalIndent(manifestFileContents, "", " ")
require.NoError(t, err)

_, err = manifestFile.Write(manifestJSON)
require.NoError(t, err)

nginxConfigContext := &model.NginxConfigContext{
Files: []*mpi.File{
{
FileMeta: &mpi.FileMeta{
Name: "/etc/nginx/unmanaged.conf",
Hash: "",
FileType: nil,
},
Unmanaged: false,
},
{
FileMeta: &mpi.FileMeta{
Name: "/etc/nginx/nginx.conf",
Hash: "",
FileType: nil,
},
Unmanaged: false,
},
},
}

expected := &model.NginxConfigContext{
Files: []*mpi.File{
{
FileMeta: &mpi.FileMeta{
Name: "/etc/nginx/unmanaged.conf",
Hash: "",
FileType: nil,
},
Unmanaged: true,
},
{
FileMeta: &mpi.FileMeta{
Name: "/etc/nginx/nginx.conf",
Hash: "",
FileType: nil,
},
Unmanaged: false,
},
},
}
configContext := resourceService.updateConfigContextFiles(ctx, nginxConfigContext)
assert.Equal(t, expected, configContext)
}

func Test_convertToUpstreamServer(t *testing.T) {
expectedMax := 2
expectedFails := 0
Expand Down
7 changes: 6 additions & 1 deletion internal/watcher/file/file_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewFileWatcherService(agentConfig *config.Config) *FileWatcherService {
}
}

//nolint:revive // cant simplify due to for loop
func (fws *FileWatcherService) Watch(ctx context.Context, ch chan<- FileUpdateMessage) {
monitoringFrequency := fws.agentConfig.Watchers.FileWatcher.MonitoringFrequency
slog.DebugContext(ctx, "Starting file watcher monitoring", "monitoring_frequency", monitoringFrequency)
Expand All @@ -83,7 +84,11 @@ func (fws *FileWatcherService) Watch(ctx context.Context, ch chan<- FileUpdateMe

return
case <-instanceWatcherTicker.C:
fws.checkForUpdates(ctx, ch)
if fws.enabled.Load() {
fws.checkForUpdates(ctx, ch)
} else {
slog.DebugContext(ctx, "Skipping check for file updates, file watcher is disabled")
}
}

if fws.watcher != nil {
Expand Down
Loading