Closes a gap named across CLAUDE.md/docs/architecture.md/deploy/README.md
since early Phase 4: the operator's Tenant CRD and -provision-tenant
were two disconnected mechanisms. The operator's reconciler generated a
K8s Secret with a locally-generated random password that authenticated
against nothing (nothing ever called ClickHouse to create a matching
user), and unconditionally claimed status.phase=Active the moment a
Tenant object existed -- actively misleading, not just incomplete.
Two unification shapes were considered (surfaced to the user via
AskUserQuestion, given the real difference in blast radius): the
operator's reconcile loop becoming a second real actor (new Postgres +
ClickHouse admin credentials flowing into the K8s controller, plus real
reconcile-loop idempotency/retry design for an inherently one-shot
external side effect), or keeping -provision-tenant as the sole real
actor and having it also sync its result into the CRD. Went with the
lighter option.
enterprise/internal/tenantcrd (new): a Syncer using the K8s dynamic
client (unstructured.Unstructured + a GroupVersionResource, not
deploy/operator's typed Tenant struct -- avoids a cross-module Go
dependency between two independently-versioned modules for one type).
Upserts the Tenant object, creates/updates a Secret with the *real*
ClickHouse credentials owned by that Tenant via an OwnerReference, then
patches status.{clickHouseDatabaseName,clickHouseSecretRef,
tantivyIndexPath}. Idempotent and safe to retry: never rotates a
credential across a re-sync, never overwrites a pre-existing
spec.displayName a human/GitOps process set.
cmd/enterprise-api/main.go's runProvisionTenant calls Sync when
TENANT_CRD_NAMESPACE is set (empty = no-op, same shape as every other
optional dependency in this codebase). Its "already active" refusal is
now split: ClickHouse re-provisioning is still refused (rotating a live
credential would break every open connection for no benefit), but CR
sync alone is now retryable using the credentials already on file in
rbacstore -- needed for retrying a previously-failed sync, or
backfilling CR sync for a tenant provisioned before this existed.
deploy/operator's reconciler rewritten to match: it never claims
PhaseActive on its own initiative anymore, only once
status.ClickHouseDatabaseName is non-empty (the field -provision-tenant,
and only -provision-tenant, sets). Phase is now a pure function of
{spec.suspended, status.ClickHouseDatabaseName != ""} recomputed every
reconcile, not toggled in place -- fixes a related bug the old code
would have hit once suspension was involved: un-suspending an
already-provisioned tenant needs to return straight to Active, which
isn't derivable from "last observed phase was Suspended" alone. The
reconciler no longer creates or manages any Secret, dropped its
`secrets` RBAC grant entirely, and gained zero new dependencies.
Helm chart: enterprise-api gets its own ServiceAccount/Role/RoleBinding
(get/list/create tenants, get/update/patch tenants/status, get/create/
update secrets -- least-privilege, scoped to the release namespace, not
a ClusterRole) and a TENANT_CRD_NAMESPACE env var, both gated on
tenantOperator.enabled. tenant-operator's ClusterRole loses the
secrets grant it no longer needs.
Verified in this environment: enterprise/internal/tenantcrd's tests run
against k8s.io/client-go's fake dynamic + typed clientsets (real client
library, fake transport, no cluster needed); deploy/operator's rewritten
tenant_controller_test.go runs against controller-runtime's fake
client, including new regression tests for the "must not claim Active
without confirmation" and "un-suspending returns to Active, not
Provisioning" properties; helm template + parsing the rendered YAML
confirms the RBAC split renders exactly as designed under both
tenantOperator.enabled=true/false. Not verified: an actual
-provision-tenant run against a real cluster with the operator watching
(no live cluster in this environment, same disclosed limitation as the
rest of /deploy). Docs updated in lockstep: CLAUDE.md, docs/architecture.md,
deploy/README.md, deploy/helm/sentry/README.md (including a corrected
"Trying the two-tenant example" walkthrough), phase-4-runbook.md (new
§11), enterprise/README.md. Also fixed two unrelated stale claims found
along the way: docs/architecture.md still said docker-compose.yml ran
plain api unconditionally (fixed in an earlier commit, doc not updated
then), and enterprise-api's own main.go doc comment still said Helm/
docker-compose wiring wasn't built yet.
108 lines
4.5 KiB
Go
108 lines
4.5 KiB
Go
// Package controller reconciles the Tenant CRD (api/v1alpha1) -- see
|
|
// TenantPhase's doc comment for the "lightweight unification" this
|
|
// controller is one half of. This reconciler never calls ClickHouse (no
|
|
// CREATE DATABASE/CREATE USER/GRANT), never touches the Tantivy index
|
|
// filesystem, and never talks to enterprise/internal/rbacstore -- those
|
|
// stay enterprise-api -provision-tenant's job (enterprise/internal/
|
|
// tenantprovision, enterprise/internal/tenantcrd). This controller's
|
|
// job is purely a function of what's already on the object: derive
|
|
// Phase and the Ready condition from Spec.Suspended and whether
|
|
// Status.ClickHouseDatabaseName has been set by -provision-tenant,
|
|
// nothing more. A Tenant reaching PhaseActive here is exactly the same
|
|
// claim as rbacstore's tenants.status='active' now, not a
|
|
// second, independently-computed one -- see docs/phase-4-isolation-design.md.
|
|
//
|
|
// Earlier versions of this controller also generated a per-tenant
|
|
// ClickHouse credential Secret with a locally-generated random
|
|
// password. That Secret authenticated against nothing (nothing in this
|
|
// controller ever called ClickHouse to create a matching user) and
|
|
// nothing else in the codebase ever read it -- a placeholder that
|
|
// actively misled ("looks provisioned") rather than one that honestly
|
|
// represented "not yet provisioned." Removed rather than fixed in
|
|
// place: the real Secret, with real credentials, is now created by
|
|
// -provision-tenant (enterprise/internal/tenantcrd) once ClickHouse
|
|
// provisioning actually succeeds.
|
|
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
sentryv1alpha1 "github.com/sentry/sentry/deploy/operator/api/v1alpha1"
|
|
)
|
|
|
|
// TenantReconciler reconciles a Tenant object.
|
|
type TenantReconciler struct {
|
|
client.Client
|
|
Scheme *runtime.Scheme
|
|
}
|
|
|
|
// +kubebuilder:rbac:groups=sentry.io,resources=tenants,verbs=get;list;watch;create;update;patch;delete
|
|
// +kubebuilder:rbac:groups=sentry.io,resources=tenants/status,verbs=get;update;patch
|
|
|
|
func (r *TenantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
var tenant sentryv1alpha1.Tenant
|
|
if err := r.Get(ctx, req.NamespacedName, &tenant); err != nil {
|
|
if apierrors.IsNotFound(err) {
|
|
// Deleted -- the owned Secret -provision-tenant created (if
|
|
// any) is garbage-collected by K8s via its OwnerReference,
|
|
// nothing else to clean up at this layer. Real
|
|
// deprovisioning (revoking ClickHouse grants) isn't this
|
|
// controller's job, or -provision-tenant's today -- see
|
|
// /docs/security/threat-model.md's non-goals.
|
|
return ctrl.Result{}, nil
|
|
}
|
|
return ctrl.Result{}, fmt.Errorf("getting tenant: %w", err)
|
|
}
|
|
|
|
// provisioned is true once -provision-tenant has confirmed real
|
|
// ClickHouse provisioning by setting this field -- see
|
|
// TenantStatus.ClickHouseDatabaseName's doc comment. This
|
|
// controller treats it as the sole source of truth for "has
|
|
// provisioning actually happened," never claiming PhaseActive on
|
|
// its own say-so the way the pre-unification version did.
|
|
provisioned := tenant.Status.ClickHouseDatabaseName != ""
|
|
|
|
condStatus := metav1.ConditionFalse
|
|
reason, message := "AwaitingProvisioning", "waiting for enterprise-api -provision-tenant to provision ClickHouse for this tenant"
|
|
switch {
|
|
case tenant.Spec.Suspended:
|
|
tenant.Status.Phase = sentryv1alpha1.PhaseSuspended
|
|
reason, message = "Suspended", "tenant is suspended (spec.suspended=true)"
|
|
case provisioned:
|
|
tenant.Status.Phase = sentryv1alpha1.PhaseActive
|
|
condStatus = metav1.ConditionTrue
|
|
reason, message = "Provisioned", fmt.Sprintf("ClickHouse database %q is provisioned", tenant.Status.ClickHouseDatabaseName)
|
|
default:
|
|
tenant.Status.Phase = sentryv1alpha1.PhaseProvisioning
|
|
}
|
|
|
|
tenant.Status.ObservedGeneration = tenant.Generation
|
|
meta.SetStatusCondition(&tenant.Status.Conditions, metav1.Condition{
|
|
Type: sentryv1alpha1.ConditionReady,
|
|
Status: condStatus,
|
|
Reason: reason,
|
|
Message: message,
|
|
ObservedGeneration: tenant.Generation,
|
|
})
|
|
|
|
if err := r.Status().Update(ctx, &tenant); err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("updating tenant status: %w", err)
|
|
}
|
|
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
func (r *TenantReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&sentryv1alpha1.Tenant{}).
|
|
Complete(r)
|
|
}
|