refactor(app): split cleanupOrphanDomainResources into focused helpers
Extract removeOrphanVolumes, removeOrphanNetwork, removeOrphanRoute to flatten nesting and improve readability. Use errors.Join to collect failures.
- commit
- 9209a14c3a5ffef86850e294b891d5331a0ae91c
- author
- brice · · 2026-03-25 08:06 UTC
- parent
- 8b31c1c
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +38 -27
M internal/app/run.go +38 -27
| @@ -673,44 +673,55 @@ func gcOrphanedPreviews(ctx context.Context, svc *services, previewConfig domain | |||
| 673 | 673 | func cleanupOrphanDomainResources(ctx context.Context, svc *services, orphanDomain string) error { | |
| 674 | 674 | log := zerowrap.FromCtx(ctx) | |
| 675 | 675 | domainSanitized := strings.ReplaceAll(orphanDomain, ".", "-") | |
| 676 | - | var cleanupErr error | |
| 676 | + | var errs []error | |
| 677 | 677 | ||
| 678 | - | // Remove volumes matching domain prefix (convention: {volPrefix}-{domain-sanitized}-). | |
| 678 | + | errs = append(errs, removeOrphanVolumes(ctx, svc, domainSanitized, log)...) | |
| 679 | + | errs = append(errs, removeOrphanNetwork(ctx, svc, domainSanitized, log)) | |
| 680 | + | errs = append(errs, removeOrphanRoute(ctx, svc, orphanDomain, log)) | |
| 681 | + | svc.proxySvc.InvalidateTarget(ctx, orphanDomain) | |
| 682 | + | ||
| 683 | + | return errors.Join(errs...) | |
| 684 | + | } | |
| 685 | + | ||
| 686 | + | func removeOrphanVolumes(ctx context.Context, svc *services, domainSanitized string, log zerowrap.Logger) []error { | |
| 679 | 687 | _, volPrefix, _ := svc.configSvc.GetVolumeConfig() | |
| 680 | - | fullVolumePrefix := volPrefix + "-" + domainSanitized + "-" | |
| 688 | + | prefix := volPrefix + "-" + domainSanitized + "-" | |
| 689 | + | ||
| 681 | 690 | volumes, err := svc.runtime.ListVolumes(ctx) | |
| 682 | 691 | if err != nil { | |
| 683 | 692 | log.Warn().Err(err).Msg("failed to list volumes for orphan cleanup") | |
| 684 | - | cleanupErr = err | |
| 685 | - | } else { | |
| 686 | - | for _, v := range volumes { | |
| 687 | - | if strings.HasPrefix(v.Name, fullVolumePrefix) { | |
| 688 | - | if err := svc.runtime.RemoveVolume(ctx, v.Name, true); err != nil { | |
| 689 | - | log.Warn().Err(err).Str("volume", v.Name).Msg("failed to remove orphan volume") | |
| 690 | - | cleanupErr = err | |
| 691 | - | } | |
| 692 | - | } | |
| 693 | - | } | |
| 693 | + | return []error{err} | |
| 694 | 694 | } | |
| 695 | 695 | ||
| 696 | - | // Remove network (convention: {networkPrefix}-{domain-sanitized}). | |
| 697 | - | networkPrefix := svc.configSvc.GetNetworkPrefix() | |
| 698 | - | networkName := networkPrefix + "-" + domainSanitized | |
| 699 | - | if err := svc.runtime.RemoveNetwork(ctx, networkName); err != nil { | |
| 700 | - | log.Warn().Err(err).Str("network", networkName).Msg("failed to remove orphan network") | |
| 701 | - | cleanupErr = err | |
| 696 | + | var errs []error | |
| 697 | + | for _, v := range volumes { | |
| 698 | + | if !strings.HasPrefix(v.Name, prefix) { | |
| 699 | + | continue | |
| 700 | + | } | |
| 701 | + | if err := svc.runtime.RemoveVolume(ctx, v.Name, true); err != nil { | |
| 702 | + | log.Warn().Err(err).Str("volume", v.Name).Msg("failed to remove orphan volume") | |
| 703 | + | errs = append(errs, err) | |
| 704 | + | } | |
| 702 | 705 | } | |
| 706 | + | return errs | |
| 707 | + | } | |
| 703 | 708 | ||
| 704 | - | // Remove route and invalidate proxy. | |
| 705 | - | if err := svc.configSvc.RemoveRoute(ctx, orphanDomain); err != nil { | |
| 706 | - | if !errors.Is(err, domain.ErrRouteNotFound) { | |
| 707 | - | log.Warn().Err(err).Str("domain", orphanDomain).Msg("failed to remove orphan route") | |
| 708 | - | cleanupErr = err | |
| 709 | - | } | |
| 709 | + | func removeOrphanNetwork(ctx context.Context, svc *services, domainSanitized string, log zerowrap.Logger) error { | |
| 710 | + | name := svc.configSvc.GetNetworkPrefix() + "-" + domainSanitized | |
| 711 | + | if err := svc.runtime.RemoveNetwork(ctx, name); err != nil { | |
| 712 | + | log.Warn().Err(err).Str("network", name).Msg("failed to remove orphan network") | |
| 713 | + | return err | |
| 710 | 714 | } | |
| 711 | - | svc.proxySvc.InvalidateTarget(ctx, orphanDomain) | |
| 715 | + | return nil | |
| 716 | + | } | |
| 712 | 717 | ||
| 713 | - | return cleanupErr | |
| 718 | + | func removeOrphanRoute(ctx context.Context, svc *services, orphanDomain string, log zerowrap.Logger) error { | |
| 719 | + | err := svc.configSvc.RemoveRoute(ctx, orphanDomain) | |
| 720 | + | if err == nil || errors.Is(err, domain.ErrRouteNotFound) { | |
| 721 | + | return nil | |
| 722 | + | } | |
| 723 | + | log.Warn().Err(err).Str("domain", orphanDomain).Msg("failed to remove orphan route") | |
| 724 | + | return err | |
| 714 | 725 | } | |
| 715 | 726 | ||
| 716 | 727 | // injectTelemetryMetrics creates and injects OTel metrics into services when | |
| @@ -673,44 +673,55 @@ func gcOrphanedPreviews(ctx context.Context, svc *services, previewConfig domain | |||||
| 673 | func cleanupOrphanDomainResources(ctx context.Context, svc *services, orphanDomain string) error { | 673 | func cleanupOrphanDomainResources(ctx context.Context, svc *services, orphanDomain string) error { | ||
| 674 | log := zerowrap.FromCtx(ctx) | 674 | log := zerowrap.FromCtx(ctx) | ||
| 675 | domainSanitized := strings.ReplaceAll(orphanDomain, ".", "-") | 675 | domainSanitized := strings.ReplaceAll(orphanDomain, ".", "-") | ||
| 676 | - | var cleanupErr error | 676 | + | var errs []error |
| 677 | 677 | ||||
| 678 | - | // Remove volumes matching domain prefix (convention: {volPrefix}-{domain-sanitized}-). | 678 | + | errs = append(errs, removeOrphanVolumes(ctx, svc, domainSanitized, log)...) |
| 679 | + | errs = append(errs, removeOrphanNetwork(ctx, svc, domainSanitized, log)) | |||
| 680 | + | errs = append(errs, removeOrphanRoute(ctx, svc, orphanDomain, log)) | |||
| 681 | + | svc.proxySvc.InvalidateTarget(ctx, orphanDomain) | |||
| 682 | + | ||||
| 683 | + | return errors.Join(errs...) | |||
| 684 | + | } | |||
| 685 | + | ||||
| 686 | + | func removeOrphanVolumes(ctx context.Context, svc *services, domainSanitized string, log zerowrap.Logger) []error { | |||
| 679 | _, volPrefix, _ := svc.configSvc.GetVolumeConfig() | 687 | _, volPrefix, _ := svc.configSvc.GetVolumeConfig() | ||
| 680 | - | fullVolumePrefix := volPrefix + "-" + domainSanitized + "-" | 688 | + | prefix := volPrefix + "-" + domainSanitized + "-" |
| 689 | + | ||||
| 681 | volumes, err := svc.runtime.ListVolumes(ctx) | 690 | volumes, err := svc.runtime.ListVolumes(ctx) | ||
| 682 | if err != nil { | 691 | if err != nil { | ||
| 683 | log.Warn().Err(err).Msg("failed to list volumes for orphan cleanup") | 692 | log.Warn().Err(err).Msg("failed to list volumes for orphan cleanup") | ||
| 684 | - | cleanupErr = err | 693 | + | return []error{err} |
| 685 | - | } else { | |||
| 686 | - | for _, v := range volumes { | |||
| 687 | - | if strings.HasPrefix(v.Name, fullVolumePrefix) { | |||
| 688 | - | if err := svc.runtime.RemoveVolume(ctx, v.Name, true); err != nil { | |||
| 689 | - | log.Warn().Err(err).Str("volume", v.Name).Msg("failed to remove orphan volume") | |||
| 690 | - | cleanupErr = err | |||
| 691 | - | } | |||
| 692 | - | } | |||
| 693 | - | } | |||
| 694 | } | 694 | } | ||
| 695 | 695 | ||||
| 696 | - | // Remove network (convention: {networkPrefix}-{domain-sanitized}). | 696 | + | var errs []error |
| 697 | - | networkPrefix := svc.configSvc.GetNetworkPrefix() | 697 | + | for _, v := range volumes { |
| 698 | - | networkName := networkPrefix + "-" + domainSanitized | 698 | + | if !strings.HasPrefix(v.Name, prefix) { |
| 699 | - | if err := svc.runtime.RemoveNetwork(ctx, networkName); err != nil { | 699 | + | continue |
| 700 | - | log.Warn().Err(err).Str("network", networkName).Msg("failed to remove orphan network") | 700 | + | } |
| 701 | - | cleanupErr = err | 701 | + | if err := svc.runtime.RemoveVolume(ctx, v.Name, true); err != nil { |
| 702 | + | log.Warn().Err(err).Str("volume", v.Name).Msg("failed to remove orphan volume") | |||
| 703 | + | errs = append(errs, err) | |||
| 704 | + | } | |||
| 702 | } | 705 | } | ||
| 706 | + | return errs | |||
| 707 | + | } | |||
| 703 | 708 | ||||
| 704 | - | // Remove route and invalidate proxy. | 709 | + | func removeOrphanNetwork(ctx context.Context, svc *services, domainSanitized string, log zerowrap.Logger) error { |
| 705 | - | if err := svc.configSvc.RemoveRoute(ctx, orphanDomain); err != nil { | 710 | + | name := svc.configSvc.GetNetworkPrefix() + "-" + domainSanitized |
| 706 | - | if !errors.Is(err, domain.ErrRouteNotFound) { | 711 | + | if err := svc.runtime.RemoveNetwork(ctx, name); err != nil { |
| 707 | - | log.Warn().Err(err).Str("domain", orphanDomain).Msg("failed to remove orphan route") | 712 | + | log.Warn().Err(err).Str("network", name).Msg("failed to remove orphan network") |
| 708 | - | cleanupErr = err | 713 | + | return err |
| 709 | - | } | |||
| 710 | } | 714 | } | ||
| 711 | - | svc.proxySvc.InvalidateTarget(ctx, orphanDomain) | 715 | + | return nil |
| 716 | + | } | |||
| 712 | 717 | ||||
| 713 | - | return cleanupErr | 718 | + | func removeOrphanRoute(ctx context.Context, svc *services, orphanDomain string, log zerowrap.Logger) error { |
| 719 | + | err := svc.configSvc.RemoveRoute(ctx, orphanDomain) | |||
| 720 | + | if err == nil || errors.Is(err, domain.ErrRouteNotFound) { | |||
| 721 | + | return nil | |||
| 722 | + | } | |||
| 723 | + | log.Warn().Err(err).Str("domain", orphanDomain).Msg("failed to remove orphan route") | |||
| 724 | + | return err | |||
| 714 | } | 725 | } | ||
| 715 | 726 | ||||
| 716 | // injectTelemetryMetrics creates and injects OTel metrics into services when | 727 | // injectTelemetryMetrics creates and injects OTel metrics into services when | ||