|
5 | 5 | "context" |
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
8 | | - "strings" |
9 | 8 | "time" |
10 | 9 |
|
11 | 10 | "dario.cat/mergo" |
@@ -596,81 +595,35 @@ func (b *Backend) CleanOrphanedResources(ctx context.Context) { |
596 | 595 | namespace := b.conf.Kubernetes.JobsNamespace |
597 | 596 | taskIDs := make(map[string]struct{}) |
598 | 597 |
|
599 | | - // Collect task IDs from each resource type |
600 | | - if pvcs, err := b.client.CoreV1().PersistentVolumeClaims(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}); err == nil { |
601 | | - if err != nil { |
602 | | - b.log.Error("backlog cleanup: listing PVCs", err) |
603 | | - } |
604 | | - for _, r := range pvcs.Items { |
605 | | - if id, ok := r.Labels["taskId"]; ok { |
606 | | - taskIDs[id] = struct{}{} |
607 | | - } |
608 | | - } |
609 | | - } |
| 598 | + // Collect task IDs from resources that cleanResources manages directly. |
| 599 | + // ConfigMaps, PVCs, Roles, and RoleBindings are now owned by the Job via ownerReferences |
| 600 | + // and are garbage-collected by Kubernetes automatically — they are intentionally excluded here. |
610 | 601 |
|
611 | | - // PVs |
612 | | - if pvs, err := b.client.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("app=funnel,namespace=%s", namespace)}); err == nil { |
613 | | - if err != nil { |
614 | | - b.log.Error("backlog cleanup: listing PVs", err) |
615 | | - } |
| 602 | + // PVs (cluster-scoped; cannot be owned by a namespaced Job, so must be cleaned explicitly) |
| 603 | + pvs, err := b.client.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("app=funnel,namespace=%s", namespace)}) |
| 604 | + if err != nil { |
| 605 | + b.log.Error("backlog cleanup: listing PVs", err) |
| 606 | + } else { |
616 | 607 | for _, r := range pvs.Items { |
617 | 608 | if id, ok := r.Labels["taskId"]; ok { |
618 | 609 | taskIDs[id] = struct{}{} |
619 | 610 | } |
620 | 611 | } |
621 | 612 | } |
622 | 613 |
|
623 | | - // ConfigMaps |
624 | | - if cms, err := b.client.CoreV1().ConfigMaps(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}); err == nil { |
625 | | - if err != nil { |
626 | | - b.log.Error("backlog cleanup: listing ConfigMaps", err) |
627 | | - } |
628 | | - const cmPrefix = "funnel-worker-config-" |
629 | | - for _, r := range cms.Items { |
630 | | - if id, ok := r.Labels["taskId"]; ok { |
631 | | - taskIDs[id] = struct{}{} |
632 | | - } else if strings.HasPrefix(r.Name, cmPrefix) { |
633 | | - taskIDs[strings.TrimPrefix(r.Name, cmPrefix)] = struct{}{} |
634 | | - } |
635 | | - } |
636 | | - } |
637 | | - |
638 | | - // ServiceAccounts |
639 | | - if sas, err := b.client.CoreV1().ServiceAccounts(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}); err == nil { |
640 | | - if err != nil { |
641 | | - b.log.Error("backlog cleanup: listing ServiceAccounts", err) |
642 | | - } |
| 614 | + // ServiceAccounts (shared SAs are not owned by a Job; task-scoped SAs may also be orphaned |
| 615 | + // if they were created before ownerRef support was added) |
| 616 | + sas, err := b.client.CoreV1().ServiceAccounts(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}) |
| 617 | + if err != nil { |
| 618 | + b.log.Error("backlog cleanup: listing ServiceAccounts", err) |
| 619 | + } else { |
643 | 620 | for _, r := range sas.Items { |
644 | 621 | if id, ok := r.Labels["taskId"]; ok { |
645 | 622 | taskIDs[id] = struct{}{} |
646 | 623 | } |
647 | 624 | } |
648 | 625 | } |
649 | 626 |
|
650 | | - // Roles |
651 | | - if roles, err := b.client.RbacV1().Roles(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}); err == nil { |
652 | | - if err != nil { |
653 | | - b.log.Error("backlog cleanup: listing Roles", err) |
654 | | - } |
655 | | - for _, r := range roles.Items { |
656 | | - if id, ok := r.Labels["taskId"]; ok { |
657 | | - taskIDs[id] = struct{}{} |
658 | | - } |
659 | | - } |
660 | | - } |
661 | | - |
662 | | - // RoleBindings |
663 | | - if rbs, err := b.client.RbacV1().RoleBindings(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=funnel"}); err == nil { |
664 | | - if err != nil { |
665 | | - b.log.Error("backlog cleanup: listing RoleBindings", err) |
666 | | - } |
667 | | - for _, r := range rbs.Items { |
668 | | - if id, ok := r.Labels["taskId"]; ok { |
669 | | - taskIDs[id] = struct{}{} |
670 | | - } |
671 | | - } |
672 | | - } |
673 | | - |
674 | 627 | // TODO: Add Executor Jobs here beacause orphaned tasks can result in orphaned jobs |
675 | 628 |
|
676 | 629 | for taskID := range taskIDs { |
|
0 commit comments