|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +// Package rbac contains utilities to reconcile RBAC resources |
| 21 | +// for the barman-cloud plugin. |
| 22 | +package rbac |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + |
| 27 | + cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" |
| 28 | + "github.com/cloudnative-pg/machinery/pkg/log" |
| 29 | + rbacv1 "k8s.io/api/rbac/v1" |
| 30 | + "k8s.io/apimachinery/pkg/api/equality" |
| 31 | + apierrs "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 34 | + |
| 35 | + barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" |
| 36 | + "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/specs" |
| 37 | +) |
| 38 | + |
| 39 | +// EnsureRole ensures the RBAC Role for the given Cluster matches |
| 40 | +// the desired state derived from the given ObjectStores. On creation, |
| 41 | +// the Cluster is set as the owner of the Role for garbage collection. |
| 42 | +// |
| 43 | +// This function is called from both the Pre hook (gRPC) and the |
| 44 | +// ObjectStore controller. To handle concurrent modifications |
| 45 | +// gracefully, AlreadyExists on Create and Conflict on Patch are |
| 46 | +// retried once rather than returned as errors. |
| 47 | +func EnsureRole( |
| 48 | + ctx context.Context, |
| 49 | + c client.Client, |
| 50 | + cluster *cnpgv1.Cluster, |
| 51 | + barmanObjects []barmancloudv1.ObjectStore, |
| 52 | +) error { |
| 53 | + newRole := specs.BuildRole(cluster, barmanObjects) |
| 54 | + |
| 55 | + roleKey := client.ObjectKey{ |
| 56 | + Namespace: newRole.Namespace, |
| 57 | + Name: newRole.Name, |
| 58 | + } |
| 59 | + |
| 60 | + var role rbacv1.Role |
| 61 | + err := c.Get(ctx, roleKey, &role) |
| 62 | + |
| 63 | + switch { |
| 64 | + case apierrs.IsNotFound(err): |
| 65 | + role, err := createRole(ctx, c, cluster, newRole) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if role == nil { |
| 70 | + // Created successfully, nothing else to do. |
| 71 | + return nil |
| 72 | + } |
| 73 | + // AlreadyExists: fall through to patch with the re-read role. |
| 74 | + return patchRoleRules(ctx, c, newRole.Rules, role) |
| 75 | + |
| 76 | + case err != nil: |
| 77 | + return err |
| 78 | + |
| 79 | + default: |
| 80 | + return patchRoleRules(ctx, c, newRole.Rules, &role) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// createRole attempts to create the Role. If another writer created |
| 85 | +// it concurrently (AlreadyExists), it re-reads and returns the |
| 86 | +// existing Role for the caller to patch. On success it returns nil. |
| 87 | +func createRole( |
| 88 | + ctx context.Context, |
| 89 | + c client.Client, |
| 90 | + cluster *cnpgv1.Cluster, |
| 91 | + newRole *rbacv1.Role, |
| 92 | +) (*rbacv1.Role, error) { |
| 93 | + contextLogger := log.FromContext(ctx) |
| 94 | + |
| 95 | + if err := controllerutil.SetControllerReference(cluster, newRole, c.Scheme()); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + contextLogger.Info("Creating role", |
| 100 | + "name", newRole.Name, "namespace", newRole.Namespace) |
| 101 | + |
| 102 | + createErr := c.Create(ctx, newRole) |
| 103 | + if createErr == nil { |
| 104 | + return nil, nil |
| 105 | + } |
| 106 | + if !apierrs.IsAlreadyExists(createErr) { |
| 107 | + return nil, createErr |
| 108 | + } |
| 109 | + |
| 110 | + contextLogger.Info("Role was created concurrently, checking rules") |
| 111 | + |
| 112 | + var role rbacv1.Role |
| 113 | + if err := c.Get(ctx, client.ObjectKeyFromObject(newRole), &role); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + return &role, nil |
| 118 | +} |
| 119 | + |
| 120 | +// patchRoleRules patches the Role's rules if they differ from the |
| 121 | +// desired state. On Conflict (concurrent modification), it retries |
| 122 | +// once with a fresh read. |
| 123 | +func patchRoleRules( |
| 124 | + ctx context.Context, |
| 125 | + c client.Client, |
| 126 | + desiredRules []rbacv1.PolicyRule, |
| 127 | + role *rbacv1.Role, |
| 128 | +) error { |
| 129 | + if equality.Semantic.DeepEqual(desiredRules, role.Rules) { |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + contextLogger := log.FromContext(ctx) |
| 134 | + contextLogger.Info("Patching role", |
| 135 | + "name", role.Name, "namespace", role.Namespace, "rules", desiredRules) |
| 136 | + |
| 137 | + oldRole := role.DeepCopy() |
| 138 | + role.Rules = desiredRules |
| 139 | + |
| 140 | + patchErr := c.Patch(ctx, role, client.MergeFrom(oldRole)) |
| 141 | + if patchErr == nil || !apierrs.IsConflict(patchErr) { |
| 142 | + return patchErr |
| 143 | + } |
| 144 | + |
| 145 | + // Conflict: re-read and retry once. |
| 146 | + contextLogger.Info("Role was modified concurrently, retrying patch") |
| 147 | + if err := c.Get(ctx, client.ObjectKeyFromObject(role), role); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + if equality.Semantic.DeepEqual(desiredRules, role.Rules) { |
| 151 | + return nil |
| 152 | + } |
| 153 | + |
| 154 | + oldRole = role.DeepCopy() |
| 155 | + role.Rules = desiredRules |
| 156 | + |
| 157 | + return c.Patch(ctx, role, client.MergeFrom(oldRole)) |
| 158 | +} |
0 commit comments