Compare commits

..

2 Commits

Author SHA1 Message Date
Armando Ruocco
fca31a984c
Merge 064864ba72 into 376e178ab5 2026-03-27 10:22:44 +00:00
Armando Ruocco
064864ba72 fix(rbac): reconcile Role when ObjectStore spec changes
When an ObjectStore's credentials change (e.g., secret rename), the
RBAC Role granting the Cluster's ServiceAccount access to those secrets
was not updated because nothing triggered a Cluster reconciliation.

Implement the ObjectStore controller's Reconcile to detect referencing
Clusters and update their Roles directly. Extract ensureRole into a
shared rbac.EnsureRole function used by both the Pre hook and the
ObjectStore controller.

The shared EnsureRole accepts optional beforeCreate callbacks so the
Pre hook can set owner references on Role creation, while the
ObjectStore controller path skips ownership (it only updates existing
Roles).

Handle deleted ObjectStores gracefully by skipping NotFound errors
during Role reconciliation, and filter status-only changes with
GenerationChangedPredicate.

Add RBAC permission for the plugin to list/watch Clusters so the
ObjectStore controller can find affected Clusters. Regenerate
config/rbac/role.yaml.

Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
2026-03-27 11:21:34 +01:00
2 changed files with 14 additions and 1 deletions

View File

@ -25,11 +25,13 @@ import (
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/machinery/pkg/log"
rbacv1 "k8s.io/api/rbac/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
@ -115,7 +117,9 @@ func (r *ObjectStoreReconciler) reconcileRBACForCluster(
barmanObjects = append(barmanObjects, barmanObject)
}
return rbac.EnsureRole(ctx, r.Client, cluster, barmanObjects)
return rbac.EnsureRole(ctx, r.Client, cluster, barmanObjects, func(role *rbacv1.Role) error {
return controllerutil.SetControllerReference(cluster, role, r.Scheme)
})
}
// referencesObjectStore checks if the given ObjectStore is in the list

View File

@ -172,6 +172,11 @@ var _ = Describe("ObjectStoreReconciler", func() {
// Verify the secrets rule contains the expected secret
secretsRule := role.Rules[2]
Expect(secretsRule.ResourceNames).To(ContainElement("aws-creds"))
// Verify owner reference is set to the Cluster
Expect(role.OwnerReferences).To(HaveLen(1))
Expect(role.OwnerReferences[0].Name).To(Equal("my-cluster"))
Expect(role.OwnerReferences[0].Kind).To(Equal("Cluster"))
})
It("should skip Clusters that don't reference the ObjectStore", func() {
@ -263,6 +268,10 @@ var _ = Describe("ObjectStoreReconciler", func() {
objectStoreRule := role.Rules[0]
Expect(objectStoreRule.ResourceNames).To(ContainElement("store-a"))
Expect(objectStoreRule.ResourceNames).NotTo(ContainElement("store-b"))
// Verify owner reference is set
Expect(role.OwnerReferences).To(HaveLen(1))
Expect(role.OwnerReferences[0].Name).To(Equal("my-cluster"))
})
It("should update Role when ObjectStore credentials change", func() {