-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificaterequest_controller.go
160 lines (136 loc) · 6.52 KB
/
certificaterequest_controller.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
"github.com/go-logr/logr"
apiutil "github.com/jetstack/cert-manager/pkg/api/util"
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
api "github.com/awspca-issuer/api/v1alpha2"
"github.com/awspca-issuer/provisioners"
core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// CertificateRequestReconciler reconciles a AWSPCAIssuer object.
type CertificateRequestReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
}
// +kubebuilder:rbac:groups=cert-manager.io,resources=certificaterequests,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=cert-manager.io,resources=certificaterequests/status,verbs=get;update;patch
// Reconcile will read and validate a AWSPCAIssuer resource associated to the
// CertificateRequest resource, and it will sign the CertificateRequest with the
// provisioner in the AWSPCAIssuer.
func (r *CertificateRequestReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("certificaterequest", req.NamespacedName)
// Fetch the CertificateRequest resource being reconciled.
// Just ignore the request if the certificate request has been deleted.
cr := new(cmapi.CertificateRequest)
if err := r.Client.Get(ctx, req.NamespacedName, cr); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
log.Error(err, "failed to retrieve CertificateRequest resource")
return ctrl.Result{}, err
}
// Check the CertificateRequest's issuerRef and if it does not match the api
// group name, log a message at a debug level and stop processing.
if cr.Spec.IssuerRef.Group != "" && cr.Spec.IssuerRef.Group != api.GroupVersion.Group {
log.V(4).Info("resource does not specify an issuerRef group name that we are responsible for", "group", cr.Spec.IssuerRef.Group)
return ctrl.Result{}, nil
}
// If the certificate data is already set then we skip this request as it
// has already been completed in the past.
if len(cr.Status.Certificate) > 0 {
log.V(4).Info("existing certificate data found in status, skipping already completed CertificateRequest")
return ctrl.Result{}, nil
}
if cr.Spec.IsCA {
log.Info("AWSPCA certificate does not support online signing of CA certificates")
return ctrl.Result{}, nil
}
// Fetch the AWSPCAIssuer resource
iss := api.AWSPCAIssuer{}
issNamespaceName := types.NamespacedName{
Namespace: req.Namespace,
Name: cr.Spec.IssuerRef.Name,
}
if err := r.Client.Get(ctx, issNamespaceName, &iss); err != nil {
log.Error(err, "failed to retrieve AWSPCAIssuer resource", "namespace", req.Namespace, "name", cr.Spec.IssuerRef.Name)
_ = r.setStatus(ctx, cr, cmmeta.ConditionFalse, cmapi.CertificateRequestReasonPending, "Failed to retrieve AWSPCAIssuer resource %s: %v", issNamespaceName, err)
return ctrl.Result{}, err
}
// Check if the AWSPCAIssuer resource has been marked Ready
if !AWSPCAIssuerHasCondition(iss, api.AWSPCAIssuerCondition{Type: api.ConditionReady, Status: api.ConditionTrue}) {
err := fmt.Errorf("resource %s is not ready", issNamespaceName)
log.Error(err, "failed to retrieve AWSPCAIssuer resource", "namespace", req.Namespace, "name", cr.Spec.IssuerRef.Name)
_ = r.setStatus(ctx, cr, cmmeta.ConditionFalse, cmapi.CertificateRequestReasonPending, "AWSPCAIssuer resource %s is not Ready", issNamespaceName)
return ctrl.Result{}, err
}
// Load the provisioner that will sign the CertificateRequest
provisioner, ok := provisioners.Load(issNamespaceName)
if !ok {
err := fmt.Errorf("provisioner %s not found", issNamespaceName)
log.Error(err, "failed to provisioner for AWSPCAIssuer resource")
_ = r.setStatus(ctx, cr, cmmeta.ConditionFalse, cmapi.CertificateRequestReasonPending, "Failed to load provisioner for AWSPCAIssuer resource %s", issNamespaceName)
return ctrl.Result{}, err
}
// Sign CertificateRequest
signedPEM, _, err := provisioner.Sign(ctx, cr)
if err != nil {
log.Error(err, "failed to sign certificate request")
return ctrl.Result{}, r.setStatus(ctx, cr, cmmeta.ConditionFalse, cmapi.CertificateRequestReasonFailed, "Failed to sign certificate request: %v", err)
}
cr.Status.Certificate = signedPEM
//cr.Status.CA = trustedCAs
return ctrl.Result{}, r.setStatus(ctx, cr, cmmeta.ConditionTrue, cmapi.CertificateRequestReasonIssued, "Certificate issued")
}
// SetupWithManager initializes the CertificateRequest controller into the
// controller runtime.
func (r *CertificateRequestReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&cmapi.CertificateRequest{}).
Complete(r)
}
// AWSPCAIssuerHasCondition will return true if the given AWSPCAIssuer resource has
// a condition matching the provided AWSPCAIssuerCondition. Only the Type and
// Status field will be used in the comparison, meaning that this function will
// return 'true' even if the Reason, Message and LastTransitionTime fields do
// not match.
func AWSPCAIssuerHasCondition(iss api.AWSPCAIssuer, c api.AWSPCAIssuerCondition) bool {
existingConditions := iss.Status.Conditions
for _, cond := range existingConditions {
if c.Type == cond.Type && c.Status == cond.Status {
return true
}
}
return false
}
func (r *CertificateRequestReconciler) setStatus(ctx context.Context, cr *cmapi.CertificateRequest, status cmmeta.ConditionStatus, reason, message string, args ...interface{}) error {
completeMessage := fmt.Sprintf(message, args...)
apiutil.SetCertificateRequestCondition(cr, cmapi.CertificateRequestConditionReady, status, reason, completeMessage)
// Fire an Event to additionally inform users of the change
eventType := core.EventTypeNormal
if status == cmmeta.ConditionFalse {
eventType = core.EventTypeWarning
}
r.Recorder.Event(cr, eventType, reason, completeMessage)
return r.Client.Status().Update(ctx, cr)
}