-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jordan Olshevski
committed
Jan 5, 2024
1 parent
4586079
commit d53a4fd
Showing
3 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cleanup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apiv1 "github.com/Azure/eno/api/v1" | ||
"github.com/Azure/eno/internal/manager" | ||
"github.com/go-logr/logr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
// TODO: Should we limit the max number of resource slices per composition to protect etcd in the face of partial resource slice write failures? | ||
|
||
// TODO: Add finalizer on composition to block until all resource slices owned by the composition are fully deleted (reuse builtin?) | ||
|
||
type compositionController struct { | ||
client client.Client | ||
} | ||
|
||
func NewCompositionController(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&apiv1.ResourceSlice{}). | ||
Watches(&apiv1.Composition{}, manager.NewCompositionToResourceSliceHandler(mgr.GetClient())). | ||
WithLogConstructor(manager.NewLogConstructor(mgr, "compositionCleanupController")). | ||
Complete(&compositionController{ | ||
client: mgr.GetClient(), | ||
}) | ||
} | ||
|
||
func (c *compositionController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := logr.FromContextOrDiscard(ctx).WithValues("compositionName", req.Name, "compositionNamespace", req.Namespace) | ||
|
||
comp := &apiv1.Composition{} | ||
err := c.client.Get(ctx, req.NamespacedName, comp) | ||
if err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("getting composition: %w", err)) | ||
} | ||
if !controllerutil.ContainsFinalizer(comp, "eno.azure.io/cleanup") { | ||
return ctrl.Result{}, nil // nothing to do | ||
} | ||
|
||
list := &apiv1.ResourceSliceList{} | ||
err = c.client.List(ctx, list, client.MatchingFields{ | ||
manager.IdxResourceSlicesByComposition: comp.Name, | ||
}) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("listing resource slices: %w", err) | ||
} | ||
if len(list.Items) > 0 { | ||
return ctrl.Result{}, nil // slices still exist | ||
} | ||
|
||
controllerutil.RemoveFinalizer(comp, "eno.azure.io/cleanup") | ||
err = c.client.Update(ctx, comp) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("removing finalizer: %w", err) | ||
} | ||
|
||
logger.Info("removed finalizer from composition because none of its resource slices remain") | ||
return ctrl.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters