diff --git a/internal/mode/static/nginx/file/manager.go b/internal/mode/static/nginx/file/manager.go index 560ab878af..632a3af34f 100644 --- a/internal/mode/static/nginx/file/manager.go +++ b/internal/mode/static/nginx/file/manager.go @@ -88,10 +88,18 @@ func NewManagerImpl(logger logr.Logger, osFileManager OSFileManager) *ManagerImp func (m *ManagerImpl) ReplaceFiles(files []File) error { for _, path := range m.lastWrittenPaths { if err := m.osFileManager.Remove(path); err != nil { + if os.IsNotExist(err) { + 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 @@ -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 diff --git a/internal/mode/static/nginx/file/manager_test.go b/internal/mode/static/nginx/file/manager_test.go index c6f52a526c..07f0478ae9 100644 --- a/internal/mode/static/nginx/file/manager_test.go +++ b/internal/mode/static/nginx/file/manager_test.go @@ -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)