Skip to content

Commit

Permalink
Watch owned objects to react to OOB changes
Browse files Browse the repository at this point in the history
It's expected (and useful) behaviour to recreate a controller-created
object that is deleted out of band (e.g., by a pesky human). To make
it work this way, it's usually only necessary for the controller to
watch the objects in question, since a deletion will then trigger a
reconciliation of the owner object.

Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
  • Loading branch information
squaremo committed Apr 13, 2022
1 parent 2af9d21 commit 8e7416e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions module/controllers/bootstrapmodule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (r *BootstrapModuleReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
// This is a hack to work around https://github.com/fluxcd/source-controller/issues/315
source.Spec.Reference.Branch = "main"
// the resulting source is "controller owned" by the bootstrap module
return controllerutil.SetControllerReference(&mod, &source, r.Scheme)
})
if err != nil {
Expand Down Expand Up @@ -236,6 +237,8 @@ func (r *BootstrapModuleReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&fleetv1.BootstrapModule{}).
Owns(&sourcev1.GitRepository{}).
// These are not "controller-owned" by the bootstrap modules, so this cannot use `.Owns`
Watches(
&source.Kind{Type: &fleetv1.RemoteAssemblage{}},
&handler.EnqueueRequestForOwner{
Expand Down
1 change: 1 addition & 0 deletions module/controllers/remoteassemblage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (r *RemoteAssemblageReconciler) SetupWithManager(mgr ctrl.Manager) error {
&source.Kind{Type: &corev1.Secret{}},
handler.EnqueueRequestsFromMapFunc(r.assemblagesForSecret),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{})).
Owns(&kustomv1.Kustomization{}).
Complete(r)
}

Expand Down

0 comments on commit 8e7416e

Please sign in to comment.