@@ -21,6 +21,7 @@ import (
21
21
"github.com/crunchydata/postgres-operator/internal/config"
22
22
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
23
23
"github.com/crunchydata/postgres-operator/internal/logging"
24
+ "github.com/crunchydata/postgres-operator/internal/naming"
24
25
"github.com/crunchydata/postgres-operator/internal/registration"
25
26
"github.com/crunchydata/postgres-operator/internal/tracing"
26
27
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
@@ -32,29 +33,49 @@ const (
32
33
33
34
// PGUpgradeReconciler reconciles a PGUpgrade object
34
35
type PGUpgradeReconciler struct {
35
- Client client.Client
36
- Owner client.FieldOwner
37
-
38
36
Recorder record.EventRecorder
39
37
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
+ }
40
50
}
41
51
42
52
//+kubebuilder:rbac:groups="batch",resources="jobs",verbs={list,watch}
43
53
//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="pgupgrades",verbs={list,watch}
44
54
//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="postgresclusters",verbs={list,watch}
45
55
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 ).
49
70
For (& v1beta1.PGUpgrade {}).
50
71
Owns (& batchv1.Job {}).
51
72
Watches (
52
73
v1beta1 .NewPostgresCluster (),
53
74
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 ))... )
55
76
}),
56
77
).
57
- Complete (r )
78
+ Complete (reconciler )
58
79
}
59
80
60
81
//+kubebuilder:rbac:groups="postgres-operator.crunchydata.com",resources="pgupgrades",verbs={list}
@@ -70,7 +91,7 @@ func (r *PGUpgradeReconciler) findUpgradesForPostgresCluster(
70
91
// namespace, we can configure the [ctrl.Manager] field indexer and pass a
71
92
// [fields.Selector] here.
72
93
// - 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 {
74
95
Namespace : cluster .Namespace ,
75
96
}) == nil {
76
97
for i := range upgrades .Items {
@@ -107,14 +128,14 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
107
128
// copy before returning from its cache.
108
129
// - https://github.com/kubernetes-sigs/controller-runtime/issues/1235
109
130
upgrade := & v1beta1.PGUpgrade {}
110
- err = r .Client .Get (ctx , req .NamespacedName , upgrade )
131
+ err = r .Reader .Get (ctx , req .NamespacedName , upgrade )
111
132
112
133
if err == nil {
113
134
// Write any changes to the upgrade status on the way out.
114
135
before := upgrade .DeepCopy ()
115
136
defer func () {
116
137
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 ))
118
139
119
140
if err == nil && status != nil {
120
141
err = status
@@ -409,7 +430,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
409
430
// - https://kubernetes.io/docs/concepts/workloads/controllers/job/
410
431
// - https://github.com/kubernetes/kubernetes/blob/master/pkg/registry/batch/job/strategy.go#L58
411
432
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 ))
413
434
}
414
435
}
415
436
@@ -424,7 +445,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
424
445
// Set the pgBackRest status for bootstrapping
425
446
patch .Status .PGBackRest .Repos = []v1beta1.RepoStatus {}
426
447
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 ))
428
449
}
429
450
430
451
return ctrl.Result {}, err
@@ -461,7 +482,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
461
482
uid := object .GetUID ()
462
483
version := object .GetResourceVersion ()
463
484
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 ))
465
486
}
466
487
467
488
// Requeue to verify that Patroni endpoints are deleted
0 commit comments