Skip to content

Commit 48c0a8e

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 48c0a8e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/go-cmp v0.5.9
1111
github.com/onsi/ginkgo/v2 v2.8.0
1212
github.com/onsi/gomega v1.26.0
13+
github.com/pkg/errors v0.9.1
1314
github.com/prometheus/client_golang v1.14.0
1415
github.com/prometheus/client_model v0.3.0
1516
go.uber.org/goleak v1.2.0
@@ -50,7 +51,6 @@ require (
5051
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5152
github.com/modern-go/reflect2 v1.0.2 // indirect
5253
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
53-
github.com/pkg/errors v0.9.1 // indirect
5454
github.com/prometheus/common v0.37.0 // indirect
5555
github.com/prometheus/procfs v0.8.0 // indirect
5656
github.com/spf13/pflag v1.0.5 // indirect

pkg/certwatcher/certwatcher.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"sync"
23+
"time"
2324

2425
"github.com/fsnotify/fsnotify"
26+
"github.com/pkg/errors"
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 errors.Wrapf(kerrors.NewAggregate([]error{err, watchErr}), "failed to add watches")
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)