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.
280 lines
11 KiB
Go
280 lines
11 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
terdutv1alpha1 "git.ryuvia.com/niklas/terdut-operator/api/v1alpha1"
|
|
"git.ryuvia.com/niklas/terdut-operator/internal/tdclient"
|
|
)
|
|
|
|
var _ = Describe("TerdutTeam Controller", func() {
|
|
const operatorNamespace = "default"
|
|
|
|
var (
|
|
reconciler *TerdutTeamReconciler
|
|
fake *fakeTerdutServer
|
|
fakeSrv *httptest.Server
|
|
srv *terdutv1alpha1.TerdutServer
|
|
teamName string
|
|
teamKey types.NamespacedName
|
|
)
|
|
|
|
BeforeEach(func(ctx SpecContext) {
|
|
fake, fakeSrv = newFakeTerdutServer()
|
|
DeferCleanup(fakeSrv.Close)
|
|
|
|
srv = bootstrapReadyTerdutServer(ctx, operatorNamespace, uniqueName("ttserver"), fakeSrv.URL)
|
|
|
|
reconciler = &TerdutTeamReconciler{
|
|
Client: k8sClient,
|
|
Scheme: k8sClient.Scheme(),
|
|
OperatorNamespace: operatorNamespace,
|
|
NewClient: func(string) *tdclient.Client { return tdclient.New(fakeSrv.URL) },
|
|
}
|
|
teamName = uniqueName("team")
|
|
teamKey = types.NamespacedName{Name: teamName, Namespace: operatorNamespace}
|
|
})
|
|
|
|
AfterEach(func(ctx SpecContext) {
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
if err := k8sClient.Get(ctx, teamKey, team); err == nil {
|
|
team.Finalizers = nil
|
|
_ = k8sClient.Update(ctx, team)
|
|
_ = k8sClient.Delete(ctx, team)
|
|
}
|
|
_ = k8sClient.Delete(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
|
|
Name: fmt.Sprintf("%s.%s-team-credentials", operatorNamespace, teamName), Namespace: operatorNamespace,
|
|
}})
|
|
|
|
// The TerdutServer bootstrapReadyTerdutServer created in BeforeEach
|
|
// carries its own finalizer; clear it directly the same way, rather
|
|
// than relying on TerdutServerReconciler to ever run again here.
|
|
srvKey := types.NamespacedName{Name: srv.Name, Namespace: operatorNamespace}
|
|
if err := k8sClient.Get(ctx, srvKey, srv); err == nil {
|
|
srv.Finalizers = nil
|
|
_ = k8sClient.Update(ctx, srv)
|
|
_ = k8sClient.Delete(ctx, srv)
|
|
}
|
|
_ = k8sClient.Delete(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
|
|
Name: fmt.Sprintf("%s.%s-instance-credentials", operatorNamespace, srv.Name), Namespace: operatorNamespace,
|
|
}})
|
|
})
|
|
|
|
createTeam := func(ctx context.Context, displayName string, serverRef terdutv1alpha1.TerdutServerRef) {
|
|
team := &terdutv1alpha1.TerdutTeam{
|
|
ObjectMeta: metav1.ObjectMeta{Name: teamName, Namespace: operatorNamespace},
|
|
Spec: terdutv1alpha1.TerdutTeamSpec{ServerRef: serverRef, DisplayName: displayName},
|
|
}
|
|
Expect(k8sClient.Create(ctx, team)).To(Succeed())
|
|
}
|
|
|
|
reconcileOnce := func(ctx context.Context) {
|
|
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: teamKey})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
}
|
|
|
|
readyCondition := func(ctx context.Context) metav1.Condition {
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
Expect(k8sClient.Get(ctx, teamKey, team)).To(Succeed())
|
|
c := meta.FindStatusCondition(team.Status.Conditions, terdutv1alpha1.ConditionTeamReady)
|
|
Expect(c).NotTo(BeNil())
|
|
return *c
|
|
}
|
|
|
|
sameNSRef := func() terdutv1alpha1.TerdutServerRef {
|
|
return terdutv1alpha1.TerdutServerRef{Name: srv.Name}
|
|
}
|
|
|
|
Describe("the happy path", func() {
|
|
It("creates the team, mints its credential, and applies rename/oidc-groups", func(ctx SpecContext) {
|
|
createTeam(ctx, "platform", sameNSRef())
|
|
reconcileOnce(ctx) // finalizer
|
|
reconcileOnce(ctx) // full create+mint+apply
|
|
|
|
cond := readyCondition(ctx)
|
|
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
|
|
Expect(cond.Reason).To(Equal(terdutv1alpha1.ReasonTeamAdopted))
|
|
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
Expect(k8sClient.Get(ctx, teamKey, team)).To(Succeed())
|
|
Expect(team.Status.TeamID).To(Equal(fake.teams["platform"]))
|
|
Expect(team.Status.CredentialsSecretRef).NotTo(BeNil())
|
|
|
|
var credsSecret corev1.Secret
|
|
Expect(k8sClient.Get(ctx, types.NamespacedName{
|
|
Name: team.Status.CredentialsSecretRef.Name, Namespace: operatorNamespace,
|
|
}, &credsSecret)).To(Succeed())
|
|
Expect(credsSecret.Data[credentialsSecretDataKey]).NotTo(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Describe("waiting on the referenced TerdutServer", func() {
|
|
It("reports ServerRefNotFound when the TerdutServer doesn't exist", func(ctx SpecContext) {
|
|
createTeam(ctx, "orphan", terdutv1alpha1.TerdutServerRef{Name: "does-not-exist"})
|
|
reconcileOnce(ctx) // finalizer
|
|
reconcileOnce(ctx)
|
|
|
|
Expect(readyCondition(ctx).Reason).To(Equal(terdutv1alpha1.ReasonServerRefNotFound))
|
|
})
|
|
|
|
It("reports WaitingForServer when the TerdutServer exists but isn't Bootstrapped yet", func(ctx SpecContext) {
|
|
unreadyName := uniqueName("ttserver-unready")
|
|
unready := &terdutv1alpha1.TerdutServer{
|
|
ObjectMeta: metav1.ObjectMeta{Name: unreadyName, Namespace: operatorNamespace},
|
|
Spec: terdutv1alpha1.TerdutServerSpec{
|
|
Image: terdutv1alpha1.ImageSpec{Repository: testImageRepo, Tag: testImageTag},
|
|
Networking: terdutv1alpha1.NetworkingSpec{Hostname: "unready.example.invalid", ServicePort: 8080},
|
|
Database: terdutv1alpha1.DatabaseSpec{DSN: testDSN},
|
|
},
|
|
}
|
|
Expect(k8sClient.Create(ctx, unready)).To(Succeed())
|
|
DeferCleanup(func() { _ = k8sClient.Delete(ctx, unready) })
|
|
|
|
createTeam(ctx, "waiting", terdutv1alpha1.TerdutServerRef{Name: unreadyName})
|
|
reconcileOnce(ctx) // finalizer
|
|
reconcileOnce(ctx)
|
|
|
|
Expect(readyCondition(ctx).Reason).To(Equal(terdutv1alpha1.ReasonWaitingForServer))
|
|
})
|
|
})
|
|
|
|
Describe("cross-namespace serverRef", func() {
|
|
var otherNS string
|
|
|
|
BeforeEach(func(ctx SpecContext) {
|
|
otherNS = uniqueName("ns")
|
|
Expect(k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: otherNS}})).To(Succeed())
|
|
DeferCleanup(func() { _ = k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: otherNS}}) })
|
|
})
|
|
|
|
It("denies by default (From: None)", func(ctx SpecContext) {
|
|
team := &terdutv1alpha1.TerdutTeam{
|
|
ObjectMeta: metav1.ObjectMeta{Name: teamName, Namespace: otherNS},
|
|
Spec: terdutv1alpha1.TerdutTeamSpec{
|
|
ServerRef: terdutv1alpha1.TerdutServerRef{Name: srv.Name, Namespace: operatorNamespace},
|
|
DisplayName: "cross-ns-denied",
|
|
},
|
|
}
|
|
Expect(k8sClient.Create(ctx, team)).To(Succeed())
|
|
DeferCleanup(func() { _ = k8sClient.Delete(ctx, team) })
|
|
|
|
crossKey := types.NamespacedName{Name: teamName, Namespace: otherNS}
|
|
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: crossKey}) // finalizer
|
|
Expect(err).NotTo(HaveOccurred())
|
|
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: crossKey})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(k8sClient.Get(ctx, crossKey, team)).To(Succeed())
|
|
cond := meta.FindStatusCondition(team.Status.Conditions, terdutv1alpha1.ConditionTeamReady)
|
|
Expect(cond.Reason).To(Equal(terdutv1alpha1.ReasonRefNotPermitted))
|
|
})
|
|
|
|
It("permits when the TerdutServer's allowedTeams.namespaces.from is All", func(ctx SpecContext) {
|
|
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: srv.Name, Namespace: operatorNamespace}, srv)).To(Succeed())
|
|
srv.Spec.AllowedTeams.Namespaces.From = "All"
|
|
Expect(k8sClient.Update(ctx, srv)).To(Succeed())
|
|
|
|
team := &terdutv1alpha1.TerdutTeam{
|
|
ObjectMeta: metav1.ObjectMeta{Name: teamName, Namespace: otherNS},
|
|
Spec: terdutv1alpha1.TerdutTeamSpec{
|
|
ServerRef: terdutv1alpha1.TerdutServerRef{Name: srv.Name, Namespace: operatorNamespace},
|
|
DisplayName: "cross-ns-allowed",
|
|
},
|
|
}
|
|
Expect(k8sClient.Create(ctx, team)).To(Succeed())
|
|
DeferCleanup(func() { _ = k8sClient.Delete(ctx, team) })
|
|
|
|
crossKey := types.NamespacedName{Name: teamName, Namespace: otherNS}
|
|
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: crossKey}) // finalizer
|
|
Expect(err).NotTo(HaveOccurred())
|
|
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: crossKey})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(k8sClient.Get(ctx, crossKey, team)).To(Succeed())
|
|
cond := meta.FindStatusCondition(team.Status.Conditions, terdutv1alpha1.ConditionTeamReady)
|
|
// Past the gate: Adopted (the fake server has no reason to
|
|
// reject this), definitely not RefNotPermitted.
|
|
Expect(cond.Reason).To(Equal(terdutv1alpha1.ReasonTeamAdopted))
|
|
})
|
|
})
|
|
|
|
Describe("adopt-on-409 recovery", func() {
|
|
It("adopts an already-created team instead of erroring", func(ctx SpecContext) {
|
|
fake.nextTeamID = 1
|
|
fake.teams["platform"] = 1
|
|
fake.teamNames[1] = "platform"
|
|
|
|
createTeam(ctx, "platform", sameNSRef())
|
|
reconcileOnce(ctx) // finalizer
|
|
reconcileOnce(ctx)
|
|
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
Expect(k8sClient.Get(ctx, teamKey, team)).To(Succeed())
|
|
Expect(team.Status.TeamID).To(Equal(int64(1)))
|
|
Expect(readyCondition(ctx).Status).To(Equal(metav1.ConditionTrue))
|
|
})
|
|
|
|
It("adopts an already-created team-scoped service account instead of erroring", func(ctx SpecContext) {
|
|
createTeam(ctx, "platform", sameNSRef())
|
|
reconcileOnce(ctx) // finalizer
|
|
|
|
// Pre-seed the service account the mint step is about to try to
|
|
// create, simulating an attempt that got this far before being
|
|
// interrupted.
|
|
fake.nextID = 1
|
|
saName := fmt.Sprintf("terdut-team.%s.%s", operatorNamespace, teamName)
|
|
fake.accounts[saName] = 1
|
|
fake.keyMints[1] = 1
|
|
|
|
reconcileOnce(ctx)
|
|
|
|
Expect(readyCondition(ctx).Status).To(Equal(metav1.ConditionTrue))
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
Expect(k8sClient.Get(ctx, teamKey, team)).To(Succeed())
|
|
var credsSecret corev1.Secret
|
|
Expect(k8sClient.Get(ctx, types.NamespacedName{
|
|
Name: team.Status.CredentialsSecretRef.Name, Namespace: operatorNamespace,
|
|
}, &credsSecret)).To(Succeed())
|
|
// Minted fresh (mint2), not the pre-seeded account's original
|
|
// (never-issued-to-this-reconcile) key.
|
|
Expect(string(credsSecret.Data[credentialsSecretDataKey])).To(Equal("instance-key-1-mint2"))
|
|
})
|
|
})
|
|
|
|
Describe("deletion", func() {
|
|
It("deletes the team server-side and removes the credentials Secret", func(ctx SpecContext) {
|
|
createTeam(ctx, "to-delete", sameNSRef())
|
|
reconcileOnce(ctx)
|
|
reconcileOnce(ctx)
|
|
Expect(readyCondition(ctx).Status).To(Equal(metav1.ConditionTrue))
|
|
|
|
team := &terdutv1alpha1.TerdutTeam{}
|
|
Expect(k8sClient.Get(ctx, teamKey, team)).To(Succeed())
|
|
teamID := team.Status.TeamID
|
|
credsName := team.Status.CredentialsSecretRef.Name
|
|
|
|
Expect(k8sClient.Delete(ctx, team)).To(Succeed())
|
|
reconcileOnce(ctx) // runs the finalizer
|
|
|
|
Expect(fake.teamDelete[teamID]).To(BeTrue())
|
|
|
|
err := k8sClient.Get(ctx, teamKey, team)
|
|
Expect(err).To(HaveOccurred(), "the TerdutTeam itself should be gone once the finalizer clears")
|
|
|
|
var leftover corev1.Secret
|
|
err = k8sClient.Get(ctx, types.NamespacedName{Name: credsName, Namespace: operatorNamespace}, &leftover)
|
|
Expect(err).To(HaveOccurred(), "the team-credentials Secret should have been cleaned up")
|
|
})
|
|
})
|
|
})
|