Skip to content

Commit e44e41d

Browse files
fix(dashboard): resolve the Tenant CR of a tenant ordered in the root
A tenant's CR lives in its parent's namespace, and the console recovered the parent from the namespace name: `tenant-a-b` sits under `tenant-a`. The root is the one level that naming skips. A tenant `acme` ordered in `tenant-root` owns `tenant-acme`, not `tenant-root-acme`, so the prefix test never matched a top-level tenant and `realParentNamespace` returned undefined for it. That is most tenants on a normal cluster, and the Edit button, offered only where the CR's namespace resolves, was missing for all of them. The ancestor labels carry the parent the name does not, so the lookup falls back to `tenant-root` when the chain names it. Deriving the CR name keys on the parent being the root rather than on the child's name happening to start with it, which keeps a root-level tenant called `root-x` from resolving to the CR `x`, and the row label agrees with it rather than showing `x`. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent e1e925a commit e44e41d

4 files changed

Lines changed: 163 additions & 9 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, it, expect } from "vitest"
2+
import type { TenantNamespace } from "@cozystack/types"
3+
import { realParentNamespace } from "./tenant-tree.ts"
4+
5+
function tn(name: string, ancestors: string[]): TenantNamespace {
6+
const labels: Record<string, string> = {}
7+
for (const a of [...ancestors, name]) labels[`tenant.cozystack.io/${a}`] = ""
8+
return {
9+
apiVersion: "core.cozystack.io/v1alpha1",
10+
kind: "TenantNamespace",
11+
metadata: { name, labels },
12+
} as TenantNamespace
13+
}
14+
15+
describe("realParentNamespace", () => {
16+
it("takes the deepest ancestor that prefixes the namespace", () => {
17+
// The full chain is on the labels; only the longest prefix is the parent.
18+
expect(
19+
realParentNamespace(
20+
tn("tenant-acme-eu-dev", ["tenant-root", "tenant-acme", "tenant-acme-eu"]),
21+
),
22+
).toBe("tenant-acme-eu")
23+
})
24+
25+
it("names an inaccessible parent, which is the point of asking", () => {
26+
// Visibility is the caller's problem: the CR of a bridged row lives in a
27+
// namespace the user may not read, and the caller has to be able to tell.
28+
expect(
29+
realParentNamespace(tn("tenant-acme-eu", ["tenant-root", "tenant-acme"])),
30+
).toBe("tenant-acme")
31+
})
32+
33+
it("resolves a tenant ordered directly in the root", () => {
34+
// A tenant `acme` created in `tenant-root` owns `tenant-acme`, not
35+
// `tenant-root-acme`, so the prefix rule alone leaves every top-level
36+
// tenant — most tenants on a normal cluster — looking parentless.
37+
expect(realParentNamespace(tn("tenant-acme", ["tenant-root"]))).toBe(
38+
"tenant-root",
39+
)
40+
})
41+
42+
it("does not mistake a root-level name that starts with root", () => {
43+
// `tenant-root-x` is the tenant `root-x` ordered in the root, and the
44+
// prefix branch answers with the same parent the label does.
45+
expect(realParentNamespace(tn("tenant-root-x", ["tenant-root"]))).toBe(
46+
"tenant-root",
47+
)
48+
})
49+
50+
it("has no parent for the hierarchy root", () => {
51+
expect(realParentNamespace(tn("tenant-root", []))).toBeUndefined()
52+
})
53+
54+
it("has no parent when the ancestor labels are gone", () => {
55+
// A TenantNamespace carrying only its own label is either the root or
56+
// mislabelled; either way there is no CR namespace to derive.
57+
expect(realParentNamespace(tn("tenant-acme", []))).toBeUndefined()
58+
})
59+
})

‎packages/system/dashboard/images/console/apps/console/src/lib/tenant-tree.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TenantNamespace } from "@cozystack/types"
2+
import { ROOT_TENANT_NAMESPACE } from "./constants.ts"
23
import { tenantDisplayName } from "./tenant-context.tsx"
34

45
const ANCESTOR_LABEL_PREFIX = "tenant.cozystack.io/"
@@ -76,11 +77,18 @@ export function flattenTenantForest(
7677
/**
7778
* Row label relative to the visible parent: `tenant-whmcs-crpjxhwm` under
7879
* `tenant-whmcs` shows as `crpjxhwm`. Forest roots keep their full display
79-
* name — there is no parent on screen to be relative to.
80+
* name — there is no parent on screen to be relative to, and neither do the
81+
* root's own children, whose namespace never carries the parent's name: under
82+
* `tenant-root` the tenant `root-x` owns `tenant-root-x`, and stripping the
83+
* prefix there would label it `x` while its CR is `root-x`.
8084
*/
8185
export function relativeTenantName(node: TenantTreeNode): string {
8286
const ns = node.tn.metadata.name
83-
if (node.parentNs && ns.startsWith(`${node.parentNs}-`)) {
87+
if (
88+
node.parentNs &&
89+
node.parentNs !== ROOT_TENANT_NAMESPACE &&
90+
ns.startsWith(`${node.parentNs}-`)
91+
) {
8492
return ns.slice(node.parentNs.length + 1)
8593
}
8694
return tenantDisplayName(node.tn)
@@ -96,7 +104,16 @@ export function relativeTenantName(node: TenantTreeNode): string {
96104
*/
97105
export function realParentNamespace(tn: TenantNamespace): string | undefined {
98106
const ns = tn.metadata.name
99-
return ancestorNamespaces(tn)
107+
const ancestors = ancestorNamespaces(tn)
108+
const nested = ancestors
100109
.filter((ancestor) => ns.startsWith(`${ancestor}-`))
101110
.sort((a, b) => b.length - a.length)[0]
111+
if (nested) return nested
112+
// The root is the one level the naming rule skips: a tenant `x` ordered in
113+
// `tenant-root` owns `tenant-x`, not `tenant-root-x`, so the prefix test
114+
// never matches and every top-level tenant — most tenants on a normal
115+
// cluster — looked parentless. The ancestor labels still say otherwise.
116+
return ancestors.includes(ROOT_TENANT_NAMESPACE)
117+
? ROOT_TENANT_NAMESPACE
118+
: undefined
102119
}

‎packages/system/dashboard/images/console/apps/console/src/routes/TenantsPage.test.tsx‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect, vi, beforeAll } from "vitest"
22
import { screen } from "@testing-library/react"
33
import userEvent from "@testing-library/user-event"
4+
import { useLocation } from "react-router"
45
import {
56
K8sClient,
67
type K8sList,
@@ -30,10 +31,15 @@ const VISIBLE = [
3031
tn("tenant-whmcs-a-b", ["tenant-root", "tenant-whmcs", "tenant-whmcs-a"]),
3132
]
3233

33-
function makeClient(): K8sClient {
34+
function LocationProbe() {
35+
const { pathname } = useLocation()
36+
return <div data-testid="location">{pathname}</div>
37+
}
38+
39+
function makeClient(tenants: unknown[] = VISIBLE): K8sClient {
3440
const client = new K8sClient()
3541
vi.spyOn(client, "list").mockImplementation(async (_g, _v, plural) => {
36-
const items = plural === "tenantnamespaces" ? VISIBLE : []
42+
const items = plural === "tenantnamespaces" ? tenants : []
3743
return {
3844
apiVersion: "v1",
3945
kind: `${plural}List`,
@@ -109,3 +115,69 @@ describe("TenantsPage tree", () => {
109115
expect(await screen.findByText("x")).toBeInTheDocument()
110116
})
111117
})
118+
119+
// Tenants ordered directly in the root are the common case on a normal
120+
// cluster, and they are the one level the hierarchical namespace naming skips:
121+
// `acme` created in `tenant-root` owns `tenant-acme`, not `tenant-root-acme`.
122+
const ROOT_VISIBLE = [
123+
tn("tenant-root", []),
124+
tn("tenant-acme", ["tenant-root"]),
125+
tn("tenant-acme-eu", ["tenant-root", "tenant-acme"]),
126+
// A root-level tenant whose own name starts with `root`, which is the case
127+
// that makes "strip the parent's prefix" the wrong rule rather than merely
128+
// an incomplete one.
129+
tn("tenant-root-x", ["tenant-root"]),
130+
]
131+
132+
function renderTenants(tenants: unknown[], selected: string) {
133+
localStorage.setItem(SELECTED_TENANT_KEY, selected)
134+
renderWithK8sProvider(
135+
<TenantProvider>
136+
<TenantsPage />
137+
<LocationProbe />
138+
</TenantProvider>,
139+
{ client: makeClient(tenants), initialRoute: "/admin/tenants" },
140+
)
141+
}
142+
143+
describe("TenantsPage under a visible root", () => {
144+
it("offers Edit on a tenant ordered directly in the root", async () => {
145+
renderTenants(ROOT_VISIBLE, "root")
146+
147+
await userEvent.click(await screen.findByTitle("Edit tenant acme"))
148+
149+
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/acme/edit")
150+
})
151+
152+
it("names a nested tenant's CR relative to its parent", async () => {
153+
renderTenants(ROOT_VISIBLE, "root")
154+
155+
await userEvent.click(await screen.findByTitle("Edit tenant acme-eu"))
156+
157+
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/eu/edit")
158+
})
159+
160+
// `tenant-root-x` is the tenant `root-x`, not the tenant `x`: the row label
161+
// and the CR the row opens have to agree on that.
162+
it("keeps a root-level tenant named root-x whole", async () => {
163+
renderTenants(ROOT_VISIBLE, "root")
164+
165+
expect(await screen.findByText("root-x")).toBeInTheDocument()
166+
await userEvent.click(screen.getByTitle("Edit tenant root-x"))
167+
168+
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/root-x/edit")
169+
})
170+
})
171+
172+
// A forest root that carries no ancestor labels is self-referential: its CR
173+
// lives in its own namespace, and deriving the CR name must not subtract the
174+
// node's own name from itself and leave nothing.
175+
describe("TenantsPage self-referential root", () => {
176+
it("names the CR of a root that is not tenant-root", async () => {
177+
renderTenants([tn("tenant-whmcs", [])], "whmcs")
178+
179+
await userEvent.click(await screen.findByTitle("Edit tenant whmcs"))
180+
181+
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/whmcs/edit")
182+
})
183+
})

‎packages/system/dashboard/images/console/apps/console/src/routes/TenantsPage.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
relativeTenantName,
1414
type TenantTreeNode,
1515
} from "../lib/tenant-tree.ts"
16-
import { TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
16+
import { ROOT_TENANT_NAMESPACE, TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
1717
import { formatAge } from "../lib/status.ts"
1818
import { TenantQuotaCompact } from "../components/QuotaDisplay.tsx"
1919
import type { ResourceQuota } from "../components/QuotaDisplay.tsx"
@@ -34,11 +34,17 @@ type TreeNode = TenantTreeNode
3434
/**
3535
* The Tenant CR of a namespace lives in its parent's namespace under the
3636
* short name: `tenant-whmcs-jzmnbwum` under `tenant-whmcs` is CR `jzmnbwum`.
37-
* Root-level children (`tenant-kvaps` under `tenant-root`) collapse to the
38-
* same rule via the plain `tenant-` prefix strip.
37+
* Under the root the naming rule drops a level — a tenant `kvaps` ordered in
38+
* `tenant-root` owns `tenant-kvaps` — so there the CR name is the plain
39+
* prefix strip, which also names the root's own self-referential CR (`root`).
40+
* Excluding the root from the prefix branch rather than dropping the branch
41+
* keeps two things right at once: a root-level tenant called `root-x`
42+
* resolves to the CR `root-x` and not `x`, and a self-referential node whose
43+
* namespace is not `tenant-root` still names itself instead of the empty
44+
* string that subtracting its own name would leave.
3945
*/
4046
function tenantCrName(ns: string, parentNs: string): string {
41-
return ns.startsWith(`${parentNs}-`)
47+
return parentNs !== ROOT_TENANT_NAMESPACE && ns.startsWith(`${parentNs}-`)
4248
? ns.slice(parentNs.length + 1)
4349
: ns.slice(TENANT_NAMESPACE_PREFIX.length)
4450
}

0 commit comments

Comments
 (0)