@@ -19,9 +19,14 @@ package certwatcher
19
19
import (
20
20
"context"
21
21
"crypto/tls"
22
+ "fmt"
22
23
"sync"
24
+ "time"
23
25
24
26
"github.com/fsnotify/fsnotify"
27
+ kerrors "k8s.io/apimachinery/pkg/util/errors"
28
+ "k8s.io/apimachinery/pkg/util/sets"
29
+ "k8s.io/apimachinery/pkg/util/wait"
25
30
"sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
26
31
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
27
32
)
@@ -72,11 +77,24 @@ func (cw *CertWatcher) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate,
72
77
73
78
// Start starts the watch on the certificate and key files.
74
79
func (cw * CertWatcher ) Start (ctx context.Context ) error {
75
- files := []string {cw .certPath , cw .keyPath }
76
-
77
- for _ , f := range files {
78
- if err := cw .watcher .Add (f ); err != nil {
79
- return err
80
+ files := sets .New (cw .certPath , cw .keyPath )
81
+
82
+ {
83
+ ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
84
+ defer cancel ()
85
+ var watchErr error
86
+ if err := wait .PollImmediateUntilWithContext (ctx , 1 * time .Second , func (ctx context.Context ) (done bool , err error ) {
87
+ for _ , f := range files .UnsortedList () {
88
+ if err := cw .watcher .Add (f ); err != nil {
89
+ watchErr = err
90
+ return false , nil //nolint:nilerr // We want to keep trying.
91
+ }
92
+ // We've added the watch, remove it from the set.
93
+ files .Delete (f )
94
+ }
95
+ return true , nil
96
+ }); err != nil {
97
+ return fmt .Errorf ("failed to add watches: %w" , kerrors .NewAggregate ([]error {err , watchErr }))
80
98
}
81
99
}
82
100
@@ -154,13 +172,13 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
154
172
}
155
173
156
174
func isWrite (event fsnotify.Event ) bool {
157
- return event .Op & fsnotify . Write == fsnotify .Write
175
+ return event .Op . Has ( fsnotify .Write )
158
176
}
159
177
160
178
func isCreate (event fsnotify.Event ) bool {
161
- return event .Op & fsnotify . Create == fsnotify .Create
179
+ return event .Op . Has ( fsnotify .Create )
162
180
}
163
181
164
182
func isRemove (event fsnotify.Event ) bool {
165
- return event .Op & fsnotify . Remove == fsnotify .Remove
183
+ return event .Op . Has ( fsnotify .Remove )
166
184
}
0 commit comments