Factor out garbage-collection code
@@ -89,11 +89,34 @@ func (c *Cluster) Sync(spec cluster.SyncSet) error {
c.muSyncErrors.RUnlock()
if c.GC {
+ deleteErrs, gcFailure := c.collectGarbage(spec, checksums, logger)
+ if gcFailure != nil {
+ return gcFailure
+ }
+ errs = append(errs, deleteErrs...)
+ }
+
+ // If `nil`, errs is a cluster.SyncError(nil) rather than error(nil), so it cannot be returned directly.
+ if errs == nil {
+ return nil
+ }
+
+ // It is expected that Cluster.Sync is invoked with *all* resources.
+ // Otherwise it will override previously recorded sync errors.
+ c.setSyncErrors(errs)
+ return errs
+}
+
+func (c *Cluster) collectGarbage(
+ spec cluster.SyncSet,
+ checksums map[string]string,
+ logger log.Logger) (cluster.SyncError, error) {
+
orphanedResources := makeChangeSet()
clusterResources, err := c.getResourcesInSyncSet(spec.Name)
if err != nil {
- return errors.Wrap(err, "collating resources in cluster for calculating garbage collection")
+ return nil, errors.Wrap(err, "collating resources in cluster for calculating garbage collection")
}
for resourceID, res := range clusterResources {
@@ -113,20 +136,7 @@ func (c *Cluster) Sync(spec cluster.SyncSet) error {
}
}
- if deleteErrs := c.applier.apply(logger, orphanedResources, nil); len(deleteErrs) > 0 {
- errs = append(errs, deleteErrs...)
- }
- }
-
- // If `nil`, errs is a cluster.SyncError(nil) rather than error(nil), so it cannot be returned directly.
- if errs == nil {
- return nil
- }
-
- // It is expected that Cluster.Sync is invoked with *all* resources.
- // Otherwise it will override previously recorded sync errors.
- c.setSyncErrors(errs)
- return errs
+ return c.applier.apply(logger, orphanedResources, nil), nil
}
// --- internals in support of Sync
@@ -414,7 +424,7 @@ func (c *Kubectl) apply(logger log.Logger, cs changeSet, errored map[flux.Resour
// When deleting objects, the only real concern is that we don't
// try to delete things that have already been deleted by
- // Kubernete's GC -- most notably, resources in a namespace which
+ // Kubernetes' GC -- most notably, resources in a namespace which
// is also being deleted. GC does not have the dependency ranking,
// but we can use it as a shortcut to avoid the above problem at
// least.
