feat(image): validate ref shape against ImageType before auto-pull#40
Merged
Conversation
EnsureImage drives pulls based on vmCfg.ImageType, but never checked that vmCfg.Image actually has the shape the chosen backend can handle. A cloudimg backend with a bare OCI ref (`simular/win10:22h2-20260510`) ran all the way down to http.Get(), which returned the cryptic `unsupported protocol scheme ""` — the issue 38 failure mode. Add validateRefShape() ahead of Pull: - cloudimg: must be a URL with scheme (http/https); bare refs or local names are imported images that auto-pull can't materialize anyway - oci: must be parseable by name.ParseReference Malformed refs trigger a clear warn-and-return instead of a wasted HTTP round-trip with a misleading error. This is a defensive guard, not the issue 38 fix proper — vk-cocoon still needs to stop writing bare OCI refs into `cocoonstack.snapshot.baseimage` annotations for cloudimg bases. But the guard makes mis-annotated snapshots fail fast with an actionable signal at the cocoon CLI layer.
/code pass on the validateRefShape change: - helpers.go: drop the standalone url.Parse(ref) + Scheme!="" check in favor of the existing IsURL helper. url.Parse accepts non-http schemes (file://, git://) that fail at cloudimg.Pull anyway; IsURL restricts to http(s):// directly, matching the comment and the cloudimg backend's actual contract. Also drops the net/url import. - ensure_image_test.go: add positive-case TestEnsureImage_AcceptsGoodShape to lock in that well-formed cloudimg URLs and tagged OCI refs still reach Pull; add a "cloudimg non-http scheme" (file://) reject case to cover the url.Parse-passes-but-shouldn't trap. - Drop "issue 38" references from godoc and test comment per "don't reference current task/fix" rule; the surrounding behavior stands on its own.
There was a problem hiding this comment.
Pull request overview
Adds an early “ref shape” validation guard in EnsureImage so auto-pull decisions fail fast with a clear warning when vmCfg.ImageType and vmCfg.Image are incompatible (e.g., cloudimg backend receiving a bare OCI-style ref), avoiding misleading downstream errors.
Changes:
- Validate base-image reference shape (
cloudimgmust behttp(s)URL;ocimust parse vianame.ParseReference) before callingPull. - Warn and skip auto-pull on malformed/mismatched refs to prevent wasted network work and confusing errors.
- Add unit tests covering both rejected malformed shapes and accepted well-formed shapes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| cmd/core/helpers.go | Adds validateRefShape and invokes it in EnsureImage before auto-pull. |
| cmd/core/ensure_image_test.go | Adds tests asserting bad ref shapes skip pull and good shapes reach pull. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
EnsureImagedrives pulls based onvmCfg.ImageTypebut never validated thatvmCfg.Imageactually has the shape the chosen backend can handle. Mismatch surfaces deep:This PR adds `validateRefShape(ref, imageType)` ahead of `Pull`:
Malformed refs short-circuit with an actionable warning instead of a wasted HTTP round-trip.
Scope
This is a defensive guard, not the issue 38 fix proper. The real issue 38 fix lives on the vk-cocoon side: stop writing bare OCI refs into `cocoonstack.snapshot.baseimage` annotations for cloudimg bases. Once vk-cocoon writes fully-qualified `/dl/{name}/{ref}` URLs (which the cocoonstack/epoch#4 routes now support), this guard becomes a no-op on the happy path — it only fires when something has gone wrong upstream.
Two complementary benefits independent of issue 38:
Test plan
Followups