-
Notifications
You must be signed in to change notification settings - Fork 198
fix(dashboard): stop promising an image upload the console cannot do #3847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
myasnikovdaniil
wants to merge
3
commits into
main
Choose a base branch
from
feat/console-disk-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
packages/system/dashboard/images/console/apps/console/src/lib/vm-disk-upload.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { describe, it, expect } from "vitest" | ||
| import { | ||
| isUploadSource, | ||
| uploadState, | ||
| usableProxyURL, | ||
| virtctlUploadCommand, | ||
| UPLOAD_PROXY_URL_PLACEHOLDER, | ||
| type DataVolume, | ||
| } from "./vm-disk-upload.ts" | ||
|
|
||
| function dv( | ||
| source: Record<string, unknown> | undefined, | ||
| status?: DataVolume["status"], | ||
| ): DataVolume { | ||
| return { | ||
| apiVersion: "cdi.kubevirt.io/v1beta1", | ||
| kind: "DataVolume", | ||
| metadata: { name: "vm-disk-demo", namespace: "tenant-root" }, | ||
| ...(source === undefined ? {} : { spec: { source } }), | ||
| ...(status ? { status } : {}), | ||
| } | ||
| } | ||
|
|
||
| describe("isUploadSource", () => { | ||
| it("recognises an upload source", () => { | ||
| expect(isUploadSource(dv({ upload: {} }))).toBe(true) | ||
| }) | ||
|
|
||
| it("rejects the other vm-disk sources", () => { | ||
| expect(isUploadSource(dv({ http: { url: "https://example.org/i.qcow2" } }))).toBe(false) | ||
| expect(isUploadSource(dv({ pvc: { name: "vm-default-images-ubuntu" } }))).toBe(false) | ||
| expect(isUploadSource(dv({ blank: {} }))).toBe(false) | ||
| }) | ||
|
|
||
| it("rejects a DataVolume with no source and an unreadable one", () => { | ||
| expect(isUploadSource(dv(undefined))).toBe(false) | ||
| expect(isUploadSource(undefined)).toBe(false) | ||
| }) | ||
|
|
||
| it("does not mistake an inherited property for an upload source", () => { | ||
| // Object.create puts `upload` on the prototype, where `in` would find it. | ||
| const inherited = Object.create({ upload: {} }) as Record<string, unknown> | ||
| expect(isUploadSource(dv(inherited))).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe("uploadState", () => { | ||
| it("is awaiting-upload only at UploadReady — the one phase CDI accepts data in", () => { | ||
| expect(uploadState(dv({ upload: {} }, { phase: "UploadReady" })).stage).toBe( | ||
| "awaiting-upload", | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| "", | ||
| "Pending", | ||
| "PVCBound", | ||
| "WaitForFirstConsumer", | ||
| "PendingPopulation", | ||
| "UploadScheduled", | ||
| ])("treats phase %s as preparing", (phase) => { | ||
| expect(uploadState(dv({ upload: {} }, { phase })).stage).toBe("preparing") | ||
| }) | ||
|
|
||
| it("reports Succeeded and Failed distinctly", () => { | ||
| expect(uploadState(dv({ upload: {} }, { phase: "Succeeded" })).stage).toBe("succeeded") | ||
| expect(uploadState(dv({ upload: {} }, { phase: "Failed" })).stage).toBe("failed") | ||
| }) | ||
|
|
||
| it("surfaces the failure message from the Running condition", () => { | ||
| const state = uploadState( | ||
| dv({ upload: {} }, { | ||
| phase: "Failed", | ||
| conditions: [ | ||
| { type: "Bound", message: "bound" }, | ||
| { type: "Running", message: "upload server crashed" }, | ||
| ], | ||
| }), | ||
| ) | ||
| expect(state.message).toBe("upload server crashed") | ||
| }) | ||
|
|
||
| it("falls back to the Bound condition when Running carries no message", () => { | ||
| const state = uploadState( | ||
| dv({ upload: {} }, { | ||
| phase: "Failed", | ||
| conditions: [{ type: "Bound", message: "no capacity" }], | ||
| }), | ||
| ) | ||
| expect(state.message).toBe("no capacity") | ||
| }) | ||
|
|
||
| it("falls back to the Bound condition when Running carries an empty message", () => { | ||
| const state = uploadState( | ||
| dv({ upload: {} }, { | ||
| phase: "Failed", | ||
| conditions: [ | ||
| { type: "Bound", message: "no capacity" }, | ||
| { type: "Running", message: "" }, | ||
| ], | ||
| }), | ||
| ) | ||
| expect(state.message).toBe("no capacity") | ||
| }) | ||
|
|
||
| it("carries progress through at UploadReady", () => { | ||
| expect( | ||
| uploadState(dv({ upload: {} }, { phase: "UploadReady", progress: "42.0%" })).progress, | ||
| ).toBe("42.0%") | ||
| }) | ||
|
|
||
| it("is unknown for a missing DataVolume and for an unrecognised phase", () => { | ||
| expect(uploadState(undefined).stage).toBe("unknown") | ||
| expect(uploadState(dv({ upload: {} }, { phase: "ImportInProgress" })).stage).toBe( | ||
| "unknown", | ||
| ) | ||
| }) | ||
|
|
||
| it("does not report a phase it was never given", () => { | ||
| expect(uploadState(dv({ upload: {} }, {})).stage).toBe("preparing") | ||
| expect(uploadState(dv({ upload: {} }, {})).phase).toBe("") | ||
| }) | ||
| }) | ||
|
|
||
| describe("usableProxyURL", () => { | ||
| it("treats a blank or whitespace-only URL as no URL at all", () => { | ||
| expect(usableProxyURL(undefined)).toBeUndefined() | ||
| expect(usableProxyURL("")).toBeUndefined() | ||
| expect(usableProxyURL(" ")).toBeUndefined() | ||
| expect(usableProxyURL(" https://cdi-uploadproxy.example.org ")).toBe( | ||
| "https://cdi-uploadproxy.example.org", | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("virtctlUploadCommand", () => { | ||
| it("reuses the chart-created DataVolume and skips certificate verification", () => { | ||
| const cmd = virtctlUploadCommand({ | ||
| name: "vm-disk-demo", | ||
| namespace: "tenant-root", | ||
| uploadProxyURL: "https://cdi-uploadproxy.example.org", | ||
| }) | ||
| expect(cmd).toContain("virtctl image-upload dv vm-disk-demo") | ||
| // The Helm release owns the DataVolume, so creating another one fails. | ||
| expect(cmd).toContain("--no-create") | ||
| // cdi-uploadproxy is published with TLS passthrough: CDI's own cert is on the wire. | ||
| expect(cmd).toContain("--insecure") | ||
| expect(cmd).toContain("--namespace=tenant-root") | ||
| expect(cmd).toContain("--uploadproxy-url=https://cdi-uploadproxy.example.org") | ||
| }) | ||
|
|
||
| it("falls back to a visible placeholder when the cluster published no proxy URL", () => { | ||
| for (const uploadProxyURL of [undefined, "", " "]) { | ||
| const cmd = virtctlUploadCommand({ | ||
| name: "vm-disk-demo", | ||
| namespace: "tenant-root", | ||
| uploadProxyURL, | ||
| }) | ||
| expect(cmd).toContain(`--uploadproxy-url=${UPLOAD_PROXY_URL_PLACEHOLDER}`) | ||
| } | ||
| }) | ||
| }) |
118 changes: 118 additions & 0 deletions
118
packages/system/dashboard/images/console/apps/console/src/lib/vm-disk-upload.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import type { K8sResource } from "@cozystack/k8s-client" | ||
|
|
||
| export interface DataVolumeSpec { | ||
| source?: Record<string, unknown> | ||
| } | ||
|
|
||
| export interface DataVolumeStatus { | ||
| phase?: string | ||
| progress?: string | ||
| conditions?: { | ||
| type?: string | ||
| status?: string | ||
| reason?: string | ||
| message?: string | ||
| }[] | ||
| } | ||
|
|
||
| export type DataVolume = K8sResource<DataVolumeSpec, DataVolumeStatus> | ||
|
|
||
| export interface CDIConfigStatus { | ||
| uploadProxyURL?: string | ||
| } | ||
|
|
||
| export type CDIConfig = K8sResource<unknown, CDIConfigStatus> | ||
|
|
||
| /** | ||
| * Where an upload-source disk sits in the CDI lifecycle. | ||
| * | ||
| * `awaiting-upload` is the only stage in which the CDI upload server exists and | ||
| * accepts data — a token minted at any other stage has nothing to talk to. | ||
| */ | ||
| export type UploadStage = | ||
| | "preparing" | ||
| | "awaiting-upload" | ||
| | "succeeded" | ||
| | "failed" | ||
| | "unknown" | ||
|
|
||
| export interface UploadState { | ||
| stage: UploadStage | ||
| phase: string | ||
| progress?: string | ||
| message?: string | ||
| } | ||
|
|
||
| // CDI DataVolumePhase values that precede the upload server being reachable. | ||
| const PREPARING_PHASES = new Set([ | ||
| "", | ||
| "Pending", | ||
| "PVCBound", | ||
| "WaitForFirstConsumer", | ||
| "PendingPopulation", | ||
| "UploadScheduled", | ||
| ]) | ||
|
|
||
| export function isUploadSource(dv: DataVolume | undefined): boolean { | ||
| const source = dv?.spec?.source | ||
| return !!source && Object.prototype.hasOwnProperty.call(source, "upload") | ||
| } | ||
|
|
||
| function failureMessage(dv: DataVolume): string | undefined { | ||
| const running = dv.status?.conditions?.find((c) => c.type === "Running") | ||
| const bound = dv.status?.conditions?.find((c) => c.type === "Bound") | ||
| // `||`, not `??`: an empty Running message must not shadow the Bound one. | ||
| return running?.message || bound?.message | ||
| } | ||
|
|
||
| export function uploadState(dv: DataVolume | undefined): UploadState { | ||
| if (!dv) return { stage: "unknown", phase: "" } | ||
| const phase = dv.status?.phase ?? "" | ||
| if (phase === "Succeeded") return { stage: "succeeded", phase } | ||
| if (phase === "Failed") { | ||
| return { stage: "failed", phase, message: failureMessage(dv) } | ||
| } | ||
| if (phase === "UploadReady") { | ||
| return { stage: "awaiting-upload", phase, progress: dv.status?.progress } | ||
| } | ||
| if (PREPARING_PHASES.has(phase)) return { stage: "preparing", phase } | ||
| return { stage: "unknown", phase } | ||
| } | ||
|
|
||
| export interface UploadCommandOptions { | ||
| name: string | ||
| namespace: string | ||
| /** CDIConfig.status.uploadProxyURL — absent when the platform published none. */ | ||
| uploadProxyURL?: string | ||
| } | ||
|
|
||
| export const UPLOAD_PROXY_URL_PLACEHOLDER = "https://cdi-uploadproxy.<your-cozystack-domain>" | ||
|
|
||
| /** | ||
| * A whitespace-only `CDIConfig.status.uploadProxyURL` is as unusable as an | ||
| * absent one. The command builder and the panel's placeholder warning both | ||
| * read the URL through this, so they cannot disagree over which values need | ||
| * the placeholder. | ||
| */ | ||
| export function usableProxyURL(uploadProxyURL: string | undefined): string | undefined { | ||
| return uploadProxyURL?.trim() || undefined | ||
| } | ||
|
|
||
| /** | ||
| * The disk's DataVolume is created by the vm-disk Helm release, so the upload | ||
| * has to reuse it (`--no-create`); `--insecure` is required because Cozystack | ||
| * publishes cdi-uploadproxy through TLS passthrough, leaving CDI's internal | ||
| * self-signed certificate on the wire. | ||
| */ | ||
| export function virtctlUploadCommand(opts: UploadCommandOptions): string { | ||
| const proxy = usableProxyURL(opts.uploadProxyURL) ?? UPLOAD_PROXY_URL_PLACEHOLDER | ||
| return [ | ||
| "virtctl image-upload dv", | ||
| opts.name, | ||
| "--no-create", | ||
| `--namespace=${opts.namespace}`, | ||
| "--image-path=./disk.qcow2", | ||
| `--uploadproxy-url=${proxy}`, | ||
| "--insecure", | ||
| ].join(" ") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[NIT] uploadProxyURL interpolated into the shell command without quoting or validation
[NIT]
usableProxyURLonly trims the edges.CDIConfig.status.uploadProxyURLis a free-form platform-controlled string: an internal space silently splits the command's arguments, and a value likehttps://x ; curl ... | shwould execute in the shell of anyone who copies the command. In the cozystack trust model this is not an escalation (the platform admin already owns the console JS), but anew URL()validation or single-quoting the URL is a one-line hardening.name/namespaceare safe (DNS-1123 names of existing objects).