@@ -20,11 +20,12 @@ import (
20
20
"fmt"
21
21
22
22
"github.com/go-logr/logr"
23
+ "k8s.io/utils/clock"
23
24
ctrl "sigs.k8s.io/controller-runtime"
24
25
"sigs.k8s.io/controller-runtime/pkg/client"
25
26
26
27
cmapiutil "github.com/jetstack/cert-manager/pkg/api/util"
27
- cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2 "
28
+ cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1 "
28
29
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
29
30
api "github.com/nokia/adcs-issuer/api/v1"
30
31
core "k8s.io/api/core/v1"
@@ -38,6 +39,9 @@ type CertificateRequestReconciler struct {
38
39
client.Client
39
40
Log logr.Logger
40
41
Recorder record.EventRecorder
42
+
43
+ Clock clock.Clock
44
+ CheckApprovedCondition bool
41
45
}
42
46
43
47
var (
48
52
// +kubebuilder:rbac:groups=cert-manager.io,resources=certificaterequests/status,verbs=get;update;patch
49
53
// +kubebuilder:rbac:groups="",resources=events,verbs=patch
50
54
51
- func (r * CertificateRequestReconciler ) Reconcile (req ctrl.Request ) (ctrl.Result , error ) {
52
- ctx := context .Background ()
55
+ func (r * CertificateRequestReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
53
56
log := r .Log .WithValues ("certificaterequest" , req .NamespacedName )
54
57
55
58
// your logic here
@@ -71,6 +74,55 @@ func (r *CertificateRequestReconciler) Reconcile(req ctrl.Request) (ctrl.Result,
71
74
return ctrl.Result {}, nil
72
75
}
73
76
77
+ // Ignore CertificateRequest if it is already Ready
78
+ if cmapiutil .CertificateRequestHasCondition (& cr , cmapi.CertificateRequestCondition {
79
+ Type : cmapi .CertificateRequestConditionReady ,
80
+ Status : cmmeta .ConditionTrue ,
81
+ }) {
82
+ log .V (4 ).Info ("CertificateRequest is Ready. Ignoring." )
83
+ return ctrl.Result {}, nil
84
+ }
85
+ // Ignore CertificateRequest if it is already Failed
86
+ if cmapiutil .CertificateRequestHasCondition (& cr , cmapi.CertificateRequestCondition {
87
+ Type : cmapi .CertificateRequestConditionReady ,
88
+ Status : cmmeta .ConditionFalse ,
89
+ Reason : cmapi .CertificateRequestReasonFailed ,
90
+ }) {
91
+ log .V (4 ).Info ("CertificateRequest is Failed. Ignoring." )
92
+ return ctrl.Result {}, nil
93
+ }
94
+ // Ignore CertificateRequest if it already has a Denied Ready Reason
95
+ if cmapiutil .CertificateRequestHasCondition (& cr , cmapi.CertificateRequestCondition {
96
+ Type : cmapi .CertificateRequestConditionReady ,
97
+ Status : cmmeta .ConditionFalse ,
98
+ Reason : cmapi .CertificateRequestReasonDenied ,
99
+ }) {
100
+ log .V (4 ).Info ("CertificateRequest already has a Ready condition with Denied Reason. Ignoring." )
101
+ return ctrl.Result {}, nil
102
+ }
103
+
104
+ // If CertificateRequest has been denied, mark the CertificateRequest as
105
+ // Ready=Denied and set FailureTime if not already.
106
+ if cmapiutil .CertificateRequestIsDenied (& cr ) {
107
+ log .V (4 ).Info ("CertificateRequest has been denied. Marking as failed." )
108
+
109
+ if cr .Status .FailureTime == nil {
110
+ nowTime := metav1 .NewTime (r .Clock .Now ())
111
+ cr .Status .FailureTime = & nowTime
112
+ }
113
+
114
+ message := "The CertificateRequest was denied by an approval controller"
115
+ return ctrl.Result {}, r .SetStatus (ctx , & cr , cmmeta .ConditionFalse , cmapi .CertificateRequestReasonDenied , message )
116
+ }
117
+
118
+ if r .CheckApprovedCondition {
119
+ // If CertificateRequest has not been approved, exit early.
120
+ if ! cmapiutil .CertificateRequestIsApproved (& cr ) {
121
+ log .V (4 ).Info ("certificate request has not been approved" )
122
+ return ctrl.Result {}, nil
123
+ }
124
+ }
125
+
74
126
// If the certificate data is already set then we skip this request as it
75
127
// has already been completed in the past.
76
128
if len (cr .Status .Certificate ) > 0 {
@@ -116,7 +168,7 @@ func (r *CertificateRequestReconciler) Reconcile(req ctrl.Request) (ctrl.Result,
116
168
117
169
func (r * CertificateRequestReconciler ) createAdcsRequest (ctx context.Context , cmRequest * cmapi.CertificateRequest ) error {
118
170
spec := api.AdcsRequestSpec {
119
- CSRPEM : cmRequest .Spec .CSRPEM ,
171
+ CSRPEM : cmRequest .Spec .Request ,
120
172
IssuerRef : cmRequest .Spec .IssuerRef ,
121
173
}
122
174
return r .Create (ctx , & api.AdcsRequest {
@@ -137,7 +189,7 @@ func (r *CertificateRequestReconciler) SetupWithManager(mgr ctrl.Manager) error
137
189
138
190
func RequestDiffers (adcsReq * api.AdcsRequest , certReq * cmapi.CertificateRequest ) bool {
139
191
a := adcsReq .Spec .CSRPEM
140
- b := certReq .Spec .CSRPEM
192
+ b := certReq .Spec .Request
141
193
if len (a ) != len (b ) {
142
194
return true
143
195
}
0 commit comments