Skip to content

Commit 0ae50c6

Browse files
committed
fix(dashboard): read vm-disk images from the image option source
The vm-disk source picker listed vm-default-images-* PVCs in cozy-public directly, as the tenant, so it kept offering goldens whose import failed even once the image option source leaves them out. The Option list is what the other dynamic dropdowns already read, and it shares their request. With the console no longer listing those PVCs, the tenant dashboard role's read on cozy-public PVCs has no user left, so it goes too. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent b201a0f commit 0ae50c6

5 files changed

Lines changed: 106 additions & 36 deletions

File tree

‎packages/system/cozystack-basics/templates/dashboard-role.yaml‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,3 @@ rules:
1313
- apiGroups: ["source.toolkit.fluxcd.io"]
1414
resources: ["helmcharts"]
1515
verbs: ["get", "list"]
16-
# The console lists PVCs in cozy-public (the vm-default-images-* golden images)
17-
# to populate the source image dropdown when creating a vm-disk. The browser
18-
# issues this list as the tenant identity, so without a read grant here it gets
19-
# 403 and the dropdown is empty. cozy-public is a shared public namespace, so
20-
# read on its PVCs is safe to expose to every tenant dashboard subject.
21-
- apiGroups: [""]
22-
resources: ["persistentvolumeclaims"]
23-
verbs: ["get", "list", "watch"]

‎packages/system/cozystack-basics/tests/dashboard-role_test.yaml‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ release:
77
name: cozystack-basics
88
namespace: cozy-system
99

10-
# The vm-disk source image dropdown lists PersistentVolumeClaims in cozy-public
11-
# (the vm-default-images-* golden images). The console issues that list as the
12-
# tenant identity (e.g. the tenant-root ServiceAccount), so the cozy:tenant:dashboard
13-
# Role must grant read on PVCs; otherwise the browser gets 403 and the dropdown
14-
# is empty even though the images exist.
10+
# The vm-disk source image dropdown reads the image option source from
11+
# cozystack-api, which lists the goldens with its own identity and leaves out
12+
# those whose import failed. Tenants need no read on cozy-public PVCs for it.
1513
tests:
16-
- it: cozy:tenant:dashboard grants read on persistentvolumeclaims in cozy-public
14+
- it: cozy:tenant:dashboard grants no read on persistentvolumeclaims in cozy-public
1715
documentSelector:
1816
path: metadata.name
1917
value: cozy:tenant:dashboard
2018
asserts:
2119
- equal:
2220
path: metadata.namespace
2321
value: cozy-public
24-
- contains:
22+
- notContains:
2523
path: rules
2624
content:
2725
apiGroups: [""]
2826
resources: ["persistentvolumeclaims"]
29-
verbs: ["get", "list", "watch"]
27+
any: true

‎packages/system/dashboard/images/console/apps/console/src/components/DynamicOptionsWidget.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface OptionItem {
2525
default?: boolean
2626
}
2727

28-
interface OptionObject {
28+
export interface OptionObject {
2929
apiVersion: string
3030
kind: string
3131
metadata: { name: string }
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, it, expect, vi } from "vitest"
2+
import { screen } from "@testing-library/react"
3+
import type { FieldProps } from "@rjsf/utils"
4+
import { SourceField } from "./SourceField.tsx"
5+
import { createMockK8sClient } from "../test-utils/mock-k8s-client.ts"
6+
import { renderWithK8sProvider } from "../test-utils/render.tsx"
7+
8+
vi.mock("../lib/tenant-context.tsx", () => ({
9+
useTenantContext: () => ({
10+
tenants: [],
11+
selectedTenant: "root",
12+
selectTenant: () => {},
13+
tenantNamespace: "tenant-root",
14+
isLoading: false,
15+
error: null,
16+
}),
17+
}))
18+
19+
const schema = {
20+
type: "object",
21+
properties: {
22+
image: { type: "object", properties: { name: { type: "string" } } },
23+
},
24+
}
25+
26+
function renderImagePicker() {
27+
const client = createMockK8sClient({
28+
lists: [
29+
{
30+
apiGroup: "core.cozystack.io",
31+
apiVersion: "v1alpha1",
32+
plural: "options",
33+
namespace: "tenant-root",
34+
result: {
35+
apiVersion: "core.cozystack.io/v1alpha1",
36+
kind: "OptionList",
37+
metadata: { resourceVersion: "1" },
38+
items: [
39+
{
40+
apiVersion: "core.cozystack.io/v1alpha1",
41+
kind: "Option",
42+
metadata: { name: "image" },
43+
spec: { items: [{ value: "fedora" }] },
44+
},
45+
],
46+
},
47+
},
48+
// A golden whose import failed still has its PVC; the image option
49+
// source leaves it out, a raw PVC listing would not.
50+
{
51+
apiGroup: "",
52+
apiVersion: "v1",
53+
plural: "persistentvolumeclaims",
54+
namespace: "cozy-public",
55+
result: {
56+
apiVersion: "v1",
57+
kind: "PersistentVolumeClaimList",
58+
metadata: { resourceVersion: "1" },
59+
items: [
60+
{ apiVersion: "v1", kind: "PersistentVolumeClaim", metadata: { name: "vm-default-images-ubuntu", namespace: "cozy-public" } },
61+
],
62+
},
63+
},
64+
],
65+
})
66+
const props = {
67+
schema,
68+
formData: { image: { name: "" } },
69+
onChange: vi.fn(),
70+
name: "source",
71+
required: false,
72+
idSchema: { $id: "root_source" },
73+
} as unknown as FieldProps
74+
return renderWithK8sProvider(<SourceField {...props} />, { client })
75+
}
76+
77+
describe("SourceField image picker", () => {
78+
it("offers the images the image option source serves", async () => {
79+
renderImagePicker()
80+
expect(await screen.findByRole("option", { name: "fedora" })).toBeInTheDocument()
81+
expect(screen.queryByRole("option", { name: "ubuntu" })).not.toBeInTheDocument()
82+
})
83+
})

‎packages/system/dashboard/images/console/apps/console/src/components/SourceField.tsx‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import type { FieldProps } from "@rjsf/utils"
33
import { useK8sList } from "@cozystack/k8s-client"
44
import { APPS_GROUP, APPS_VERSION } from "@cozystack/types"
55
import { useTenantContext } from "../lib/tenant-context.tsx"
6-
7-
const IMAGE_PVC_PREFIX = "vm-default-images-"
6+
import type { OptionObject } from "./DynamicOptionsWidget.tsx"
87

98
interface VMDisk {
109
apiVersion: string
@@ -13,12 +12,6 @@ interface VMDisk {
1312
spec: { storage: string }
1413
}
1514

16-
interface PVC {
17-
apiVersion: string
18-
kind: string
19-
metadata: { name: string; namespace: string }
20-
}
21-
2215
function useVMDiskOptions(tenantNamespace: string | null | undefined) {
2316
const { data, isLoading } = useK8sList<VMDisk>({
2417
apiGroup: APPS_GROUP,
@@ -29,16 +22,20 @@ function useVMDiskOptions(tenantNamespace: string | null | undefined) {
2922
return { disks: data?.items ?? [], isLoading }
3023
}
3124

32-
function useImageOptions() {
33-
const { data, isLoading } = useK8sList<PVC>({
34-
apiGroup: "",
35-
apiVersion: "v1",
36-
plural: "persistentvolumeclaims",
37-
namespace: "cozy-public",
38-
})
39-
const images = (data?.items ?? [])
40-
.filter((pvc) => pvc.metadata.name.startsWith(IMAGE_PVC_PREFIX))
41-
.map((pvc) => pvc.metadata.name.slice(IMAGE_PVC_PREFIX.length))
25+
// Not a raw PVC listing: a golden whose import failed still has its PVC.
26+
function useImageOptions(tenantNamespace: string | null | undefined) {
27+
const { data, isLoading } = useK8sList<OptionObject>(
28+
{
29+
apiGroup: "core.cozystack.io",
30+
apiVersion: "v1alpha1",
31+
plural: "options",
32+
namespace: tenantNamespace ?? undefined,
33+
},
34+
{ enabled: !!tenantNamespace },
35+
)
36+
const images = (
37+
data?.items?.find((o) => o.metadata.name === "image")?.spec?.items ?? []
38+
).map((it) => it.value)
4239
return { images, isLoading }
4340
}
4441

@@ -48,7 +45,7 @@ export function SourceField(props: FieldProps) {
4845
const options = Object.keys(properties)
4946
const { tenantNamespace } = useTenantContext()
5047
const { disks, isLoading: disksLoading } = useVMDiskOptions(tenantNamespace)
51-
const { images, isLoading: imagesLoading } = useImageOptions()
48+
const { images, isLoading: imagesLoading } = useImageOptions(tenantNamespace)
5249

5350
// Determine which option is currently selected
5451
const currentOption = formData

0 commit comments

Comments
 (0)