Skip to content

Commit 27793ff

Browse files
m-messiahk8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
Handle fsnotify.Chmod events as Removals
1 parent 4b8b9e6 commit 27793ff

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

pkg/certwatcher/certwatcher.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ func (cw *CertWatcher) ReadCertificate() error {
173173

174174
func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
175175
// Only care about events which may modify the contents of the file.
176-
if !(isWrite(event) || isRemove(event) || isCreate(event)) {
176+
if !(isWrite(event) || isRemove(event) || isCreate(event) || isChmod(event)) {
177177
return
178178
}
179179

180180
log.V(1).Info("certificate event", "event", event)
181181

182-
// If the file was removed, re-add the watch.
183-
if isRemove(event) {
182+
// If the file was removed or renamed, re-add the watch to the previous name
183+
if isRemove(event) || isChmod(event) {
184184
if err := cw.watcher.Add(event.Name); err != nil {
185185
log.Error(err, "error re-watching file")
186186
}
@@ -202,3 +202,7 @@ func isCreate(event fsnotify.Event) bool {
202202
func isRemove(event fsnotify.Event) bool {
203203
return event.Op.Has(fsnotify.Remove)
204204
}
205+
206+
func isChmod(event fsnotify.Event) bool {
207+
return event.Op.Has(fsnotify.Chmod)
208+
}

pkg/certwatcher/certwatcher_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,18 @@ var _ = Describe("CertWatcher", func() {
189189

190190
Expect(os.Remove(keyPath)).To(Succeed())
191191

192+
// Note, we are checking two errors here, because os.Remove generates two fsnotify events: Chmod + Remove
192193
Eventually(func() error {
193194
readCertificateTotalAfter := testutil.ToFloat64(metrics.ReadCertificateTotal)
194-
if readCertificateTotalAfter != readCertificateTotalBefore+1.0 {
195-
return fmt.Errorf("metric read certificate total expected: %v and got: %v", readCertificateTotalBefore+1.0, readCertificateTotalAfter)
195+
if readCertificateTotalAfter != readCertificateTotalBefore+2.0 {
196+
return fmt.Errorf("metric read certificate total expected: %v and got: %v", readCertificateTotalBefore+2.0, readCertificateTotalAfter)
196197
}
197198
return nil
198199
}, "4s").Should(Succeed())
199200
Eventually(func() error {
200201
readCertificateErrorsAfter := testutil.ToFloat64(metrics.ReadCertificateErrors)
201-
if readCertificateErrorsAfter != readCertificateErrorsBefore+1.0 {
202-
return fmt.Errorf("metric read certificate errors expected: %v and got: %v", readCertificateErrorsBefore+1.0, readCertificateErrorsAfter)
202+
if readCertificateErrorsAfter != readCertificateErrorsBefore+2.0 {
203+
return fmt.Errorf("metric read certificate errors expected: %v and got: %v", readCertificateErrorsBefore+2.0, readCertificateErrorsAfter)
203204
}
204205
return nil
205206
}, "4s").Should(Succeed())

0 commit comments

Comments
 (0)