@@ -80,7 +80,7 @@ type TargetCertCreator interface {
8080 // NewCertificate creates a new key-cert pair with the given signer.
8181 NewCertificate (signer * crypto.CA , validity time.Duration ) (* crypto.TLSCertificateConfig , error )
8282 // NeedNewTargetCertKeyPair decides whether a new cert-key pair is needed. It returns a non-empty reason if it is the case.
83- NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired bool ) string
83+ NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired , secretDoesntExist bool ) string
8484 // SetAnnotations gives an option to override or set additional annotations
8585 SetAnnotations (cert * crypto.TLSCertificateConfig , annotations map [string ]string ) map [string ]string
8686}
@@ -97,6 +97,7 @@ func (c RotatedSelfSignedCertKeySecret) EnsureTargetCertKeyPair(ctx context.Cont
9797 // and need to mint one
9898 // TODO do the cross signing thing, but this shows the API consumers want and a very simple impl.
9999
100+ secretDoesntExist := false
100101 modified := false
101102 originalTargetCertKeyPairSecret , err := c .Lister .Secrets (c .Namespace ).Get (c .Name )
102103 if err != nil && ! apierrors .IsNotFound (err ) {
@@ -114,6 +115,7 @@ func (c RotatedSelfSignedCertKeySecret) EnsureTargetCertKeyPair(ctx context.Cont
114115 Type : corev1 .SecretTypeTLS ,
115116 }
116117 modified = true
118+ secretDoesntExist = true
117119 }
118120
119121 applyFn := resourceapply .ApplySecret
@@ -127,7 +129,7 @@ func (c RotatedSelfSignedCertKeySecret) EnsureTargetCertKeyPair(ctx context.Cont
127129 needsSecretTypeUpdate := ensureSecretTLSTypeSet (targetCertKeyPairSecret )
128130 modified = needsMetadataUpdate || needsSecretTypeUpdate || modified
129131
130- if reason := c .CertCreator .NeedNewTargetCertKeyPair (targetCertKeyPairSecret , signingCertKeyPair , caBundleCerts , c .Refresh , c .RefreshOnlyWhenExpired ); len (reason ) > 0 {
132+ if reason := c .CertCreator .NeedNewTargetCertKeyPair (targetCertKeyPairSecret , signingCertKeyPair , caBundleCerts , c .Refresh , c .RefreshOnlyWhenExpired , secretDoesntExist ); len (reason ) > 0 {
131133 c .EventRecorder .Eventf ("TargetUpdateRequired" , "%q in %q requires a new target cert/key pair: %v" , c .Name , c .Namespace , reason )
132134 if err := setTargetCertKeyPairSecret (targetCertKeyPairSecret , c .Validity , signingCertKeyPair , c .CertCreator , c .AdditionalAnnotations ); err != nil {
133135 return nil , err
@@ -149,7 +151,12 @@ func (c RotatedSelfSignedCertKeySecret) EnsureTargetCertKeyPair(ctx context.Cont
149151 return targetCertKeyPairSecret , nil
150152}
151153
152- func needNewTargetCertKeyPair (annotations map [string ]string , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired bool ) string {
154+ func needNewTargetCertKeyPair (secret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired , secretDoesntExist bool ) string {
155+ if secretDoesntExist {
156+ return "secret doesn't exist"
157+ }
158+
159+ annotations := secret .Annotations
153160 if reason := needNewTargetCertKeyPairForTime (annotations , signer , refresh , refreshOnlyWhenExpired ); len (reason ) > 0 {
154161 return reason
155162 }
@@ -206,7 +213,7 @@ func needNewTargetCertKeyPairForTime(annotations map[string]string, signer *cryp
206213 validity := notAfter .Sub (notBefore )
207214 at80Percent := notAfter .Add (- validity / 5 )
208215 if time .Now ().After (at80Percent ) {
209- return fmt .Sprintf ("past its latest possible time %v" , at80Percent )
216+ return fmt .Sprintf ("past refresh time (80%% of validity): %v" , at80Percent )
210217 }
211218
212219 // If Certificate is past its refresh time, we may have action to take. We only do this if the signer is old enough.
@@ -266,8 +273,8 @@ func (r *ClientRotation) NewCertificate(signer *crypto.CA, validity time.Duratio
266273 return signer .MakeClientCertificateForDuration (r .UserInfo , validity )
267274}
268275
269- func (r * ClientRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired bool ) string {
270- return needNewTargetCertKeyPair (currentCertSecret . Annotations , signer , caBundleCerts , refresh , refreshOnlyWhenExpired )
276+ func (r * ClientRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired , secretDoesntExist bool ) string {
277+ return needNewTargetCertKeyPair (currentCertSecret , signer , caBundleCerts , refresh , refreshOnlyWhenExpired , secretDoesntExist )
271278}
272279
273280func (r * ClientRotation ) SetAnnotations (cert * crypto.TLSCertificateConfig , annotations map [string ]string ) map [string ]string {
@@ -291,8 +298,8 @@ func (r *ServingRotation) RecheckChannel() <-chan struct{} {
291298 return r .HostnamesChanged
292299}
293300
294- func (r * ServingRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired bool ) string {
295- reason := needNewTargetCertKeyPair (currentCertSecret . Annotations , signer , caBundleCerts , refresh , refreshOnlyWhenExpired )
301+ func (r * ServingRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired , secretDoesntExist bool ) string {
302+ reason := needNewTargetCertKeyPair (currentCertSecret , signer , caBundleCerts , refresh , refreshOnlyWhenExpired , secretDoesntExist )
296303 if len (reason ) > 0 {
297304 return reason
298305 }
@@ -337,8 +344,8 @@ func (r *SignerRotation) NewCertificate(signer *crypto.CA, validity time.Duratio
337344 return crypto .MakeCAConfigForDuration (signerName , validity , signer )
338345}
339346
340- func (r * SignerRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired bool ) string {
341- return needNewTargetCertKeyPair (currentCertSecret . Annotations , signer , caBundleCerts , refresh , refreshOnlyWhenExpired )
347+ func (r * SignerRotation ) NeedNewTargetCertKeyPair (currentCertSecret * corev1.Secret , signer * crypto.CA , caBundleCerts []* x509.Certificate , refresh time.Duration , refreshOnlyWhenExpired , secretDoesntExist bool ) string {
348+ return needNewTargetCertKeyPair (currentCertSecret , signer , caBundleCerts , refresh , refreshOnlyWhenExpired , secretDoesntExist )
342349}
343350
344351func (r * SignerRotation ) SetAnnotations (cert * crypto.TLSCertificateConfig , annotations map [string ]string ) map [string ]string {
0 commit comments