@@ -34,6 +34,7 @@ import (
3434 . "github.com/onsi/ginkgo/v2"
3535 . "github.com/onsi/gomega"
3636 "github.com/prometheus/client_golang/prometheus/testutil"
37+
3738 "sigs.k8s.io/controller-runtime/pkg/certwatcher"
3839 "sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
3940)
@@ -113,7 +114,7 @@ var _ = Describe("CertWatcher", func() {
113114 Eventually (func () bool {
114115 secondcert , _ := watcher .GetCertificate (nil )
115116 first := firstcert .PrivateKey .(* rsa.PrivateKey )
116- return first .Equal (secondcert .PrivateKey )
117+ return first .Equal (secondcert .PrivateKey ) || firstcert . Leaf . SerialNumber == secondcert . Leaf . SerialNumber
117118 }).ShouldNot (BeTrue ())
118119
119120 ctxCancel ()
@@ -143,14 +144,41 @@ var _ = Describe("CertWatcher", func() {
143144 Eventually (func () bool {
144145 secondcert , _ := watcher .GetCertificate (nil )
145146 first := firstcert .PrivateKey .(* rsa.PrivateKey )
146- return first .Equal (secondcert .PrivateKey )
147+ return first .Equal (secondcert .PrivateKey ) || firstcert . Leaf . SerialNumber == secondcert . Leaf . SerialNumber
147148 }).ShouldNot (BeTrue ())
148149
149150 ctxCancel ()
150151 Eventually (doneCh , "4s" ).Should (BeClosed ())
151152 Expect (called .Load ()).To (BeNumerically (">=" , 1 ))
152153 })
153154
155+ It ("should reload currentCert after move out" , func () {
156+ doneCh := startWatcher ()
157+ called := atomic.Int64 {}
158+ watcher .RegisterCallback (func (crt tls.Certificate ) {
159+ called .Add (1 )
160+ Expect (crt .Certificate ).ToNot (BeEmpty ())
161+ })
162+
163+ firstcert , _ := watcher .GetCertificate (nil )
164+
165+ Expect (os .Rename (certPath , certPath + ".old" )).To (Succeed ())
166+ Expect (os .Rename (keyPath , keyPath + ".old" )).To (Succeed ())
167+
168+ err := writeCerts (certPath , keyPath , "192.168.0.3" )
169+ Expect (err ).ToNot (HaveOccurred ())
170+
171+ Eventually (func () bool {
172+ secondcert , _ := watcher .GetCertificate (nil )
173+ first := firstcert .PrivateKey .(* rsa.PrivateKey )
174+ return first .Equal (secondcert .PrivateKey ) || firstcert .Leaf .SerialNumber == secondcert .Leaf .SerialNumber
175+ }, "10s" , "1s" ).ShouldNot (BeTrue ())
176+
177+ ctxCancel ()
178+ Eventually (doneCh , "4s" ).Should (BeClosed ())
179+ Expect (called .Load ()).To (BeNumerically (">=" , 1 ))
180+ })
181+
154182 Context ("prometheus metric read_certificate_total" , func () {
155183 var readCertificateTotalBefore float64
156184 var readCertificateErrorsBefore float64
0 commit comments