Skip to content
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

Fix NGF fails to recover if conf files are unexpectedly removed #1132

Merged
merged 6 commits into from
Oct 13, 2023
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 {
bjee19 marked this conversation as resolved.
Show resolved Hide resolved
for _, path := range m.lastWrittenPaths {
if err := m.osFileManager.Remove(path); err != nil {
if os.IsNotExist(err) {
bjee19 marked this conversation as resolved.
Show resolved Hide resolved
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