Skip to content

Commit

Permalink
SQUASH: better disengage error behaviour
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
  • Loading branch information
sttts committed Apr 24, 2024
1 parent 5bafe05 commit fc48477
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pkg/controller/multicluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"sync"

kerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/cluster"
)

Expand Down Expand Up @@ -64,22 +65,33 @@ func (c *multiClusterController) Engage(clusterCtx context.Context, cl cluster.C
return nil
}

engaged := make([]cluster.Aware, 0, len(c.awares)+1)
disengage := func() error {
var errs []error
for _, aware := range engaged {
if err := aware.Disengage(clusterCtx, cl); err != nil {
errs = append(errs, err)
}
}
return kerrors.NewAggregate(errs)
}

// pass through in case the controller itself is cluster aware
if ctrl, ok := c.Controller.(cluster.Aware); ok {
if err := ctrl.Engage(clusterCtx, cl); err != nil {
return err
}
engaged = append(engaged, ctrl)
}

// start watches on the cluster
if err := c.watcher.Watch(clusterCtx, cl); err != nil {
if ctrl, ok := c.Controller.(cluster.AwareRunnable); ok {
if err := ctrl.Disengage(clusterCtx, cl); err != nil {
return err
}
if err := disengage(); err != nil {
return err
}
return err
}

c.clusters[cl.Name()] = struct{}{}

return nil
Expand All @@ -96,11 +108,12 @@ func (c *multiClusterController) Disengage(ctx context.Context, cl cluster.Clust
delete(c.clusters, cl.Name())

// pass through in case the controller itself is cluster aware
if ctrl, ok := c.Controller.(cluster.AwareRunnable); ok {
var errs []error
if ctrl, ok := c.Controller.(cluster.Aware); ok {
if err := ctrl.Disengage(ctx, cl); err != nil {
return err
errs = append(errs, err)
}
}

return nil
return kerrors.NewAggregate(errs)
}

0 comments on commit fc48477

Please sign in to comment.