|
| 1 | +package gatewayapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/openshift/cluster-ingress-operator/pkg/manifests" |
| 8 | + |
| 9 | + rbacv1 "k8s.io/api/rbac/v1" |
| 10 | + "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 13 | + |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 16 | +) |
| 17 | + |
| 18 | +var managedClusterRoles = []*rbacv1.ClusterRole{ |
| 19 | + manifests.GatewayAPIAdminClusterRole(), |
| 20 | + manifests.GatewayAPIViewClusterRole(), |
| 21 | +} |
| 22 | + |
| 23 | +// ensureGatewayAPIRBAC ensures that all RBAC resources for Gateway API are |
| 24 | +// deployed to the cluster, and up to date. |
| 25 | +func (r *reconciler) ensureGatewayAPIRBAC(ctx context.Context) error { |
| 26 | + var errs []error |
| 27 | + |
| 28 | + for _, clusterRole := range managedClusterRoles { |
| 29 | + if err := r.ensureClusterRole(ctx, clusterRole); err != nil { |
| 30 | + errs = append(errs, err) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return utilerrors.NewAggregate(errs) |
| 35 | +} |
| 36 | + |
| 37 | +// ensureClusterRole ensures that a specific ClusterRole RBAC resource is |
| 38 | +// deployed to the cluster, and up to date. |
| 39 | +func (r *reconciler) ensureClusterRole(ctx context.Context, desired *rbacv1.ClusterRole) error { |
| 40 | + clusterRoleAlreadyExists, current, err := r.currentClusterRole(ctx, desired.Name) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + if clusterRoleAlreadyExists { |
| 46 | + if updated, err := r.updateClusterRole(ctx, current, desired); err != nil { |
| 47 | + return err |
| 48 | + } else if updated { |
| 49 | + if _, _, err := r.currentClusterRole(ctx, desired.Name); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + } |
| 53 | + } else { |
| 54 | + return r.createClusterRole(ctx, desired) |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// currentClusterRole retrieves a ClusterRole from the cluster, given the name |
| 61 | +// of that role. |
| 62 | +func (r *reconciler) currentClusterRole(ctx context.Context, name string) (bool, *rbacv1.ClusterRole, error) { |
| 63 | + var existingClusterRole rbacv1.ClusterRole |
| 64 | + |
| 65 | + if err := r.client.Get(ctx, types.NamespacedName{Name: name}, &existingClusterRole); err != nil { |
| 66 | + if errors.IsNotFound(err) { |
| 67 | + return false, nil, nil |
| 68 | + } |
| 69 | + return false, nil, fmt.Errorf("failed to get ClusterRole %s: %w", name, err) |
| 70 | + } |
| 71 | + |
| 72 | + return true, &existingClusterRole, nil |
| 73 | +} |
| 74 | + |
| 75 | +// createClusterRole creates the desired ClusterRole on the cluster. |
| 76 | +func (r *reconciler) createClusterRole(ctx context.Context, desired *rbacv1.ClusterRole) error { |
| 77 | + desired.ResourceVersion = "" |
| 78 | + |
| 79 | + if err := r.client.Create(ctx, desired); err != nil { |
| 80 | + return fmt.Errorf("failed to create ClusterRole %s: %w", desired.Name, err) |
| 81 | + } |
| 82 | + |
| 83 | + log.Info("Created ClusterRole", "name", desired.Name) |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// updateClusterRole verifies whether the current ClusterRole is up to date with |
| 89 | +// what is desired, and performs an update if not. |
| 90 | +func (r *reconciler) updateClusterRole(ctx context.Context, current, desired *rbacv1.ClusterRole) (bool, error) { |
| 91 | + changed, updated := clusterRoleChanged(current, desired) |
| 92 | + if !changed { |
| 93 | + return false, nil |
| 94 | + } |
| 95 | + |
| 96 | + // Diff before updating because the client may mutate the object. |
| 97 | + diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) |
| 98 | + if err := r.client.Update(ctx, updated); err != nil { |
| 99 | + return false, fmt.Errorf("failed to update ClusterRole %s: %w", updated.Name, err) |
| 100 | + } |
| 101 | + |
| 102 | + log.Info("updated ClusterRole", "name", updated.Name, "diff", diff) |
| 103 | + |
| 104 | + return true, nil |
| 105 | +} |
| 106 | + |
| 107 | +// clusterRoleChanged indicates whether there are changes between the current |
| 108 | +// ClusterRole and what is expected, and provides the updated version of the |
| 109 | +// ClusterRole if so. |
| 110 | +func clusterRoleChanged(current, expected *rbacv1.ClusterRole) (bool, *rbacv1.ClusterRole) { |
| 111 | + if cmp.Equal(current.Rules, expected.Rules) && |
| 112 | + cmp.Equal(current.ObjectMeta.Labels, expected.ObjectMeta.Labels) && |
| 113 | + cmp.Equal(current.ObjectMeta.Annotations, expected.ObjectMeta.Annotations) { |
| 114 | + return false, nil |
| 115 | + } |
| 116 | + |
| 117 | + updated := current.DeepCopy() |
| 118 | + updated.ObjectMeta.Labels = expected.ObjectMeta.Labels |
| 119 | + updated.ObjectMeta.Annotations = expected.ObjectMeta.Annotations |
| 120 | + updated.Rules = expected.Rules |
| 121 | + |
| 122 | + return true, updated |
| 123 | +} |
0 commit comments