Skip to content

Commit 0923d01

Browse files
committed
Improve failure messages when CertificateRequests are left pending
Signed-off-by: James Munnelly <[email protected]>
1 parent 4d748d6 commit 0923d01

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

manager/manager.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ func (m *Manager) issue(ctx context.Context, volumeID string) error {
286286
log.Info("Created new CertificateRequest resource")
287287

288288
// Poll every 1s for the CertificateRequest to be ready
289+
lastFailureReason := ""
289290
if err := wait.PollUntil(time.Second, func() (done bool, err error) {
290291
updatedReq, err := m.lister.CertificateRequests(req.Namespace).Get(req.Name)
291292
if apierrors.IsNotFound(err) {
@@ -304,11 +305,25 @@ func (m *Manager) issue(ctx context.Context, volumeID string) error {
304305
// Handle cases where the request has been explicitly denied
305306
if apiutil.CertificateRequestIsDenied(updatedReq) {
306307
cond := apiutil.GetCertificateRequestCondition(updatedReq, cmapi.CertificateRequestConditionDenied)
308+
// if a CR has been explicitly denied, we DO stop execution.
309+
// there may be a case to be made that we could continue anyway even if the issuer ignores the approval
310+
// status, however these cases are likely few and far between and this makes denial more responsive.
307311
return false, fmt.Errorf("request %q has been denied by the approval plugin: %s", updatedReq.Name, cond.Message)
308312
}
309313

314+
if !apiutil.CertificateRequestIsApproved(updatedReq) {
315+
lastFailureReason = "request has not yet been approved by approval plugin"
316+
// we don't stop execution here, as some versions of cert-manager (and some external issuer plugins)
317+
// may not be aware/utilise approval.
318+
// If the certificate is still issued despite never being approved, the CSI driver should continue
319+
// and use the issued certificate despite not being approved.
320+
}
321+
310322
readyCondition := apiutil.GetCertificateRequestCondition(updatedReq, cmapi.CertificateRequestConditionReady)
311323
if readyCondition == nil {
324+
if apiutil.CertificateRequestIsApproved(updatedReq) {
325+
lastFailureReason = "request has no ready condition"
326+
}
312327
log.V(2).Info("CertificateRequest is still pending")
313328
// Issuance is still pending
314329
return false, nil
@@ -320,9 +335,11 @@ func (m *Manager) issue(ctx context.Context, volumeID string) error {
320335
case cmapi.CertificateRequestReasonFailed:
321336
return false, fmt.Errorf("request %q has failed: %s", updatedReq.Name, readyCondition.Message)
322337
case cmapi.CertificateRequestReasonPending:
338+
lastFailureReason = fmt.Sprintf("request pending: %v", readyCondition.Message)
323339
log.V(2).Info("CertificateRequest is still pending")
324340
return false, nil
325341
default:
342+
lastFailureReason = fmt.Sprintf("unrecognised Ready condition state (%s): %s", readyCondition.Reason, readyCondition.Message)
326343
log.Info("unrecognised state for Ready condition", "request_namespace", updatedReq.Namespace, "request_name", updatedReq.Name, "condition", *readyCondition)
327344
return false, nil
328345
}
@@ -333,6 +350,10 @@ func (m *Manager) issue(ctx context.Context, volumeID string) error {
333350
req = updatedReq
334351
return true, nil
335352
}, ctx.Done()); err != nil {
353+
if errors.Is(err, wait.ErrWaitTimeout) {
354+
// try and return a more helpful error message than "timed out waiting for the condition"
355+
return fmt.Errorf("waiting for request: %s", lastFailureReason)
356+
}
336357
return fmt.Errorf("waiting for request: %w", err)
337358
}
338359

0 commit comments

Comments
 (0)