Skip to content

Commit

Permalink
simplify code and remove ownerReferences and finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed Apr 29, 2024
1 parent 2efae59 commit 3298233
Show file tree
Hide file tree
Showing 18 changed files with 555 additions and 838 deletions.
14 changes: 7 additions & 7 deletions pkg/controller/daemonset/daemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ import (
// Add creates a new DaemonSet Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
r := newReconciler(mgr)
return add(mgr, r, r.handler)
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
func newReconciler(mgr manager.Manager) *ReconcileDaemonSet {
return &ReconcileDaemonSet{
scheme: mgr.GetScheme(),
handler: core.NewHandler(mgr.GetClient(), mgr.GetEventRecorderFor("wave")),
}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
func add(mgr manager.Manager, r reconcile.Reconciler, h *core.Handler) error {
// Create a new controller
c, err := controller.New("daemonset-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
Expand All @@ -58,16 +59,14 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

handler := handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.DaemonSet{})

// Watch ConfigMaps owned by a DaemonSet
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}), handler)
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}), core.EnqueueRequestForWatcher(h.GetWatchedConfigmaps()))
if err != nil {
return err
}

// Watch Secrets owned by a DaemonSet
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Secret{}), handler)
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Secret{}), core.EnqueueRequestForWatcher(h.GetWatchedSecrets()))
if err != nil {
return err
}
Expand Down Expand Up @@ -95,6 +94,7 @@ func (r *ReconcileDaemonSet) Reconcile(ctx context.Context, request reconcile.Re
err := r.handler.Get(ctx, request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
r.handler.RemoveWatches(request.NamespacedName)
// Object not found, return. Created objects are automatically garbage collected.
return reconcile.Result{}, nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/daemonset/daemonset_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ var _ = AfterSuite(func() {

// SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and
// writes the request to requests after Reconcile is finished.
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request, chan reconcile.Request) {
requestsStart := make(chan reconcile.Request)
requests := make(chan reconcile.Request)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
requestsStart <- req
result, err := inner.Reconcile(ctx, req)
requests <- req
return result, err
})
return fn, requests
return fn, requestsStart, requests
}

// Run runs the webhook server.
Expand Down
Loading

0 comments on commit 3298233

Please sign in to comment.