mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
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 <myasnikovdaniil2001@gmail.com>
(cherry picked from commit e44e41d307)
Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com>
This commit is contained in:
4 files changed
+163
-9
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import type { TenantNamespace } from "@cozystack/types"
|
||||
import { realParentNamespace } from "./tenant-tree.ts"
|
||||
|
||||
function tn(name: string, ancestors: string[]): TenantNamespace {
|
||||
const labels: Record<string, string> = {}
|
||||
for (const a of [...ancestors, name]) labels[`tenant.cozystack.io/${a}`] = ""
|
||||
return {
|
||||
apiVersion: "core.cozystack.io/v1alpha1",
|
||||
kind: "TenantNamespace",
|
||||
metadata: { name, labels },
|
||||
} as TenantNamespace
|
||||
}
|
||||
|
||||
describe("realParentNamespace", () => {
|
||||
it("takes the deepest ancestor that prefixes the namespace", () => {
|
||||
// The full chain is on the labels; only the longest prefix is the parent.
|
||||
expect(
|
||||
realParentNamespace(
|
||||
tn("tenant-acme-eu-dev", ["tenant-root", "tenant-acme", "tenant-acme-eu"]),
|
||||
),
|
||||
).toBe("tenant-acme-eu")
|
||||
})
|
||||
|
||||
it("names an inaccessible parent, which is the point of asking", () => {
|
||||
// Visibility is the caller's problem: the CR of a bridged row lives in a
|
||||
// namespace the user may not read, and the caller has to be able to tell.
|
||||
expect(
|
||||
realParentNamespace(tn("tenant-acme-eu", ["tenant-root", "tenant-acme"])),
|
||||
).toBe("tenant-acme")
|
||||
})
|
||||
|
||||
it("resolves a tenant ordered directly in the root", () => {
|
||||
// A tenant `acme` created in `tenant-root` owns `tenant-acme`, not
|
||||
// `tenant-root-acme`, so the prefix rule alone leaves every top-level
|
||||
// tenant — most tenants on a normal cluster — looking parentless.
|
||||
expect(realParentNamespace(tn("tenant-acme", ["tenant-root"]))).toBe(
|
||||
"tenant-root",
|
||||
)
|
||||
})
|
||||
|
||||
it("does not mistake a root-level name that starts with root", () => {
|
||||
// `tenant-root-x` is the tenant `root-x` ordered in the root, and the
|
||||
// prefix branch answers with the same parent the label does.
|
||||
expect(realParentNamespace(tn("tenant-root-x", ["tenant-root"]))).toBe(
|
||||
"tenant-root",
|
||||
)
|
||||
})
|
||||
|
||||
it("has no parent for the hierarchy root", () => {
|
||||
expect(realParentNamespace(tn("tenant-root", []))).toBeUndefined()
|
||||
})
|
||||
|
||||
it("has no parent when the ancestor labels are gone", () => {
|
||||
// A TenantNamespace carrying only its own label is either the root or
|
||||
// mislabelled; either way there is no CR namespace to derive.
|
||||
expect(realParentNamespace(tn("tenant-acme", []))).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { TenantNamespace } from "@cozystack/types"
|
||||
import { ROOT_TENANT_NAMESPACE } from "./constants.ts"
|
||||
import { tenantDisplayName } from "./tenant-context.tsx"
|
||||
|
||||
const ANCESTOR_LABEL_PREFIX = "tenant.cozystack.io/"
|
||||
@@ -76,11 +77,18 @@ export function flattenTenantForest(
|
||||
/**
|
||||
* Row label relative to the visible parent: `tenant-whmcs-crpjxhwm` under
|
||||
* `tenant-whmcs` shows as `crpjxhwm`. Forest roots keep their full display
|
||||
* name — there is no parent on screen to be relative to.
|
||||
* name — there is no parent on screen to be relative to, and neither do the
|
||||
* root's own children, whose namespace never carries the parent's name: under
|
||||
* `tenant-root` the tenant `root-x` owns `tenant-root-x`, and stripping the
|
||||
* prefix there would label it `x` while its CR is `root-x`.
|
||||
*/
|
||||
export function relativeTenantName(node: TenantTreeNode): string {
|
||||
const ns = node.tn.metadata.name
|
||||
if (node.parentNs && ns.startsWith(`${node.parentNs}-`)) {
|
||||
if (
|
||||
node.parentNs &&
|
||||
node.parentNs !== ROOT_TENANT_NAMESPACE &&
|
||||
ns.startsWith(`${node.parentNs}-`)
|
||||
) {
|
||||
return ns.slice(node.parentNs.length + 1)
|
||||
}
|
||||
return tenantDisplayName(node.tn)
|
||||
@@ -96,7 +104,16 @@ export function relativeTenantName(node: TenantTreeNode): string {
|
||||
*/
|
||||
export function realParentNamespace(tn: TenantNamespace): string | undefined {
|
||||
const ns = tn.metadata.name
|
||||
return ancestorNamespaces(tn)
|
||||
const ancestors = ancestorNamespaces(tn)
|
||||
const nested = ancestors
|
||||
.filter((ancestor) => ns.startsWith(`${ancestor}-`))
|
||||
.sort((a, b) => b.length - a.length)[0]
|
||||
if (nested) return nested
|
||||
// The root is the one level the naming rule skips: a tenant `x` ordered in
|
||||
// `tenant-root` owns `tenant-x`, not `tenant-root-x`, so the prefix test
|
||||
// never matches and every top-level tenant — most tenants on a normal
|
||||
// cluster — looked parentless. The ancestor labels still say otherwise.
|
||||
return ancestors.includes(ROOT_TENANT_NAMESPACE)
|
||||
? ROOT_TENANT_NAMESPACE
|
||||
: undefined
|
||||
}
|
||||
+74
-2
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeAll } from "vitest"
|
||||
import { screen } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { useLocation } from "react-router"
|
||||
import {
|
||||
K8sClient,
|
||||
type K8sList,
|
||||
@@ -30,10 +31,15 @@ const VISIBLE = [
|
||||
tn("tenant-whmcs-a-b", ["tenant-root", "tenant-whmcs", "tenant-whmcs-a"]),
|
||||
]
|
||||
|
||||
function makeClient(): K8sClient {
|
||||
function LocationProbe() {
|
||||
const { pathname } = useLocation()
|
||||
return <div data-testid="location">{pathname}</div>
|
||||
}
|
||||
|
||||
function makeClient(tenants: unknown[] = VISIBLE): K8sClient {
|
||||
const client = new K8sClient()
|
||||
vi.spyOn(client, "list").mockImplementation(async (_g, _v, plural) => {
|
||||
const items = plural === "tenantnamespaces" ? VISIBLE : []
|
||||
const items = plural === "tenantnamespaces" ? tenants : []
|
||||
return {
|
||||
apiVersion: "v1",
|
||||
kind: `${plural}List`,
|
||||
@@ -109,3 +115,69 @@ describe("TenantsPage tree", () => {
|
||||
expect(await screen.findByText("x")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
// Tenants ordered directly in the root are the common case on a normal
|
||||
// cluster, and they are the one level the hierarchical namespace naming skips:
|
||||
// `acme` created in `tenant-root` owns `tenant-acme`, not `tenant-root-acme`.
|
||||
const ROOT_VISIBLE = [
|
||||
tn("tenant-root", []),
|
||||
tn("tenant-acme", ["tenant-root"]),
|
||||
tn("tenant-acme-eu", ["tenant-root", "tenant-acme"]),
|
||||
// A root-level tenant whose own name starts with `root`, which is the case
|
||||
// that makes "strip the parent's prefix" the wrong rule rather than merely
|
||||
// an incomplete one.
|
||||
tn("tenant-root-x", ["tenant-root"]),
|
||||
]
|
||||
|
||||
function renderTenants(tenants: unknown[], selected: string) {
|
||||
localStorage.setItem(SELECTED_TENANT_KEY, selected)
|
||||
renderWithK8sProvider(
|
||||
<TenantProvider>
|
||||
<TenantsPage />
|
||||
<LocationProbe />
|
||||
</TenantProvider>,
|
||||
{ client: makeClient(tenants), initialRoute: "/admin/tenants" },
|
||||
)
|
||||
}
|
||||
|
||||
describe("TenantsPage under a visible root", () => {
|
||||
it("offers Edit on a tenant ordered directly in the root", async () => {
|
||||
renderTenants(ROOT_VISIBLE, "root")
|
||||
|
||||
await userEvent.click(await screen.findByTitle("Edit tenant acme"))
|
||||
|
||||
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/acme/edit")
|
||||
})
|
||||
|
||||
it("names a nested tenant's CR relative to its parent", async () => {
|
||||
renderTenants(ROOT_VISIBLE, "root")
|
||||
|
||||
await userEvent.click(await screen.findByTitle("Edit tenant acme-eu"))
|
||||
|
||||
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/eu/edit")
|
||||
})
|
||||
|
||||
// `tenant-root-x` is the tenant `root-x`, not the tenant `x`: the row label
|
||||
// and the CR the row opens have to agree on that.
|
||||
it("keeps a root-level tenant named root-x whole", async () => {
|
||||
renderTenants(ROOT_VISIBLE, "root")
|
||||
|
||||
expect(await screen.findByText("root-x")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByTitle("Edit tenant root-x"))
|
||||
|
||||
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/root-x/edit")
|
||||
})
|
||||
})
|
||||
|
||||
// A forest root that carries no ancestor labels is self-referential: its CR
|
||||
// lives in its own namespace, and deriving the CR name must not subtract the
|
||||
// node's own name from itself and leave nothing.
|
||||
describe("TenantsPage self-referential root", () => {
|
||||
it("names the CR of a root that is not tenant-root", async () => {
|
||||
renderTenants([tn("tenant-whmcs", [])], "whmcs")
|
||||
|
||||
await userEvent.click(await screen.findByTitle("Edit tenant whmcs"))
|
||||
|
||||
expect(screen.getByTestId("location")).toHaveTextContent("/admin/tenants/whmcs/edit")
|
||||
})
|
||||
})
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
relativeTenantName,
|
||||
type TenantTreeNode,
|
||||
} from "../lib/tenant-tree.ts"
|
||||
import { TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
|
||||
import { ROOT_TENANT_NAMESPACE, TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
|
||||
import { formatAge } from "../lib/status.ts"
|
||||
import { TenantQuotaCompact } from "../components/QuotaDisplay.tsx"
|
||||
import type { ResourceQuota } from "../components/QuotaDisplay.tsx"
|
||||
@@ -34,11 +34,17 @@ type TreeNode = TenantTreeNode
|
||||
/**
|
||||
* The Tenant CR of a namespace lives in its parent's namespace under the
|
||||
* short name: `tenant-whmcs-jzmnbwum` under `tenant-whmcs` is CR `jzmnbwum`.
|
||||
* Root-level children (`tenant-kvaps` under `tenant-root`) collapse to the
|
||||
* same rule via the plain `tenant-` prefix strip.
|
||||
* Under the root the naming rule drops a level — a tenant `kvaps` ordered in
|
||||
* `tenant-root` owns `tenant-kvaps` — so there the CR name is the plain
|
||||
* prefix strip, which also names the root's own self-referential CR (`root`).
|
||||
* Excluding the root from the prefix branch rather than dropping the branch
|
||||
* keeps two things right at once: a root-level tenant called `root-x`
|
||||
* resolves to the CR `root-x` and not `x`, and a self-referential node whose
|
||||
* namespace is not `tenant-root` still names itself instead of the empty
|
||||
* string that subtracting its own name would leave.
|
||||
*/
|
||||
function tenantCrName(ns: string, parentNs: string): string {
|
||||
return ns.startsWith(`${parentNs}-`)
|
||||
return parentNs !== ROOT_TENANT_NAMESPACE && ns.startsWith(`${parentNs}-`)
|
||||
? ns.slice(parentNs.length + 1)
|
||||
: ns.slice(TENANT_NAMESPACE_PREFIX.length)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user