|
5 | 5 | "crypto/x509" |
6 | 6 | "encoding/pem" |
7 | 7 | "fmt" |
8 | | - "log" |
9 | 8 | "net/netip" |
10 | 9 | "sort" |
11 | 10 | "strconv" |
@@ -299,53 +298,31 @@ func (r *IngressClassReconciler) loadCerts( |
299 | 298 | return false, certificateIDs, nil |
300 | 299 | } |
301 | 300 |
|
302 | | -// cleanupCerts deletes the certificates from the Certificates API that are no longer associated with any Ingress in the IngressClass |
303 | | -func (r *IngressClassReconciler) cleanupCerts(ctx context.Context, ingressClass *networkingv1.IngressClass, ingresses []*networkingv1.Ingress) error { |
304 | | - // Prepare a map of secret names that are currently being used by the ingresses |
305 | | - usedSecrets := map[string]bool{} |
306 | | - for _, ingress := range ingresses { |
307 | | - for _, tls := range ingress.Spec.TLS { |
308 | | - if tls.SecretName == "" { |
309 | | - continue |
310 | | - } |
311 | | - // Retrieve the TLS Secret |
312 | | - tlsSecret := &corev1.Secret{} |
313 | | - err := r.Client.Get(ctx, types.NamespacedName{Namespace: ingress.Namespace, Name: tls.SecretName}, tlsSecret) |
314 | | - if err != nil { |
315 | | - log.Printf("failed to get TLS secret %s: %v", tls.SecretName, err) |
316 | | - continue |
317 | | - } |
318 | | - certName := getCertName(ingressClass, ingress, tlsSecret) |
319 | | - usedSecrets[certName] = true |
320 | | - } |
321 | | - } |
322 | | - |
323 | | - certificatesList, err := r.CertificateClient.ListCertificate(ctx, r.ProjectID, r.Region) |
324 | | - if err != nil { |
325 | | - return fmt.Errorf("failed to list certificates: %w", err) |
326 | | - } |
327 | | - |
328 | | - if certificatesList == nil || certificatesList.Items == nil { |
329 | | - return nil // No certificates to clean up |
330 | | - } |
331 | | - for _, cert := range certificatesList.Items { |
332 | | - certID := *cert.Id |
333 | | - certName := *cert.Name |
334 | | - |
335 | | - // The certificatesList contains all certificates in the project, so we need to filter them by the ALB IngressClass UID. |
336 | | - if !strings.HasPrefix(certName, generateShortUID(ingressClass.UID)) { |
337 | | - continue |
338 | | - } |
339 | | - |
340 | | - // If the tls secret is no longer in referenced, delete the certificate |
341 | | - if _, inUse := usedSecrets[certName]; !inUse { |
342 | | - err := r.CertificateClient.DeleteCertificate(ctx, r.ProjectID, r.Region, certID) |
343 | | - if err != nil { |
344 | | - return fmt.Errorf("failed to delete certificate %s: %v", certName, err) |
345 | | - } |
346 | | - } |
347 | | - } |
348 | | - return nil |
| 301 | +// cleanupCerts deletes all certificates from the Certificates API that are associated with this IngressClass. |
| 302 | +func (r *IngressClassReconciler) cleanupCerts(ctx context.Context, ingressClass *networkingv1.IngressClass) error { |
| 303 | + // We use the IngressClass UID to identify certificates for this specific class. |
| 304 | + // A shortened version is used because that is how the names were generated on creation. |
| 305 | + // Note: While a UID collision between clusters is technically possible, it is almost impossible in practice. |
| 306 | + classPrefix := generateShortUID(ingressClass.UID) |
| 307 | + |
| 308 | + certificatesList, err := r.CertificateClient.ListCertificate(ctx, r.ProjectID, r.Region) |
| 309 | + if err != nil { |
| 310 | + return fmt.Errorf("failed to list certificates: %w", err) |
| 311 | + } |
| 312 | + |
| 313 | + if certificatesList == nil || certificatesList.Items == nil { |
| 314 | + return nil // No certificates to clean up |
| 315 | + } |
| 316 | + |
| 317 | + for _, cert := range certificatesList.Items { |
| 318 | + if strings.HasPrefix(*cert.Name, classPrefix) { |
| 319 | + err := r.CertificateClient.DeleteCertificate(ctx, r.ProjectID, r.Region, *cert.Id) |
| 320 | + if err != nil { |
| 321 | + return fmt.Errorf("failed to delete orphaned certificate %s: %v", *cert.Name, err) |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + return nil |
349 | 326 | } |
350 | 327 |
|
351 | 328 | // isCertReady checks if the certificate chain is complete (leaf + intermediates). |
|
0 commit comments