-
Notifications
You must be signed in to change notification settings - Fork 12
/
reconcile_certificate.go
164 lines (138 loc) · 5.26 KB
/
reconcile_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package reconcilers
import (
"context"
"math"
"time"
reconcilerutil "github.com/3scale-ops/basereconciler/util"
operatorv1alpha1 "github.com/3scale-ops/marin3r/apis/operator.marin3r/v1alpha1"
"github.com/3scale-ops/marin3r/pkg/reconcilers/operator/discoveryservicecertificate/providers"
internal_provider "github.com/3scale-ops/marin3r/pkg/reconcilers/operator/discoveryservicecertificate/providers/marin3r"
"github.com/3scale-ops/marin3r/pkg/util/clock"
"github.com/3scale-ops/marin3r/pkg/util/pki"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// CertificateReconciler is a struct with methods to reconcile DiscoveryServiceCertificates
type CertificateReconciler struct {
ctx context.Context
logger logr.Logger
client client.Client
scheme *runtime.Scheme
dsc *operatorv1alpha1.DiscoveryServiceCertificate
provider providers.CertificateProvider
clock clock.Clock
// Calculated fields
ready bool
hash string
notBefore *time.Time
notAfter *time.Time
schedule *time.Duration
}
// Ensure the provider implements the CertificateProvider interface
var _ providers.CertificateProvider = &internal_provider.CertificateProvider{}
// NewCertificateReconciler returns a new RevisionReconciler
func NewCertificateReconciler(ctx context.Context, logger logr.Logger, client client.Client,
s *runtime.Scheme, dsc *operatorv1alpha1.DiscoveryServiceCertificate, provider providers.CertificateProvider) CertificateReconciler {
return CertificateReconciler{ctx, logger, client, s, dsc, provider, clock.Real{}, false, "", nil, nil, nil}
}
// IsReady returns true if the certificate is ready after the
// reconcile. Should be invoked only after running Reconcile()
func (r *CertificateReconciler) IsReady() bool {
return r.ready
}
// GetCertificateHash returns true if the hash of the certificate.
// Should be invoked only after running Reconcile()
func (r *CertificateReconciler) GetCertificateHash() string {
return r.hash
}
// NotBefore returns the NotBefore property of the reconciled certificate.
// Should be invoked only after running Reconcile()
func (r *CertificateReconciler) NotBefore() time.Time {
return *r.notBefore
}
// NotAfter returns the NotAfter property of the reconciled certificate.
// Should be invoked only after running Reconcile()
func (r *CertificateReconciler) NotAfter() time.Time {
return *r.notAfter
}
// GetSchedule returns a time.Duration value that indicates
// when the reconcile needs to be triggered to renew the
// certificate
func (r *CertificateReconciler) GetSchedule() *time.Duration {
return r.schedule
}
// Reconcile progresses DiscoveryServiceCertificates revisions to match the desired state.
// It does so by creating/updating/deleting EnvoyConfigRevision API resources.
func (r *CertificateReconciler) Reconcile() (ctrl.Result, error) {
var err error
var certBytes []byte
// Get the certificate
certBytes, _, err = r.provider.GetCertificate()
if err != nil {
if errors.IsNotFound(err) {
_, _, err = r.provider.CreateCertificate()
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}
// Verify the certificate is valid
err = r.provider.VerifyCertificate()
if err != nil {
if pki.IsVerifyError(err) {
// The certificate is invalid
r.logger.Info("certificate failed validation", "reason", err.Error())
r.ready = false
} else {
// Some other failure occurred during the verify process
return ctrl.Result{}, err
}
} else {
r.ready = true
}
cert, err := pki.LoadX509Certificate(certBytes)
if err != nil {
return ctrl.Result{}, err
}
// time to certificate expiration is used to calculate next reconcile schedule
timeToExpire := cert.NotAfter.Sub(r.clock.Now())
// total duration of the certificate, used to calculate when to start trying renewal
duration := cert.NotAfter.Sub(cert.NotBefore)
if r.dsc.GetCertificateRenewalConfig().Enabled {
// renew the certificate when 20% or less of certificate's duration has passed
renewBefore := time.Duration(int64(math.Floor(float64(duration) * 0.20)))
// If certificate is not valid or is within the renewal window, reissue it
if r.ready == false || timeToExpire < renewBefore {
certBytes, _, err = r.provider.UpdateCertificate()
if err != nil {
return ctrl.Result{}, err
}
r.logger.Info("reissued certificate")
return ctrl.Result{Requeue: true}, nil
}
// schedule next reconcile
schedule := timeToExpire - renewBefore
r.schedule = &schedule
r.logger.Info("scheduled certificate renewal", "time", r.clock.Now().Add(schedule).String())
} else {
// schedule nextReconcile when certificate expires to update Ready = false in the status
if !r.clock.Now().After(cert.NotAfter) {
schedule := timeToExpire + time.Second
r.schedule = &schedule
r.logger.Info("scheduled certificate reconcile", "time", r.clock.Now().Add(schedule).String())
} else {
r.schedule = nil
}
}
// store the certificate hash for status reconciliation
r.hash = reconcilerutil.Hash(certBytes)
//store certificate validity times for status reconciliation
r.notBefore = &cert.NotBefore
r.notAfter = &cert.NotAfter
return ctrl.Result{}, nil
}