Add RenewBeforeDuration option to controller context

This commit is contained in:
James Munnelly 2018-08-08 12:32:56 +01:00
parent a27bc5dada
commit fa0bc9998e
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,
IssuerAmbientCredentials: opts.IssuerAmbientCredentials,
ClusterResourceNamespace: opts.ClusterResourceNamespace,
RenewBeforeExpiryDuration: opts.RenewBeforeExpiryDuration,
},
IngressShimOptions: controller.IngressShimOptions{
DefaultIssuerName: opts.DefaultIssuerName,

View File

@ -31,6 +31,7 @@ type ControllerOptions struct {
ClusterIssuerAmbientCredentials bool
IssuerAmbientCredentials bool
RenewBeforeExpiryDuration time.Duration
// Default issuer/certificates details consumed by ingress-shim
DefaultIssuerName string
@ -54,6 +55,7 @@ const (
defaultClusterIssuerAmbientCredentials = true
defaultIssuerAmbientCredentials = false
defaultRenewBeforeExpiryDuration = time.Hour * 24 * 30
defaultTLSACMEIssuerName = ""
defaultTLSACMEIssuerKind = "Issuer"
@ -84,6 +86,7 @@ func NewControllerOptions() *ControllerOptions {
EnabledControllers: defaultEnabledControllers,
ClusterIssuerAmbientCredentials: defaultClusterIssuerAmbientCredentials,
IssuerAmbientCredentials: defaultIssuerAmbientCredentials,
RenewBeforeExpiryDuration: defaultRenewBeforeExpiryDuration,
DefaultIssuerName: defaultTLSACMEIssuerName,
DefaultIssuerKind: defaultTLSACMEIssuerKind,
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. "+
"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.")
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, ""+
"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 (
"context"
"crypto/x509"
"fmt"
"reflect"
"strings"
@ -24,8 +23,6 @@ import (
"github.com/jetstack/cert-manager/pkg/util/pki"
)
const renewBefore = time.Hour * 24 * 30
const (
errorIssuerNotFound = "IssuerNotFound"
errorIssuerNotReady = "IssuerNotReady"
@ -147,13 +144,8 @@ func (c *Controller) Sync(ctx context.Context, crt *v1alpha1.Certificate) (err e
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 renewIn <= 0 {
if c.Context.IssuerOptions.CertificateNeedsRenew(cert) {
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) {
key, err := keyFunc(crt)
@ -201,7 +183,7 @@ func (c *Controller) scheduleRenewal(crt *v1alpha1.Certificate) {
}
durationUntilExpiry := cert.NotAfter.Sub(time.Now())
renewIn := durationUntilExpiry - renewBefore
renewIn := durationUntilExpiry - c.Context.IssuerOptions.RenewBeforeExpiryDuration
c.scheduledWorkQueue.Add(key, renewIn)

View File

@ -1,6 +1,8 @@
package controller
import (
"time"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
@ -50,6 +52,11 @@ type IssuerOptions struct {
// IssuerAmbientCredentials controls whether an issuer should pick up ambient
// credentials, such as those from metadata services, to construct clients.
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 {

View File

@ -1,7 +1,9 @@
package controller
import (
"crypto/x509"
"fmt"
"time"
cmapi "github.com/jetstack/cert-manager/pkg/apis/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
}
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
}