c9c52af2f7
CI / test (push) Successful in 1m41s
Implements ROADMAP.md Stage 2 against terdut-server's now-real
GET /api/teams?name= (TEAM-LOOKUP.md, landed in terdut-server just
before this commit) -- without it, the adopt-on-409 pattern this
controller depends on for team creation had no server-side lookup to
call, the same gap TerdutServer's own bootstrap flow hit and fixed in
Stage 1.
- api/v1alpha1: TerdutTeamSpec per DESIGN.md §4.2 (serverRef, displayName,
oidc). status.credentialsSecretRef drops namespace for key, matching
the fix already applied to TerdutServer's.
- internal/controller:
- terdutteam_controller.go: resolves serverRef (same-namespace by
default; cross-namespace gated by the target TerdutServer's
spec.allowedTeams, DESIGN.md §4.6), waits for that TerdutServer to be
Bootstrapped (no cross-controller RPC -- reads its
status.credentialsSecretRef directly, DESIGN.md §5), creates the team
and mints its team-scoped credential using the TerdutServer's
instance-scoped one, then applies rename/oidc-groups with the
team-scoped credential every reconcile (both are idempotent PUTs of
the whole resource -- applied unconditionally rather than diffed
against a stored last-applied value, same "cheap because it's small"
reasoning §5 already gives the escalation policy's whole-policy PUT).
- terdutteam_allowedteams.go: the §4.6 consent check in isolation from
any client, unit-tested directly against hand-built inputs.
- terdutteam_bootstrap.go: create-or-adopt-on-409 for the team itself
(via TEAM-LOOKUP.md) and for its team-scoped service account (via the
same GET-by-name+mint-new-key pattern Stage 1 already uses for the
instance account).
- secrets.go: extracted TerdutServer's write/read-credential-Secret
helpers into free functions, now shared by both controllers rather
than duplicated.
- Finalizer deletes the team server-side (owner-gated, needs the
team-scoped credential -- confirmed against source that an
instance-scoped one does not satisfy requireTeamOwner, same finding
as TEAM-LOOKUP.md's) and cleans up its credentials Secret. A team
created but never fully reconciled to Ready (no team-scoped
credential ever minted) is left orphaned server-side on delete -- a
known, documented limitation (terdut-server has no delete path that
doesn't require owner-equivalent access), not a silent gap.
- internal/tdclient: Team type, CreateTeam, GetTeamByName, RenameTeam,
DeleteTeam, SetTeamOIDCGroups, CreateTeamServiceAccount -- matching
terdut-server's real handlers' shapes field-for-field, same as Stage
1's client additions.
- Tests: envtest covering the happy path, both not-ready reasons
(ServerRefNotFound, WaitingForServer), cross-namespace allow/deny
(default-closed and explicit All), both adopt-on-409 paths (team
itself, team-scoped service account), and deletion. Extended the shared
fakeTerdutServer (Stage 1) with team endpoints rather than writing a
second, separately-drifting fake. 72.8%/30.6% coverage, 0 lint issues.
Verified locally: make fmt lint test build all clean.
85 lines
3.3 KiB
Go
85 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
terdutv1alpha1 "git.ryuvia.com/niklas/terdut-operator/api/v1alpha1"
|
|
"git.ryuvia.com/niklas/terdut-operator/internal/tdclient"
|
|
)
|
|
|
|
// createOrAdoptTeam calls POST /api/teams with the TerdutServer's
|
|
// instance-scoped credential, or, if an earlier interrupted attempt
|
|
// already created this name (409), adopts it via GET /api/teams?name=
|
|
// (TEAM-LOOKUP.md) rather than treating the conflict as an error --
|
|
// DESIGN.md §5's general adopt-on-conflict rule, the same shape
|
|
// TerdutServer's own bootstrap flow uses for minting its instance account.
|
|
func (r *TerdutTeamReconciler) createOrAdoptTeam(ctx context.Context, team *terdutv1alpha1.TerdutTeam, instanceClient *tdclient.Client) error {
|
|
created, err := instanceClient.CreateTeam(ctx, team.Spec.DisplayName)
|
|
if err == nil {
|
|
team.Status.TeamID = created.ID
|
|
return nil
|
|
}
|
|
|
|
statusErr, ok := errors.AsType[*tdclient.StatusError](err)
|
|
if !ok || statusErr.Code != http.StatusConflict {
|
|
return fmt.Errorf("POST /api/teams: %w", err)
|
|
}
|
|
|
|
found, err := instanceClient.GetTeamByName(ctx, team.Spec.DisplayName)
|
|
if err != nil {
|
|
return fmt.Errorf("GET /api/teams?name=%s (adopting after 409): %w", team.Spec.DisplayName, err)
|
|
}
|
|
if found == nil {
|
|
// Genuinely pathological, not just a narrow crash window: the name
|
|
// was taken a moment ago and isn't now. Surfaced as a plain error
|
|
// (standard requeue-with-backoff) rather than a dedicated
|
|
// condition -- there's no documented recovery to point at that
|
|
// differs from "try again".
|
|
return fmt.Errorf("POST /api/teams 409'd for %q but GET found nothing", team.Spec.DisplayName)
|
|
}
|
|
team.Status.TeamID = found.ID
|
|
return nil
|
|
}
|
|
|
|
// mintTeamCredential mints this team's own team-scoped service account,
|
|
// using the TerdutServer's instance-scoped credential (DESIGN.md §6 point
|
|
// 3: an instance-scoped caller may do this against any team). Adopts via
|
|
// GET+mint-new-key on a 409, the same pattern TerdutServer's own bootstrap
|
|
// flow uses.
|
|
func (r *TerdutTeamReconciler) mintTeamCredential(ctx context.Context, team *terdutv1alpha1.TerdutTeam, instanceClient *tdclient.Client) error {
|
|
saName := teamServiceAccountName(team)
|
|
|
|
result, err := instanceClient.CreateTeamServiceAccount(ctx, saName, team.Status.TeamID)
|
|
var key string
|
|
if err == nil {
|
|
key = result.Key.Key
|
|
} else {
|
|
statusErr, ok := errors.AsType[*tdclient.StatusError](err)
|
|
if !ok || statusErr.Code != http.StatusConflict {
|
|
return fmt.Errorf("POST /api/service-accounts (team scope): %w", err)
|
|
}
|
|
sa, err := instanceClient.GetServiceAccountByName(ctx, saName)
|
|
if err != nil {
|
|
return fmt.Errorf("GET /api/service-accounts?name=%s (adopting after 409): %w", saName, err)
|
|
}
|
|
if sa == nil {
|
|
return fmt.Errorf("POST /api/service-accounts 409'd for %q but GET found nothing", saName)
|
|
}
|
|
minted, err := instanceClient.CreateServiceAccountKey(ctx, sa.ID, "initial")
|
|
if err != nil {
|
|
return fmt.Errorf("POST /api/service-accounts/%d/keys (adopting after 409): %w", sa.ID, err)
|
|
}
|
|
key = minted.Key
|
|
}
|
|
|
|
credsName := teamCredentialsSecretName(team)
|
|
if err := writeOperatorSecret(ctx, r.Client, r.OperatorNamespace, credsName, key); err != nil {
|
|
return err
|
|
}
|
|
team.Status.CredentialsSecretRef = &terdutv1alpha1.SecretKeyRef{Name: credsName, Key: credentialsSecretDataKey}
|
|
return nil
|
|
}
|