Skip to content

Fix: Add garbage collection to handle Approved-Unissued CSRs #133116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pkg/controller/certificates/cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ func (ccc *CSRCleanerController) worker(ctx context.Context) {

func (ccc *CSRCleanerController) handle(ctx context.Context, csr *capi.CertificateSigningRequest) error {
logger := klog.FromContext(ctx)
if isIssuedPastDeadline(logger, csr) || isDeniedPastDeadline(logger, csr) || isFailedPastDeadline(logger, csr) || isPendingPastDeadline(logger, csr) || isIssuedExpired(logger, csr) {
if isIssuedPastDeadline(logger, csr) ||
isDeniedPastDeadline(logger, csr) ||
isFailedPastDeadline(logger, csr) ||
isPendingPastDeadline(logger, csr) ||
isIssuedExpired(logger, csr) ||
isApprovedUnissuedPastDeadline(logger, csr) {
if err := ccc.csrClient.Delete(ctx, csr.Name, metav1.DeleteOptions{}); err != nil {
return fmt.Errorf("unable to delete CSR %q: %v", csr.Name, err)
}
Expand Down Expand Up @@ -179,6 +184,19 @@ func isIssuedPastDeadline(logger klog.Logger, csr *capi.CertificateSigningReques
return false
}

// isApprovedUnissuedPastDeadline checks if the certificate has an Approved status but
// no certificate has been issued, and the approval time has passed the deadline
// that pending requests are maintained for.
func isApprovedUnissuedPastDeadline(logger klog.Logger, csr *capi.CertificateSigningRequest) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a pre-existing issue with some of the other check functions, but let's improve this to hoist the checks that don't change out of the loop, e.g.:

func isApprovedUnissuedPastDeadline(logger klog.Logger, csr *capi.CertificateSigningRequest) bool {
  if isIssued(csr) {
    return false
  }
	for _, c := range csr.Status.Conditions {
		if c.Type == capi.CertificateApproved && isOlderThan(c.LastUpdateTime, pendingExpiration) {
      ...

can make the same change to isIssuedPastDeadline

func isIssuedPastDeadline(logger klog.Logger, csr *capi.CertificateSigningRequest) bool {
	if !isIssued(csr) {
		return false
	}
	for _, c := range csr.Status.Conditions {
		if c.Type == capi.CertificateApproved && isOlderThan(c.LastUpdateTime, approvedExpiration) {

for _, c := range csr.Status.Conditions {
if c.Type == capi.CertificateApproved && !(isIssued(csr) && isOlderThan(c.LastUpdateTime, pendingExpiration)) {
logger.Info("Cleaning CSR as it is approved but unissued for more than pendingExpiration duration.", "csr", csr.Name, "pendingExpiration", pendingExpiration)
return true
}
}
return false
}

// isOlderThan checks that t is a non-zero and older than d from time.Now().
func isOlderThan(t metav1.Time, d time.Duration) bool {
return !t.IsZero() && time.Since(t.Time) > d
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/certificates/cleaner/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ func TestCleanerWithApprovedExpiredCSR(t *testing.T) {
[]capi.CertificateSigningRequestCondition{},
[]string{"delete"},
},
{
"delete approved unissued past deadline",
metav1.NewTime(time.Now().Add(-1 * time.Minute)),
nil,
[]capi.CertificateSigningRequestCondition{
{
Type: capi.CertificateApproved,
LastUpdateTime: metav1.NewTime(time.Now().Add(-25 * time.Hour)),
},
},
[]string{"delete"},
},
{
"no delete approved unissued not past deadline",
metav1.NewTime(time.Now().Add(-1 * time.Minute)),
nil,
[]capi.CertificateSigningRequestCondition{
{
Type: capi.CertificateApproved,
LastUpdateTime: metav1.NewTime(time.Now().Add(-5 * time.Hour)),
},
},
[]string{},
},
{
"no delete approved not passed deadline unexpired",
metav1.NewTime(time.Now().Add(-1 * time.Minute)),
Expand Down