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: 10 additions & 2 deletions internal/mode/static/nginx/file/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,18 @@ func NewManagerImpl(logger logr.Logger, osFileManager OSFileManager) *ManagerImp
func (m *ManagerImpl) ReplaceFiles(files []File) error {
Comment thread
bjee19 marked this conversation as resolved.
Outdated
for _, path := range m.lastWrittenPaths {
if err := m.osFileManager.Remove(path); err != nil {
if os.IsNotExist(err) {
Comment thread
bjee19 marked this conversation as resolved.
Outdated
m.logger.Info(
"File not found when attempting to delete",
"path", path,
"error", err,
)
continue
}
return fmt.Errorf("failed to delete file %q: %w", path, err)
}

m.logger.Info("deleted file", "path", path)
m.logger.Info("Deleted file", "path", path)
}

// In some cases, NGINX reads files in runtime, like a JWK. If you remove such file, NGINX will fail
Expand All @@ -106,7 +114,7 @@ func (m *ManagerImpl) ReplaceFiles(files []File) error {
}

m.lastWrittenPaths = append(m.lastWrittenPaths, file.Path)
m.logger.Info("wrote file", "path", file.Path)
m.logger.Info("Wrote file", "path", file.Path)
}

return nil
Expand Down
20 changes: 20 additions & 0 deletions internal/mode/static/nginx/file/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ var _ = Describe("EventHandler", func() {
})
})

When("file does not exist", func() {
It("should not error", func() {
fakeOSMgr := &filefakes.FakeOSFileManager{}
mgr := file.NewManagerImpl(zap.New(), fakeOSMgr)

files := []file.File{
{
Type: file.TypeRegular,
Path: "regular-1.conf",
Content: []byte("regular-1"),
},
}

Expect(mgr.ReplaceFiles(files)).ToNot(HaveOccurred())

fakeOSMgr.RemoveReturns(os.ErrNotExist)
Expect(mgr.ReplaceFiles(files)).ToNot(HaveOccurred())
})
})

When("file type is not supported", func() {
It("should panic", func() {
mgr := file.NewManagerImpl(zap.New(), nil)
Expand Down