Files
alex bezek 3187082b0e RBAC overhaul (#804)
* docs: add RBAC overhaul design spec and requirements

Captures the motivation, constraints, and design decisions for the RBAC
overhaul before the implementation changes land.

* refactor(rbac): remove kubebuilder RBAC markers and disable controller-gen RBAC output

RBAC is now defined explicitly in Helm templates rather than generated
from code annotations. Removes all +kubebuilder:rbac markers from
controllers and drain.go, and drops the rbac output target from
controller-gen so it no longer clobbers the Helm-managed files.

* refactor(rbac): reorganize operator component RBAC into per-component Helm templates

Replaces the monolithic controller-rbac.yaml and per-component rbac.yaml
files with a consistent per-component directory structure (agent/,
api-manager/, bindings-forwarder/). Each component now owns its own
Role, RoleBinding, and optional namespace-scoped variants.

Key changes:
- agent: split rbac.yaml into role.yaml + rolebinding.yaml with
  optional namespaced variants for namespace-scoped installs
- api-manager: moved from templates/rbac/role.yaml into dedicated
  api-manager/ directory alongside its other templates; adds
  leader-election-role.yaml and namespaced role support
- bindings-forwarder: renamed rbac.yaml -> role.yaml for consistency
- Deleted controller-rbac.yaml (replaced by api-manager/role.yaml)
- Renamed controller-{cm,deployment,pdb,serviceaccount}.yaml into
  api-manager/ directory for cohesion
- Renamed service-account.yaml -> serviceaccount.yaml everywhere
- values.yaml/schema: adds crdAccessRoles and per-component RBAC flags

* feat(rbac): add CRD editor/viewer ClusterRoles for ngrok resources

Moves existing editor/viewer roles into a dedicated rbac/crd-access/
subdirectory with consistent naming, and adds new roles for
NgrokTrafficPolicy (previously missing).

These ClusterRoles are for users of the operator — granting cluster
members read or write access to ngrok CRDs — as opposed to the
operator's own service account permissions.

* test(rbac): update Helm unit tests and add chainsaw e2e RBAC verification

Updates all Helm unit tests and snapshots to match the reorganized
template structure (per-component directories, renamed files). Adds
new test suites for api-manager RBAC and crd-access roles.

Also adds a chainsaw e2e test that verifies the operator's service
accounts have exactly the permissions they need — no more, no less.

* chore: update generated artifacts after RBAC overhaul

Regenerates manifest-bundle.yaml and updates the Helm README to
reflect the new values added for per-component RBAC configuration.

* remove plan and chainsaw tests and make bindings not try to use watchNamespace

* break out k8soperator permissions and bindings permissions to separate role

* update requirements and gen manifest bundle

* make agent and api manager only query their release namespace when looking for the kubernetesoperator crd

* make bindings role always be created even if bindings is disabled
2026-05-06 18:42:34 +00:00

100 lines
3.1 KiB
Go

/*
MIT License
Copyright (c) 2025 ngrok, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package gateway
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/events"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/go-logr/logr"
"github.com/ngrok/ngrok-operator/pkg/managerdriver"
)
// ReferenceGrantReconciler reconciles ReferenceGrants
type ReferenceGrantReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Recorder events.EventRecorder
Driver *managerdriver.Driver
}
func (r *ReferenceGrantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx).WithValues("ReferenceGrant", req.Name)
ctx = ctrl.LoggerInto(ctx, log)
var referenceGrant gatewayv1beta1.ReferenceGrant
err := r.Get(ctx, req.NamespacedName, &referenceGrant)
switch {
case err == nil:
_, err := r.Driver.UpdateReferenceGrant(&referenceGrant)
if err != nil {
log.Error(err, "failed to update ReferenceGrant in store")
return ctrl.Result{}, err
}
case client.IgnoreNotFound(err) == nil:
if err := r.Driver.DeleteReferenceGrant(req.NamespacedName); err != nil {
log.Error(err, "failed to delete ReferenceGrant from store")
return ctrl.Result{}, err
}
default:
return ctrl.Result{}, err
}
return managerdriver.HandleSyncResult(r.Driver.Sync(ctx, r.Client))
}
// SetupWithManager sets up the controller with the Manager.
func (r *ReferenceGrantReconciler) SetupWithManager(mgr ctrl.Manager) error {
storedResources := []client.Object{
&gatewayv1beta1.ReferenceGrant{},
}
builder := ctrl.NewControllerManagedBy(mgr).For(&gatewayv1beta1.ReferenceGrant{})
for _, obj := range storedResources {
builder = builder.Watches(
obj,
managerdriver.NewControllerEventHandler(
obj.GetObjectKind().GroupVersionKind().Kind,
r.Driver,
r.Client,
),
).WithEventFilter(
predicate.Or(
predicate.GenerationChangedPredicate{},
),
)
}
return builder.Complete(r)
}