Merge pull request #801 from munnerz/renew-duration

Add RenewBeforeExpiryDuration option to controller context
This commit is contained in:
jetstack-bot 2018-08-08 14:34:56 +01:00 committed by GitHub
commit c43e4d75f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 20 deletions

View File

@ -135,6 +135,7 @@ func buildControllerContext(opts *options.ControllerOptions) (*controller.Contex
ClusterIssuerAmbientCredentials: opts.ClusterIssuerAmbientCredentials, ClusterIssuerAmbientCredentials: opts.ClusterIssuerAmbientCredentials,
IssuerAmbientCredentials: opts.IssuerAmbientCredentials, IssuerAmbientCredentials: opts.IssuerAmbientCredentials,
ClusterResourceNamespace: opts.ClusterResourceNamespace, ClusterResourceNamespace: opts.ClusterResourceNamespace,
RenewBeforeExpiryDuration: opts.RenewBeforeExpiryDuration,
}, },
IngressShimOptions: controller.IngressShimOptions{ IngressShimOptions: controller.IngressShimOptions{
DefaultIssuerName: opts.DefaultIssuerName, DefaultIssuerName: opts.DefaultIssuerName,

View File

@ -31,6 +31,7 @@ type ControllerOptions struct {
ClusterIssuerAmbientCredentials bool ClusterIssuerAmbientCredentials bool
IssuerAmbientCredentials bool IssuerAmbientCredentials bool
RenewBeforeExpiryDuration time.Duration
// Default issuer/certificates details consumed by ingress-shim // Default issuer/certificates details consumed by ingress-shim
DefaultIssuerName string DefaultIssuerName string
@ -54,6 +55,7 @@ const (
defaultClusterIssuerAmbientCredentials = true defaultClusterIssuerAmbientCredentials = true
defaultIssuerAmbientCredentials = false defaultIssuerAmbientCredentials = false
defaultRenewBeforeExpiryDuration = time.Hour * 24 * 30
defaultTLSACMEIssuerName = "" defaultTLSACMEIssuerName = ""
defaultTLSACMEIssuerKind = "Issuer" defaultTLSACMEIssuerKind = "Issuer"
@ -84,6 +86,7 @@ func NewControllerOptions() *ControllerOptions {
EnabledControllers: defaultEnabledControllers, EnabledControllers: defaultEnabledControllers,
ClusterIssuerAmbientCredentials: defaultClusterIssuerAmbientCredentials, ClusterIssuerAmbientCredentials: defaultClusterIssuerAmbientCredentials,
IssuerAmbientCredentials: defaultIssuerAmbientCredentials, IssuerAmbientCredentials: defaultIssuerAmbientCredentials,
RenewBeforeExpiryDuration: defaultRenewBeforeExpiryDuration,
DefaultIssuerName: defaultTLSACMEIssuerName, DefaultIssuerName: defaultTLSACMEIssuerName,
DefaultIssuerKind: defaultTLSACMEIssuerKind, DefaultIssuerKind: defaultTLSACMEIssuerKind,
DefaultACMEIssuerChallengeType: defaultACMEIssuerChallengeType, DefaultACMEIssuerChallengeType: defaultACMEIssuerChallengeType,
@ -133,6 +136,10 @@ func (s *ControllerOptions) AddFlags(fs *pflag.FlagSet) {
"Whether an issuer may make use of ambient credentials. 'Ambient Credentials' are credentials drawn from the environment, metadata services, or local files which are not explicitly configured in the Issuer API object. "+ "Whether an issuer may make use of ambient credentials. 'Ambient Credentials' are credentials drawn from the environment, metadata services, or local files which are not explicitly configured in the Issuer API object. "+
"When this flag is enabled, the following sources for credentials are also used: "+ "When this flag is enabled, the following sources for credentials are also used: "+
"AWS - All sources the Go SDK defaults to, notably including any EC2 IAM roles available via instance metadata.") "AWS - All sources the Go SDK defaults to, notably including any EC2 IAM roles available via instance metadata.")
fs.DurationVar(&s.RenewBeforeExpiryDuration, "renew-before-expiry-duration", defaultRenewBeforeExpiryDuration, ""+
"The default 'renew before expiry' time for Certificates. "+
"Once a certificate is within this duration until expiry, a new Certificate "+
"will be attempted to be issued.")
fs.StringVar(&s.DefaultIssuerName, "default-issuer-name", defaultTLSACMEIssuerName, ""+ fs.StringVar(&s.DefaultIssuerName, "default-issuer-name", defaultTLSACMEIssuerName, ""+
"Name of the Issuer to use when the tls is requested but issuer name is not specified on the ingress resource.") "Name of the Issuer to use when the tls is requested but issuer name is not specified on the ingress resource.")

View File

@ -2,7 +2,6 @@ package certificates
import ( import (
"context" "context"
"crypto/x509"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -24,8 +23,6 @@ import (
"github.com/jetstack/cert-manager/pkg/util/pki" "github.com/jetstack/cert-manager/pkg/util/pki"
) )
const renewBefore = time.Hour * 24 * 30
const ( const (
errorIssuerNotFound = "IssuerNotFound" errorIssuerNotFound = "IssuerNotFound"
errorIssuerNotReady = "IssuerNotReady" errorIssuerNotReady = "IssuerNotReady"
@ -147,13 +144,8 @@ func (c *Controller) Sync(ctx context.Context, crt *v1alpha1.Certificate) (err e
return c.issue(ctx, i, crtCopy) return c.issue(ctx, i, crtCopy)
} }
// calculate the amount of time until expiry
durationUntilExpiry := cert.NotAfter.Sub(time.Now())
// calculate how long until we should start attempting to renew the
// certificate
renewIn := durationUntilExpiry - renewBefore
// if we should being attempting to renew now, then trigger a renewal // if we should being attempting to renew now, then trigger a renewal
if renewIn <= 0 { if c.Context.IssuerOptions.CertificateNeedsRenew(cert) {
return c.renew(ctx, i, crtCopy) return c.renew(ctx, i, crtCopy)
} }
@ -175,16 +167,6 @@ func (c *Controller) getGenericIssuer(crt *v1alpha1.Certificate) (v1alpha1.Gener
} }
} }
func needsRenew(cert *x509.Certificate) bool {
durationUntilExpiry := cert.NotAfter.Sub(time.Now())
renewIn := durationUntilExpiry - renewBefore
// step three: check if referenced secret is valid (after start & before expiry)
if renewIn <= 0 {
return true
}
return false
}
func (c *Controller) scheduleRenewal(crt *v1alpha1.Certificate) { func (c *Controller) scheduleRenewal(crt *v1alpha1.Certificate) {
key, err := keyFunc(crt) key, err := keyFunc(crt)
@ -201,7 +183,7 @@ func (c *Controller) scheduleRenewal(crt *v1alpha1.Certificate) {
} }
durationUntilExpiry := cert.NotAfter.Sub(time.Now()) durationUntilExpiry := cert.NotAfter.Sub(time.Now())
renewIn := durationUntilExpiry - renewBefore renewIn := durationUntilExpiry - c.Context.IssuerOptions.RenewBeforeExpiryDuration
c.scheduledWorkQueue.Add(key, renewIn) c.scheduledWorkQueue.Add(key, renewIn)

View File

@ -1,6 +1,8 @@
package controller package controller
import ( import (
"time"
kubeinformers "k8s.io/client-go/informers" kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record" "k8s.io/client-go/tools/record"
@ -50,6 +52,11 @@ type IssuerOptions struct {
// IssuerAmbientCredentials controls whether an issuer should pick up ambient // IssuerAmbientCredentials controls whether an issuer should pick up ambient
// credentials, such as those from metadata services, to construct clients. // credentials, such as those from metadata services, to construct clients.
IssuerAmbientCredentials bool IssuerAmbientCredentials bool
// RenewBeforeExpiryDuration is the default 'renew before expiry' time for Certificates.
// Once a certificate is within this duration until expiry, a new Certificate
// will be attempted to be issued.
RenewBeforeExpiryDuration time.Duration
} }
type ACMEOptions struct { type ACMEOptions struct {

View File

@ -1,7 +1,9 @@
package controller package controller
import ( import (
"crypto/x509"
"fmt" "fmt"
"time"
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1" cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
cmlisters "github.com/jetstack/cert-manager/pkg/client/listers/certmanager/v1alpha1" cmlisters "github.com/jetstack/cert-manager/pkg/client/listers/certmanager/v1alpha1"
@ -65,3 +67,16 @@ func (o IssuerOptions) CanUseAmbientCredentials(iss cmapi.GenericIssuer) bool {
} }
return false return false
} }
func (o IssuerOptions) CertificateNeedsRenew(cert *x509.Certificate) bool {
// calculate the amount of time until expiry
durationUntilExpiry := cert.NotAfter.Sub(time.Now())
// calculate how long until we should start attempting to renew the
// certificate
renewIn := durationUntilExpiry - o.RenewBeforeExpiryDuration
// if we should being attempting to renew now, then trigger a renewal
if renewIn <= 0 {
return true
}
return false
}