@@ -41,6 +41,7 @@ const (
41
41
caCertName = "ca.crt"
42
42
caKeyName = "ca.key"
43
43
rotationCheckFrequency = 12 * time .Hour
44
+ certValidityDuration = 10 * 365 * 24 * time .Hour
44
45
lookaheadInterval = 90 * 24 * time .Hour
45
46
)
46
47
@@ -62,9 +63,6 @@ var _ manager.Runnable = &CertRotator{}
62
63
63
64
var restartOnSecretRefresh = false
64
65
65
- var certValidityDuration = flag .Duration ("cert-validity-duration" , 10 * 365 * 24 * time .Hour , "Sets how long the cert is valid for, defaults to 10 years" )
66
-
67
-
68
66
//WebhookInfo is used by the rotator to receive info about resources to be updated with certificates
69
67
type WebhookInfo struct {
70
68
//Name is the name of the webhook for a validating or mutating webhook, or the CRD name in case of a CRD conversion webhook
@@ -73,7 +71,7 @@ type WebhookInfo struct {
73
71
}
74
72
75
73
func init () {
76
- flag .BoolVar (& restartOnSecretRefresh , "cert-restart-on-secret-refresh" , true , "Kills the process when secrets are refreshed so that the pod can be restarted (secrets take up to 60s to be updated by running pods)" )
74
+ flag .BoolVar (& restartOnSecretRefresh , "cert-restart-on-secret-refresh" , false , "Kills the process when secrets are refreshed so that the pod can be restarted (secrets take up to 60s to be updated by running pods)" )
77
75
}
78
76
79
77
func (w WebhookInfo ) gvk () schema.GroupVersionKind {
@@ -148,7 +146,7 @@ func addNamespacedCache(mgr manager.Manager, namespace string) (cache.Cache, err
148
146
// SyncingSource is a reader that needs syncing prior to being usable.
149
147
type SyncingReader interface {
150
148
client.Reader
151
- WaitForCacheSync (stop <- chan struct {} ) bool
149
+ WaitForCacheSync (ctx context. Context ) bool
152
150
}
153
151
154
152
// CertRotator contains cert artifacts and a channel to close when the certs are ready.
@@ -169,11 +167,11 @@ type CertRotator struct {
169
167
}
170
168
171
169
// Start starts the CertRotator runnable to rotate certs and ensure the certs are ready.
172
- func (cr * CertRotator ) Start (stop <- chan struct {} ) error {
170
+ func (cr * CertRotator ) Start (ctx context. Context ) error {
173
171
if cr .reader == nil {
174
172
return errors .New ("nil reader" )
175
173
}
176
- if ! cr .reader .WaitForCacheSync (stop ) {
174
+ if ! cr .reader .WaitForCacheSync (ctx ) {
177
175
return errors .New ("failed waiting for reader to sync" )
178
176
}
179
177
@@ -199,7 +197,7 @@ tickerLoop:
199
197
if err := cr .refreshCertIfNeeded (); err != nil {
200
198
crLog .Error (err , "error rotating certs" )
201
199
}
202
- case <- stop :
200
+ case <- ctx . Done () :
203
201
break tickerLoop
204
202
case <- cr .certsNotMounted :
205
203
return errors .New ("could not mount certs" )
@@ -264,7 +262,7 @@ func (cr *CertRotator) refreshCerts(refreshCA bool, secret *corev1.Secret) error
264
262
var caArtifacts * KeyPairArtifacts
265
263
now := time .Now ()
266
264
begin := now .Add (- 1 * time .Hour )
267
- end := now .Add (* certValidityDuration )
265
+ end := now .Add (certValidityDuration )
268
266
if refreshCA {
269
267
var err error
270
268
caArtifacts , err = cr .CreateCACert (begin , end )
@@ -539,43 +537,17 @@ func ValidCert(caCert, cert, key []byte, dnsName string, at time.Time) (bool, er
539
537
return true , nil
540
538
}
541
539
542
- // controller code for making sure the CA cert on the
543
- // webhooks don't get clobbered
544
-
545
- var _ handler.Mapper = & crdMapper {}
546
-
547
- type crdMapper struct {
548
- secretKey types.NamespacedName
549
- crdNames []string
550
- }
551
-
552
- func (m * crdMapper ) Map (object handler.MapObject ) []reconcile.Request {
553
- if object .Meta .GetNamespace () != "" {
554
- return nil
555
- }
556
- for _ , crdName := range m .crdNames {
557
- if object .Meta .GetName () == crdName {
558
- return []reconcile.Request {{NamespacedName : m .secretKey }}
540
+ func reconcileSecretAndWebhookMapFunc (webhook WebhookInfo , r * ReconcileWH ) func (object client.Object ) []reconcile.Request {
541
+ return func (object client.Object ) []reconcile.Request {
542
+ whKey := types.NamespacedName {Name : webhook .Name }
543
+ if object .GetNamespace () != whKey .Namespace {
544
+ return nil
559
545
}
546
+ if object .GetName () != whKey .Name {
547
+ return nil
548
+ }
549
+ return []reconcile.Request {{NamespacedName : r .secretKey }}
560
550
}
561
- return nil
562
- }
563
-
564
- var _ handler.Mapper = & mapper {}
565
-
566
- type mapper struct {
567
- secretKey types.NamespacedName
568
- whKey types.NamespacedName
569
- }
570
-
571
- func (m * mapper ) Map (object handler.MapObject ) []reconcile.Request {
572
- if object .Meta .GetNamespace () != m .whKey .Namespace {
573
- return nil
574
- }
575
- if object .Meta .GetName () != m .whKey .Name {
576
- return nil
577
- }
578
- return []reconcile.Request {{NamespacedName : m .secretKey }}
579
551
}
580
552
581
553
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -599,10 +571,7 @@ func addController(mgr manager.Manager, r *ReconcileWH) error {
599
571
wh .SetGroupVersionKind (webhook .gvk ())
600
572
err = c .Watch (
601
573
source .NewKindWithCache (wh , r .cache ),
602
- & handler.EnqueueRequestsFromMapFunc {ToRequests : & mapper {
603
- secretKey : r .secretKey ,
604
- whKey : types.NamespacedName {Name : webhook .Name },
605
- }},
574
+ handler .EnqueueRequestsFromMapFunc (reconcileSecretAndWebhookMapFunc (webhook , r )),
606
575
)
607
576
if err != nil {
608
577
return fmt .Errorf ("watching webhook %s: %w" , webhook .Name , err )
@@ -628,13 +597,12 @@ type ReconcileWH struct {
628
597
629
598
// Reconcile reads that state of the cluster for a validatingwebhookconfiguration
630
599
// object and makes sure the most recent CA cert is included
631
- func (r * ReconcileWH ) Reconcile (request reconcile.Request ) (reconcile.Result , error ) {
600
+ func (r * ReconcileWH ) Reconcile (ctx context. Context , request reconcile.Request ) (reconcile.Result , error ) {
632
601
if request .NamespacedName != r .secretKey {
633
602
return reconcile.Result {}, nil
634
603
}
635
604
636
- stop := make (<- chan struct {})
637
- if ! r .cache .WaitForCacheSync (stop ) {
605
+ if ! r .cache .WaitForCacheSync (ctx ) {
638
606
return reconcile.Result {}, errors .New ("cache not ready" )
639
607
}
640
608
@@ -657,11 +625,9 @@ func (r *ReconcileWH) Reconcile(request reconcile.Request) (reconcile.Result, er
657
625
}
658
626
659
627
// Ensure certs on webhooks
660
- fmt .Println ("Starting cert injection" )
661
628
if err := r .ensureCerts (artifacts .CertPEM ); err != nil {
662
629
return reconcile.Result {}, err
663
630
}
664
- fmt .Println ("Finished cert injection" )
665
631
666
632
// Set CAInjected if the reconciler has not exited early.
667
633
r .wasCAInjected .Store (true )
@@ -690,32 +656,25 @@ func (r *ReconcileWH) ensureCerts(certPem []byte) error {
690
656
updatedResource .SetGroupVersionKind (gvk )
691
657
if err := r .cache .Get (r .ctx , types.NamespacedName {Name : webhook .Name }, updatedResource ); err != nil {
692
658
if k8sErrors .IsNotFound (err ) {
693
- fmt .Println ("Webhook not found. Unable to update certificate." , err )
694
659
log .Error (err , "Webhook not found. Unable to update certificate." )
695
660
continue
696
661
}
697
662
anyError = err
698
663
log .Error (err , "Error getting webhook for certificate update." )
699
- fmt .Println ("Error getting webhook for certificate update." , err )
700
-
701
664
continue
702
665
}
703
666
if ! updatedResource .GetDeletionTimestamp ().IsZero () {
704
- fmt .Println ("Webhook is being deleted. Unable to update certificate" )
705
667
log .Info ("Webhook is being deleted. Unable to update certificate" )
706
668
continue
707
669
}
708
670
709
671
log .Info ("Ensuring CA cert" , "name" , webhook .Name , "gvk" , gvk )
710
672
if err := injectCert (updatedResource , certPem , webhook .Type ); err != nil {
711
- fmt .Println ("Unable to inject cert to webhook.:" , err )
712
673
log .Error (err , "Unable to inject cert to webhook." )
713
674
anyError = err
714
675
continue
715
676
}
716
677
if err := r .writer .Update (r .ctx , updatedResource ); err != nil {
717
- fmt .Println ("Error updating webhook with certificate:" , err )
718
-
719
678
log .Error (err , "Error updating webhook with certificate" )
720
679
anyError = err
721
680
continue
@@ -766,4 +725,4 @@ func (cr *CertRotator) ensureReady() {
766
725
}
767
726
crLog .Info ("CA certs are injected to webhooks" )
768
727
close (cr .IsReady )
769
- }
728
+ }
0 commit comments