@@ -21,6 +21,7 @@ import (
2121 "github.com/crunchydata/postgres-operator/internal/config"
2222 "github.com/crunchydata/postgres-operator/internal/controller/runtime"
2323 "github.com/crunchydata/postgres-operator/internal/logging"
24+ "github.com/crunchydata/postgres-operator/internal/naming"
2425 "github.com/crunchydata/postgres-operator/internal/registration"
2526 "github.com/crunchydata/postgres-operator/internal/tracing"
2627 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
@@ -32,29 +33,49 @@ const (
3233
3334// PGUpgradeReconciler reconciles a PGUpgrade object
3435type PGUpgradeReconciler struct {
35- Client client.Client
36- Owner client.FieldOwner
37-
3836 Recorder record.EventRecorder
3937 Registration registration.Registration
38+
39+ Reader interface {
40+ Get (context.Context , client.ObjectKey , client.Object , ... client.GetOption ) error
41+ List (context.Context , client.ObjectList , ... client.ListOption ) error
42+ }
43+ Writer interface {
44+ Delete (context.Context , client.Object , ... client.DeleteOption ) error
45+ Patch (context.Context , client.Object , client.Patch , ... client.PatchOption ) error
46+ }
47+ StatusWriter interface {
48+ Patch (context.Context , client.Object , client.Patch , ... client.SubResourcePatchOption ) error
49+ }
4050}
4151
4252//+kubebuilder:rbac:groups="batch",resources="jobs",verbs={list,watch}
4353//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="pgupgrades",verbs={list,watch}
4454//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="postgresclusters",verbs={list,watch}
4555
46- // SetupWithManager sets up the controller with the Manager.
47- func (r * PGUpgradeReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
48- return ctrl .NewControllerManagedBy (mgr ).
56+ // ManagedReconciler creates a [PGUpgradeReconciler] and adds it to m.
57+ func ManagedReconciler (m ctrl.Manager , r registration.Registration ) error {
58+ kubernetes := client .WithFieldOwner (m .GetClient (), naming .ControllerPGUpgrade )
59+ recorder := m .GetEventRecorderFor (naming .ControllerPGUpgrade )
60+
61+ reconciler := & PGUpgradeReconciler {
62+ Reader : kubernetes ,
63+ Recorder : recorder ,
64+ Registration : r ,
65+ StatusWriter : kubernetes .Status (),
66+ Writer : kubernetes ,
67+ }
68+
69+ return ctrl .NewControllerManagedBy (m ).
4970 For (& v1beta1.PGUpgrade {}).
5071 Owns (& batchv1.Job {}).
5172 Watches (
5273 v1beta1 .NewPostgresCluster (),
5374 handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , cluster client.Object ) []ctrl.Request {
54- return runtime .Requests (r .findUpgradesForPostgresCluster (ctx , client .ObjectKeyFromObject (cluster ))... )
75+ return runtime .Requests (reconciler .findUpgradesForPostgresCluster (ctx , client .ObjectKeyFromObject (cluster ))... )
5576 }),
5677 ).
57- Complete (r )
78+ Complete (reconciler )
5879}
5980
6081//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="pgupgrades",verbs={list}
@@ -70,7 +91,7 @@ func (r *PGUpgradeReconciler) findUpgradesForPostgresCluster(
7091 // namespace, we can configure the [ctrl.Manager] field indexer and pass a
7192 // [fields.Selector] here.
7293 // - https://book.kubebuilder.io/reference/watching-resources/externally-managed.html
73- if r .Client .List (ctx , & upgrades , & client.ListOptions {
94+ if r .Reader .List (ctx , & upgrades , & client.ListOptions {
7495 Namespace : cluster .Namespace ,
7596 }) == nil {
7697 for i := range upgrades .Items {
@@ -107,14 +128,14 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
107128 // copy before returning from its cache.
108129 // - https://github.com/kubernetes-sigs/controller-runtime/issues/1235
109130 upgrade := & v1beta1.PGUpgrade {}
110- err = r .Client .Get (ctx , req .NamespacedName , upgrade )
131+ err = r .Reader .Get (ctx , req .NamespacedName , upgrade )
111132
112133 if err == nil {
113134 // Write any changes to the upgrade status on the way out.
114135 before := upgrade .DeepCopy ()
115136 defer func () {
116137 if ! equality .Semantic .DeepEqual (before .Status , upgrade .Status ) {
117- status := r .Client . Status (). Patch (ctx , upgrade , client .MergeFrom (before ), r . Owner )
138+ status := r .StatusWriter . Patch (ctx , upgrade , client .MergeFrom (before ))
118139
119140 if err == nil && status != nil {
120141 err = status
@@ -409,7 +430,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
409430 // - https://kubernetes.io/docs/concepts/workloads/controllers/job/
410431 // - https://github.com/kubernetes/kubernetes/blob/master/pkg/registry/batch/job/strategy.go#L58
411432 propagate := client .PropagationPolicy (metav1 .DeletePropagationBackground )
412- err = client .IgnoreNotFound (r .Client .Delete (ctx , object , exactly , propagate ))
433+ err = client .IgnoreNotFound (r .Writer .Delete (ctx , object , exactly , propagate ))
413434 }
414435 }
415436
@@ -424,7 +445,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
424445 // Set the pgBackRest status for bootstrapping
425446 patch .Status .PGBackRest .Repos = []v1beta1.RepoStatus {}
426447
427- err = r .Client . Status (). Patch (ctx , patch , client .MergeFrom (world .Cluster ), r . Owner )
448+ err = r .StatusWriter . Patch (ctx , patch , client .MergeFrom (world .Cluster ))
428449 }
429450
430451 return ctrl.Result {}, err
@@ -461,7 +482,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
461482 uid := object .GetUID ()
462483 version := object .GetResourceVersion ()
463484 exactly := client.Preconditions {UID : & uid , ResourceVersion : & version }
464- err = client .IgnoreNotFound (r .Client .Delete (ctx , object , exactly ))
485+ err = client .IgnoreNotFound (r .Writer .Delete (ctx , object , exactly ))
465486 }
466487
467488 // Requeue to verify that Patroni endpoints are deleted
0 commit comments