Skip to content

Commit ab55747

Browse files
committed
pkg/certwatcher: Start should retry for 10s when adding files
This fixes a flake in CI, but it could also come in handy when running the certwatcher against volume mounted certificates. Ideally the timeout is going to be configurable at some point, for now, let's just retry for a fixed number of seconds, before returning an error. Signed-off-by: Vince Prignano <[email protected]>
1 parent 81199b9 commit ab55747

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

pkg/certwatcher/certwatcher.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ package certwatcher
1919
import (
2020
"context"
2121
"crypto/tls"
22+
"fmt"
2223
"sync"
24+
"time"
2325

2426
"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"
2530
"sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
2631
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
2732
)
@@ -72,11 +77,24 @@ func (cw *CertWatcher) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate,
7277

7378
// Start starts the watch on the certificate and key files.
7479
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}))
8098
}
8199
}
82100

@@ -154,13 +172,13 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
154172
}
155173

156174
func isWrite(event fsnotify.Event) bool {
157-
return event.Op&fsnotify.Write == fsnotify.Write
175+
return event.Op.Has(fsnotify.Write)
158176
}
159177

160178
func isCreate(event fsnotify.Event) bool {
161-
return event.Op&fsnotify.Create == fsnotify.Create
179+
return event.Op.Has(fsnotify.Create)
162180
}
163181

164182
func isRemove(event fsnotify.Event) bool {
165-
return event.Op&fsnotify.Remove == fsnotify.Remove
183+
return event.Op.Has(fsnotify.Remove)
166184
}

0 commit comments

Comments
 (0)