Skip to content

Commit 4940cca

Browse files
authored
Merge pull request #13 from shreyansjainl/fix/status-conditions-unbounded-growth
ISR-11280 Fix unbounded Role status.conditions growth and GrantStatement REVOKE-without-regrant bug
2 parents 2e69b77 + 8e881d3 commit 4940cca

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎controllers/postgresql/grantstatement_controller.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ var (
6363
type GrantStatementReconciler struct {
6464
client.Client
6565
Scheme *runtime.Scheme
66+
// APIReader bypasses the manager's cache. The skip-if-unchanged check below
67+
// must never evaluate against a status the cache hasn't caught up on yet -
68+
// otherwise a reconcile that just ran REVOKE ALL can be immediately followed
69+
// by another reconcile that reads the pre-revoke cached status, wrongly
70+
// concludes nothing changed, and skips re-granting, leaving the role with
71+
// no privileges. Reading the object live here closes that race.
72+
APIReader client.Reader
6673
}
6774

6875
//+kubebuilder:rbac:groups=postgresql.facets.cloud,resources=grantstatements,verbs=get;list;watch;create;update;patch;delete
@@ -84,9 +91,14 @@ func (r *GrantStatementReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8491
panic(err)
8592
}
8693

87-
// get grantstatement resource
94+
// get grantstatement resource - read live (not from the cache) since the
95+
// skip-if-unchanged decision below depends on this object's latest status
96+
reader := r.APIReader
97+
if reader == nil {
98+
reader = r.Client
99+
}
88100
grantStatement := &postgresqlv1alpha1.GrantStatement{}
89-
err = r.Get(ctx, req.NamespacedName, grantStatement)
101+
err = reader.Get(ctx, req.NamespacedName, grantStatement)
90102
if err != nil {
91103
return ctrl.Result{}, nil
92104
}

‎controllers/postgresql/role_controller.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func (r *RoleReconciler) appendRoleStatusCondition(ctx context.Context, role *po
481481

482482
getLastItem := roleStatusConditions[len(roleStatusConditions)-1]
483483
if getLastItem.Reason != condition.Reason {
484-
role.Status.Conditions = append(role.Status.Conditions, condition)
484+
role.Status.Conditions = append(roleStatusConditions, condition)
485485
err := r.Status().Update(ctx, role)
486486
if err != nil {
487487
roleLogger.Error(err, fmt.Sprintf("Resource status update failed for role `%s`", role.Name))

‎main.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ func main() {
106106
os.Exit(1)
107107
}
108108
if err = (&postgresqlcontrollers.GrantStatementReconciler{
109-
Client: mgr.GetClient(),
110-
Scheme: mgr.GetScheme(),
109+
Client: mgr.GetClient(),
110+
Scheme: mgr.GetScheme(),
111+
APIReader: mgr.GetAPIReader(),
111112
}).SetupWithManager(mgr); err != nil {
112113
setupLog.Error(err, "unable to create controller", "controller", "GrantStatement")
113114
os.Exit(1)

0 commit comments

Comments
 (0)