@@ -41,6 +41,7 @@ const (
4141 caCertName = "ca.crt"
4242 caKeyName = "ca.key"
4343 rotationCheckFrequency = 12 * time .Hour
44+ certValidityDuration = 10 * 365 * 24 * time .Hour
4445 lookaheadInterval = 90 * 24 * time .Hour
4546)
4647
@@ -62,9 +63,6 @@ var _ manager.Runnable = &CertRotator{}
6263
6364var restartOnSecretRefresh = false
6465
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-
6866//WebhookInfo is used by the rotator to receive info about resources to be updated with certificates
6967type WebhookInfo struct {
7068 //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 {
7371}
7472
7573func 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)" )
7775}
7876
7977func (w WebhookInfo ) gvk () schema.GroupVersionKind {
@@ -148,7 +146,7 @@ func addNamespacedCache(mgr manager.Manager, namespace string) (cache.Cache, err
148146// SyncingSource is a reader that needs syncing prior to being usable.
149147type SyncingReader interface {
150148 client.Reader
151- WaitForCacheSync (stop <- chan struct {} ) bool
149+ WaitForCacheSync (ctx context. Context ) bool
152150}
153151
154152// CertRotator contains cert artifacts and a channel to close when the certs are ready.
@@ -169,11 +167,11 @@ type CertRotator struct {
169167}
170168
171169// 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 {
173171 if cr .reader == nil {
174172 return errors .New ("nil reader" )
175173 }
176- if ! cr .reader .WaitForCacheSync (stop ) {
174+ if ! cr .reader .WaitForCacheSync (ctx ) {
177175 return errors .New ("failed waiting for reader to sync" )
178176 }
179177
@@ -199,7 +197,7 @@ tickerLoop:
199197 if err := cr .refreshCertIfNeeded (); err != nil {
200198 crLog .Error (err , "error rotating certs" )
201199 }
202- case <- stop :
200+ case <- ctx . Done () :
203201 break tickerLoop
204202 case <- cr .certsNotMounted :
205203 return errors .New ("could not mount certs" )
@@ -264,7 +262,7 @@ func (cr *CertRotator) refreshCerts(refreshCA bool, secret *corev1.Secret) error
264262 var caArtifacts * KeyPairArtifacts
265263 now := time .Now ()
266264 begin := now .Add (- 1 * time .Hour )
267- end := now .Add (* certValidityDuration )
265+ end := now .Add (certValidityDuration )
268266 if refreshCA {
269267 var err error
270268 caArtifacts , err = cr .CreateCACert (begin , end )
@@ -539,43 +537,17 @@ func ValidCert(caCert, cert, key []byte, dnsName string, at time.Time) (bool, er
539537 return true , nil
540538}
541539
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
559545 }
546+ if object .GetName () != whKey .Name {
547+ return nil
548+ }
549+ return []reconcile.Request {{NamespacedName : r .secretKey }}
560550 }
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 }}
579551}
580552
581553// 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 {
599571 wh .SetGroupVersionKind (webhook .gvk ())
600572 err = c .Watch (
601573 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 )),
606575 )
607576 if err != nil {
608577 return fmt .Errorf ("watching webhook %s: %w" , webhook .Name , err )
@@ -628,13 +597,12 @@ type ReconcileWH struct {
628597
629598// Reconcile reads that state of the cluster for a validatingwebhookconfiguration
630599// 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 ) {
632601 if request .NamespacedName != r .secretKey {
633602 return reconcile.Result {}, nil
634603 }
635604
636- stop := make (<- chan struct {})
637- if ! r .cache .WaitForCacheSync (stop ) {
605+ if ! r .cache .WaitForCacheSync (ctx ) {
638606 return reconcile.Result {}, errors .New ("cache not ready" )
639607 }
640608
@@ -657,11 +625,9 @@ func (r *ReconcileWH) Reconcile(request reconcile.Request) (reconcile.Result, er
657625 }
658626
659627 // Ensure certs on webhooks
660- fmt .Println ("Starting cert injection" )
661628 if err := r .ensureCerts (artifacts .CertPEM ); err != nil {
662629 return reconcile.Result {}, err
663630 }
664- fmt .Println ("Finished cert injection" )
665631
666632 // Set CAInjected if the reconciler has not exited early.
667633 r .wasCAInjected .Store (true )
@@ -690,32 +656,25 @@ func (r *ReconcileWH) ensureCerts(certPem []byte) error {
690656 updatedResource .SetGroupVersionKind (gvk )
691657 if err := r .cache .Get (r .ctx , types.NamespacedName {Name : webhook .Name }, updatedResource ); err != nil {
692658 if k8sErrors .IsNotFound (err ) {
693- fmt .Println ("Webhook not found. Unable to update certificate." , err )
694659 log .Error (err , "Webhook not found. Unable to update certificate." )
695660 continue
696661 }
697662 anyError = err
698663 log .Error (err , "Error getting webhook for certificate update." )
699- fmt .Println ("Error getting webhook for certificate update." , err )
700-
701664 continue
702665 }
703666 if ! updatedResource .GetDeletionTimestamp ().IsZero () {
704- fmt .Println ("Webhook is being deleted. Unable to update certificate" )
705667 log .Info ("Webhook is being deleted. Unable to update certificate" )
706668 continue
707669 }
708670
709671 log .Info ("Ensuring CA cert" , "name" , webhook .Name , "gvk" , gvk )
710672 if err := injectCert (updatedResource , certPem , webhook .Type ); err != nil {
711- fmt .Println ("Unable to inject cert to webhook.:" , err )
712673 log .Error (err , "Unable to inject cert to webhook." )
713674 anyError = err
714675 continue
715676 }
716677 if err := r .writer .Update (r .ctx , updatedResource ); err != nil {
717- fmt .Println ("Error updating webhook with certificate:" , err )
718-
719678 log .Error (err , "Error updating webhook with certificate" )
720679 anyError = err
721680 continue
@@ -766,4 +725,4 @@ func (cr *CertRotator) ensureReady() {
766725 }
767726 crLog .Info ("CA certs are injected to webhooks" )
768727 close (cr .IsReady )
769- }
728+ }
0 commit comments