← Back to Pull requests

VEP 395: Support direct OCI layer fetch with Layer option #4301

Merged
Acedus merged 35 files (1802 lines) on Nov 24, 2026 19:13 UTC
View on GitHub
Acedus Acedus commented on Oct 01, 2026 12:54 UTC
Author

<!-- Thanks for sending a pull request! Here are some tips for you:
1. Follow the instructions for writing a release note from k8s: https://git.k8s.io/community/contributors/guide/release-notes.md
-->

What this PR does / why we need it:
This PR implements the CDI portion of https://github.com/kubevirt/enhancements/issues/395. It adds a Layer field to DataVolumeSourceRegistry that enables filtering and fetching a single OCI blob directly according to a set of annotations.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:
<!-- Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->
``release-note
Added Layer option for Registry DataSource to allow importing disk images from specific OCI image layers by filtering with annotations.
``

Diff: 1802 lines (+1519 / -283) across 35 files Reviewer: nav1s Review duration: 78139m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.72) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.72 · 1802 lines diff across 35 files
0.72
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
45 items
Acedus Acedus commented on Oct 01, 2026 12:54 UTC
Author

/cc @0xFelix @akalenyu @nav1s

kubevirt-prow[bot] kubevirt-prow[bot] commented on Oct 01, 2026 12:54 UTC
Bot

@Acedus: GitHub didn't allow me to request PR reviews from the following users: nav1s.

Note that only kubevirt members and repo collaborators can review this PR, and authors cannot review their own PRs.

<details>

In response to this:

>/cc @0xFelix @akalenyu @nav1s

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
</details>

0xFelix 0xFelix commented on code on Oct 01, 2026 13:03 UTC
Code Review
pkg/apiserver/webhooks/util.go:1
@@ -158,6 +158,34 @@ func validateDataVolumeSourceRegistry(sourceRegistry *cdiv1.DataVolumeSourceRegi
 		return causes
 	}
 
+	if sourceRegistry.Layer != nil {
+		if importMethod != nil && *importMethod == cdiv1.RegistryPullNode {
+			causes = append(causes, metav1.StatusCause{
+				Type:    metav1.CauseTypeFieldValueNotSupported,
+				Message: "Node pull import method is not supported with Layer",
+				Field:   field.Child("source", "registry", "layer").String(),
+			})
+			return causes
+		}
+		matchAnnotations := sourceRegistry.Layer.MatchAnnotations
+		if len(matchAnnotations) == 0 {
+			causes = append(causes, metav1.StatusCause{
+				Type:    metav1.CauseTypeFieldValueRequired,
+				Message: "Source registry Layer has to select an annotation to match",
+				Field:   field.Child("source", "registry", "layer", "matchAnnotations").String(),
+			})
+			return causes
+		}
+		if _, ok := matchAnnotations[""]; ok {
+			causes = append(causes, metav1.StatusCause{
+				Type:    metav1.CauseTypeFieldValueInvalid,
+				Message: "Source registry Layer annotation key cannot be empty",

				Message: "Source registry Layer selector cannot contain an empty key",

coveralls coveralls commented on Oct 01, 2026 13:08 UTC
Contributor

![Coverage Status](https://coveralls.io/builds/83126599)

coverage: 50.688% (+0.3%) from 50.376% — Acedus:registry-ds-layer-support into kubevirt:main

0xFelix 0xFelix commented on code on Oct 01, 2026 13:09 UTC
Code Review
pkg/apiserver/webhooks/datavolume-validate_test.go:1
@@ -195,6 +195,37 @@ var _ = Describe("Validating Webhook", func() {
 			Expect(resp.Allowed).To(BeTrue())
 		})
 
+		Context("registry with Layer", func() {
+
+			newLayerDataVolume := func(matchAnnotations map[string]string) *cdiv1.DataVolume {
+				dataVolume := newRegistryDataVolume("testDV", "docker://registry:5000/test")
+				dataVolume.Spec.Source.Registry.Layer = &cdiv1.LayerSelector{MatchAnnotations: matchAnnotations}
+				return dataVolume
+			}
+			diskName := map[string]string{"org.example.disk.name": "rootdisk"}
+
+			DescribeTable("should validate Registry source Layer with PullMethod", func(pullMethod *cdiv1.RegistryPullMethod, expected bool) {
+				dataVolume := newLayerDataVolume(diskName)
+				dataVolume.Spec.Source.Registry.PullMethod = pullMethod
+				resp := validateDataVolumeCreate(dataVolume)
+				Expect(resp.Allowed).To(Equal(expected))
+			},
+				Entry("reject with node PullMethod", ptr.To(cdiv1.RegistryPullNode), false),
+				Entry("accept with pod PullMethod", ptr.To(cdiv1.RegistryPullPod), true),
+				Entry("accept with no PullMethod", nil, true),
+			)
+
+			DescribeTable("should validate Registry source Layer annotations", func(matchAnnotations map[string]string, expected bool) {
+				resp := validateDataVolumeCreate(newLayerDataVolume(matchAnnotations))
+				Expect(resp.Allowed).To(Equal(expected))
+			},
+				Entry("reject for no annotations", map[string]string{}, false),

Add case for nil map?

0xFelix 0xFelix commented on code on Oct 01, 2026 13:14 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)
+	if err != nil {
+		return nil, "", fmt.Errorf("Error retrieving manifest: %w", err)
+	}
+	if mimeType == "" {
+		mimeType = manifest.GuessMIMEType(manBlob)
+	}
+	return manBlob, mimeType, nil
+}
+
+func parseOCIManifest(manBlob []byte, mimeType string) (*manifest.OCI1, error) {
+	if mimeType != imgspecv1.MediaTypeImageManifest {

This is pretty loose at the moment, allowing to supply any image that has just this mediaType. What about asserting on well known artifactTypes as well? We can only work with application/vnd.kubevirt.virtualmachine.v1 and application/vnd.kubevirt.virtualmachinetemplate.v1 as specified by VEP 256.

0xFelix 0xFelix commented on code on Oct 01, 2026 13:15 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {

Similar for layers, as for manifests. Filter on the mediaType, to only allow well known / supported media types?

0xFelix 0xFelix commented on code on Oct 01, 2026 13:16 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {

application/vnd.kubevirt.disk.raw+zstd + application/vnd.kubevirt.persistentstate.tar+zstd after https://github.com/kubevirt/enhancements/pull/464 was merged.

0xFelix 0xFelix commented on code on Oct 01, 2026 13:19 UTC
Code Review
tests/import_test.go:1
@@ -2278,6 +2278,76 @@ var _ = Describe("Multi-arch image pull", func() {
 	})
 })
 
+var _ = FDescribe("OCI artifact import", func() {

Press F to pay respects

0xFelix 0xFelix commented on code on Oct 01, 2026 13:22 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index

In that case, a user could receive a different Platform than they've requested? Should it not fail instead? I.e. cannot restrict with Platform on a plain manifest.

Acedus Acedus commented on code on Oct 01, 2026 13:30 UTC
Code Review
tests/import_test.go:1
@@ -2278,6 +2278,76 @@ var _ = Describe("Multi-arch image pull", func() {
 	})
 })
 
+var _ = FDescribe("OCI artifact import", func() {

F

Acedus Acedus commented on code on Oct 01, 2026 13:32 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)
+	if err != nil {
+		return nil, "", fmt.Errorf("Error retrieving manifest: %w", err)
+	}
+	if mimeType == "" {
+		mimeType = manifest.GuessMIMEType(manBlob)
+	}
+	return manBlob, mimeType, nil
+}
+
+func parseOCIManifest(manBlob []byte, mimeType string) (*manifest.OCI1, error) {
+	if mimeType != imgspecv1.MediaTypeImageManifest {

I've purposefully made it rather generic and deliberately hoped to avoid mentioning kubevirt wherever possible. I don't mind doing that, though.

Acedus Acedus commented on code on Oct 01, 2026 13:40 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index

I agree, but it would mean that virt-import would need to avoid passing a platform option for what it resolves to be a plain manifest.

0xFelix 0xFelix commented on code on Oct 02, 2026 13:50 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)
+	if err != nil {
+		return nil, "", fmt.Errorf("Error retrieving manifest: %w", err)
+	}
+	if mimeType == "" {
+		mimeType = manifest.GuessMIMEType(manBlob)
+	}
+	return manBlob, mimeType, nil
+}
+
+func parseOCIManifest(manBlob []byte, mimeType string) (*manifest.OCI1, error) {
+	if mimeType != imgspecv1.MediaTypeImageManifest {

IIUC the importer recursively detects the file format it works with, i.e. you can stack formats freely? What would happen with a media type of application/vnd.oci.image.layer.v1.tar or application/vnd.oci.image.layer.v1.tar+gzip? If that works we don't have to limit it to the known media types.

0xFelix 0xFelix commented on code on Oct 02, 2026 13:50 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)
+	if err != nil {
+		return nil, "", fmt.Errorf("Error retrieving manifest: %w", err)
+	}
+	if mimeType == "" {
+		mimeType = manifest.GuessMIMEType(manBlob)
+	}
+	return manBlob, mimeType, nil
+}
+
+func parseOCIManifest(manBlob []byte, mimeType string) (*manifest.OCI1, error) {
+	if mimeType != imgspecv1.MediaTypeImageManifest {

With the above media types it would have to look for a *.img file again.

0xFelix 0xFelix commented on code on Oct 02, 2026 13:51 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index

If that's not supported anyway, I'm happy to avoid it in virt-import.

Acedus Acedus commented on code on Oct 04, 2026 12:24 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)
+	if err != nil {
+		return nil, "", fmt.Errorf("Error retrieving manifest: %w", err)
+	}
+	if mimeType == "" {
+		mimeType = manifest.GuessMIMEType(manBlob)
+	}
+	return manBlob, mimeType, nil
+}
+
+func parseOCIManifest(manBlob []byte, mimeType string) (*manifest.OCI1, error) {
+	if mimeType != imgspecv1.MediaTypeImageManifest {

I guess we can limit on artifactType for starters .

Acedus Acedus commented on code on Oct 04, 2026 12:42 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {

~~That's fair, I forgot we need to support application/vnd.kubevirt.persistentstate.tar+zstd, let me see what we can do.~~

EDIT: Left my thoughts on the VEP.

nav1s nav1s commented on code on Oct 08, 2026 12:27 UTC
Code Review
pkg/importer/registry-container.go:1
@@ -0,0 +1,136 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"archive/tar"
+	"context"
+	"errors"
+	"fmt"
+	"io"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/image"
+	"github.com/containers/image/v5/types"
+
+	"k8s.io/klog/v2"
+)
+
+// ErrBootcImageDetected is returned when a bootc/ostree-bootable container image is detected
+// but conversion is not yet implemented.
+var ErrBootcImageDetected = errors.New("bootc image detected: this image contains an ostree-based bootable OS (containers.bootc=1 or ostree.bootable=1) and cannot be imported as a regular container disk; bootc-to-disk conversion is not yet implemented")
+
+const (
+	whFilePrefix = ".wh."
+)
+
+// containerDisk is a container disk image, the disk image being the first file under pathPrefix.
+type containerDisk struct {
+	disk       diskWriter
+	pathPrefix string
+}
+
+func (c containerDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	img, err := image.FromUnparsedImage(ctx, sys, image.UnparsedInstance(src, nil))
+	if err != nil {
+		klog.Errorf("Error retrieving image: %v", err)
+		return nil, nil, fmt.Errorf("Error retrieving image: %w", err)
+	}
+
+	info, err := img.Inspect(ctx)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Error inspecting image: %w", err)
+	}
+	if err := validateImagePlatformMatch(sys, info); err != nil {
+		return nil, nil, err
+	}
+	if err := checkBootcImage(info); err != nil {
+		return nil, nil, err
+	}
+	return img.LayerInfos(), info, nil
+}
+
+func (c containerDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		klog.Errorf("%v: %v", errReadingLayer, err)
+		return false, fmt.Errorf("%w: %v", errReadingLayer, err)
+	}
+	defer readers.Close()
+
+	tarReader := tar.NewReader(readers.TopReader())
+	for {
+		hdr, err := tarReader.Next()
+		if errors.Is(err, io.EOF) {
+			return false, nil // End of archive
+		}
+		if err != nil {
+			klog.Errorf("%v: %v", errReadingLayer, err)
+			return false, fmt.Errorf("%w: %v", errReadingLayer, err)
+		}
+		if !c.wants(hdr) {
+			continue
+		}
+
+		klog.Infof("File '%v' found in the layer", hdr.Name)
+		if err := c.disk.write(tarReader, hdr.Name); err != nil {
+			return false, err
+		}
+		return true, nil
+	}
+}
+
+func (c containerDisk) wants(hdr *tar.Header) bool {
+	return hasPrefix(hdr.Name, c.pathPrefix) && !isWhiteout(hdr.Name) && !isDir(hdr)
+}
+
+func hasPrefix(path string, pathPrefix string) bool {
+	return strings.HasPrefix(path, pathPrefix) ||
+		strings.HasPrefix(path, "./"+pathPrefix)
+}
+
+func isWhiteout(path string) bool {
+	return strings.HasPrefix(filepath.Base(path), whFilePrefix)
+}
+
+func isDir(hdr *tar.Header) bool {
+	return hdr.Typeflag == tar.TypeDir
+}
+
+const (

nit: should we move these consts to the const block at the top?

nav1s nav1s commented on code on Oct 08, 2026 12:31 UTC
Code Review
pkg/importer/registry-datasource.go:1
@@ -54,6 +53,7 @@ type RegistryDataSource struct {
 	accessKey         string
 	secKey            string
 	imageArchitecture string
+	layer             *cdiv1.LayerSelector // nil for a container image

nit: shouldn't this be: "nil for container disk image"?

nav1s nav1s commented on code on Oct 08, 2026 12:38 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {

should we support nested indexes here?

nav1s nav1s commented on code on Oct 08, 2026 13:16 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)

should we verify the digest we got here is what we ordered? (as far as I can tell this is the caller's responsibility), reference

nav1s nav1s commented on Oct 08, 2026 13:18 UTC
Reviewer

Thanks @Acedus! looking pretty sharp, few comments below

Acedus Acedus commented on code on Nov 08, 2026 13:52 UTC
Code Review
pkg/importer/registry-container.go:1
@@ -0,0 +1,136 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"archive/tar"
+	"context"
+	"errors"
+	"fmt"
+	"io"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/image"
+	"github.com/containers/image/v5/types"
+
+	"k8s.io/klog/v2"
+)
+
+// ErrBootcImageDetected is returned when a bootc/ostree-bootable container image is detected
+// but conversion is not yet implemented.
+var ErrBootcImageDetected = errors.New("bootc image detected: this image contains an ostree-based bootable OS (containers.bootc=1 or ostree.bootable=1) and cannot be imported as a regular container disk; bootc-to-disk conversion is not yet implemented")
+
+const (
+	whFilePrefix = ".wh."
+)
+
+// containerDisk is a container disk image, the disk image being the first file under pathPrefix.
+type containerDisk struct {
+	disk       diskWriter
+	pathPrefix string
+}
+
+func (c containerDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	img, err := image.FromUnparsedImage(ctx, sys, image.UnparsedInstance(src, nil))
+	if err != nil {
+		klog.Errorf("Error retrieving image: %v", err)
+		return nil, nil, fmt.Errorf("Error retrieving image: %w", err)
+	}
+
+	info, err := img.Inspect(ctx)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Error inspecting image: %w", err)
+	}
+	if err := validateImagePlatformMatch(sys, info); err != nil {
+		return nil, nil, err
+	}
+	if err := checkBootcImage(info); err != nil {
+		return nil, nil, err
+	}
+	return img.LayerInfos(), info, nil
+}
+
+func (c containerDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		klog.Errorf("%v: %v", errReadingLayer, err)
+		return false, fmt.Errorf("%w: %v", errReadingLayer, err)
+	}
+	defer readers.Close()
+
+	tarReader := tar.NewReader(readers.TopReader())
+	for {
+		hdr, err := tarReader.Next()
+		if errors.Is(err, io.EOF) {
+			return false, nil // End of archive
+		}
+		if err != nil {
+			klog.Errorf("%v: %v", errReadingLayer, err)
+			return false, fmt.Errorf("%w: %v", errReadingLayer, err)
+		}
+		if !c.wants(hdr) {
+			continue
+		}
+
+		klog.Infof("File '%v' found in the layer", hdr.Name)
+		if err := c.disk.write(tarReader, hdr.Name); err != nil {
+			return false, err
+		}
+		return true, nil
+	}
+}
+
+func (c containerDisk) wants(hdr *tar.Header) bool {
+	return hasPrefix(hdr.Name, c.pathPrefix) && !isWhiteout(hdr.Name) && !isDir(hdr)
+}
+
+func hasPrefix(path string, pathPrefix string) bool {
+	return strings.HasPrefix(path, pathPrefix) ||
+		strings.HasPrefix(path, "./"+pathPrefix)
+}
+
+func isWhiteout(path string) bool {
+	return strings.HasPrefix(filepath.Base(path), whFilePrefix)
+}
+
+func isDir(hdr *tar.Header) bool {
+	return hdr.Typeflag == tar.TypeDir
+}
+
+const (

It's actually idiomatic to keep consts separate depending on their logical use near their consumers in Go.

Acedus Acedus commented on code on Nov 08, 2026 13:52 UTC
Code Review
pkg/importer/registry-datasource.go:1
@@ -54,6 +53,7 @@ type RegistryDataSource struct {
 	accessKey         string
 	secKey            string
 	imageArchitecture string
+	layer             *cdiv1.LayerSelector // nil for a container image

No, imports aren't just for container disks.

Acedus Acedus commented on code on Nov 08, 2026 14:00 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {

We could support it later as a follow-up if we add support for a producer that does it.

Acedus Acedus commented on code on Nov 08, 2026 14:03 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,166 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource, _ types.BlobInfoCache) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := list.ChooseInstance(sys)
+		if err != nil {
+			return nil, fmt.Errorf("Error selecting a manifest from the image index: %w", err)
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	}
+
+	// a plain manifest declares no platform, the requested one only selects from an index
+	return parseOCIManifest(manBlob, mimeType)
+}
+
+func getManifest(ctx context.Context, src types.ImageSource, instanceDigest *digest.Digest) ([]byte, string, error) {
+	manBlob, mimeType, err := src.GetManifest(ctx, instanceDigest)

Good catch.

Acedus Acedus commented on Nov 08, 2026 14:12 UTC
Author

@nav1s @0xFelix I believe it's ready now.

0xFelix 0xFelix commented on code on Nov 11, 2026 13:11 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,207 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/image"
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var supportedArtifactTypes = map[string]bool{
+	"application/vnd.kubevirt.virtualmachine.v1":         true,
+	"application/vnd.kubevirt.virtualmachinetemplate.v1": true,
+}
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+	errPlatformUndeclared = errors.New("The manifest declares no platform to check the requested architecture against")
+	errArtifactType       = errors.New("Layer selection requires a valid artifact type")
+	errLayerTar           = errors.New("The selected layer is a tar archive rather than a disk image")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)

IIUC then src.GetBlob is passed as open. It doesn't verify the blob against layer.Digest. Wrap it in layer.Digest.Verifier() and fail if it's not Verified()?

0xFelix 0xFelix commented on code on Nov 11, 2026 13:11 UTC
Code Review
pkg/importer/registry-artifact_test.go:1
@@ -0,0 +1,272 @@
+package importer
+
+import (
+	"archive/tar"
+	"bytes"
+	"compress/gzip"
+	"context"
+	"crypto/rand"
+	"encoding/json"
+	"os"
+	"path/filepath"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	"github.com/opencontainers/image-spec/specs-go"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+var _ = Describe("Registry artifact import", func() {
+	matching := func(annotations map[string]string) *cdiv1.LayerSelector {
+		return &cdiv1.LayerSelector{MatchAnnotations: annotations}
+	}
+
+	Context("selecting the layer", func() {
+		const (
+			nameAnnotation = "org.example.disk.name"
+			sizeAnnotation = "org.example.disk.size"
+		)
+		named := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{nameAnnotation: name})
+		}
+
+		rootdisk := types.BlobInfo{Digest: "sha256:root", Annotations: map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}}
+		datadisk := types.BlobInfo{Digest: "sha256:data", Annotations: map[string]string{nameAnnotation: "datadisk", sizeAnnotation: "10Gi"}}
+		layers := []types.BlobInfo{rootdisk, datadisk}
+
+		DescribeTable("should select the one layer carrying every annotation", func(selector *cdiv1.LayerSelector, expected types.BlobInfo) {
+			Expect(selectLayer(layers, selector)).To(Equal(expected))
+		},
+			Entry("the first layer", named("rootdisk"), rootdisk),
+			Entry("a later layer", named("datadisk"), datadisk),
+			Entry("by several annotations", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}), rootdisk),
+		)
+
+		DescribeTable("should fail when the annotations select no layer", func(selector *cdiv1.LayerSelector) {
+			_, err := selectLayer(layers, selector)
+			Expect(err).To(MatchError(errLayerNotFound))
+		},
+			Entry("when no layer carries the annotation", matching(map[string]string{"org.example.disk.unknown": "rootdisk"})),
+			Entry("when no layer annotation has the value", named("unknowndisk")),
+			Entry("when only some of the annotations match", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "5Gi"})),
+		)
+
+		It("should refuse a selector with no annotations rather than match every layer", func() {
+			_, err := selectLayer([]types.BlobInfo{rootdisk}, matching(nil))
+			Expect(err).To(MatchError(errEmptyLayerSelector))
+		})
+
+		It("should fail when the annotations select more than one layer", func() {
+			_, err := selectLayer(layers, matching(map[string]string{sizeAnnotation: "10Gi"}))
+			Expect(err).To(MatchError(errLayerAmbiguous))
+			Expect(err).To(MatchError(ContainSubstring("match 2 layers (sha256:root, sha256:data)")))
+		})
+	})
+
+	Context("parsing the artifact manifest", func() {
+		ociManifest := func(artifactType string) []byte {
+			manBlob, err := json.Marshal(imgspecv1.Manifest{
+				Versioned:    specs.Versioned{SchemaVersion: 2},
+				MediaType:    imgspecv1.MediaTypeImageManifest,
+				ArtifactType: artifactType,
+				Config:       imgspecv1.DescriptorEmptyJSON,
+			})
+			Expect(err).ToNot(HaveOccurred())
+			return manBlob
+		}
+
+		DescribeTable("should accept the KubeVirt artifact types", func(artifactType string) {
+			man, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).ToNot(HaveOccurred())
+			Expect(man.ArtifactType).To(Equal(artifactType))
+		},
+			Entry("a virtual machine", "application/vnd.kubevirt.virtualmachine.v1"),
+			Entry("a virtual machine template", "application/vnd.kubevirt.virtualmachinetemplate.v1"),
+		)
+
+		DescribeTable("should refuse any other manifest", func(artifactType string) {
+			_, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).To(MatchError(errArtifactType))
+		},
+			Entry("a container image, declaring no artifact type", ""),
+			Entry("an artifact of another type", "application/vnd.example.disk.v1"),
+			Entry("an artifact of an unknown version", "application/vnd.kubevirt.virtualmachine.v2"),
+		)
+	})
+
+	Context("choosing the manifest of an image index", func() {
+		var (
+			amd64Entry = imgspecv1.Descriptor{Digest: digest.FromString("amd64"), Platform: &imgspecv1.Platform{OS: "linux", Architecture: "amd64"}}
+			bareEntry  = imgspecv1.Descriptor{Digest: digest.FromString("no platform")}
+		)
+
+		DescribeTable("should choose the entry", func(architecture string, entries []imgspecv1.Descriptor, expected imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			Expect(chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: architecture}, index)).To(Equal(expected.Digest))
+		},
+			Entry("declaring no platform when no architecture is requested", "", []imgspecv1.Descriptor{bareEntry}, bareEntry),
+			Entry("declaring the requested architecture over one declaring no platform", "amd64", []imgspecv1.Descriptor{bareEntry, amd64Entry}, amd64Entry),
+		)
+
+		DescribeTable("should refuse an entry declaring no platform when an architecture is requested", func(entries []imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			_, err := chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: "arm64"}, index)
+			Expect(err).To(MatchError(errPlatformUndeclared))
+		},
+			Entry("when it is the only entry", []imgspecv1.Descriptor{bareEntry}),
+			Entry("when no other entry matches the architecture", []imgspecv1.Descriptor{amd64Entry, bareEntry}),
+		)
+	})
+
+	Context("extracting the disk image", func() {
+		rawDisk := func(content string) []byte {
+			return bytes.Repeat([]byte(content), 1024)
+		}
+
+		var destDir string
+
+		BeforeEach(func() {
+			destDir = GinkgoT().TempDir()
+		})
+
+		artifact := func() artifactDisk {
+			return artifactDisk{disk: diskWriter{destDir: destDir}, pathPrefix: "disk"}
+		}
+		diskImage := func() string {
+			return filepath.Join(destDir, "disk", "disk.img")
+		}
+
+		It("should write out the whole blob of the layer", func() {
+			layers, open := openLayers(rawDisk("root"))
+
+			Expect(extractDisk(context.Background(), artifact(), layers, open)).To(Succeed())
+			Expect(os.ReadFile(diskImage())).To(Equal(rawDisk("root")))
+		})
+
+		DescribeTable("should refuse a tar layer rather than import the archive as a raw disk", func(compress func([]byte) []byte) {
+			// random content keeps the compressed layer longer than the header the format is detected by
+			content := make([]byte, 4096)
+			Expect(rand.Read(content)).To(Equal(len(content)))
+			var archive bytes.Buffer
+			tw := tar.NewWriter(&archive)
+			Expect(tw.WriteHeader(&tar.Header{Name: "disk.img", Mode: 0600, Size: int64(len(content))})).To(Succeed())
+			Expect(tw.Write(content)).To(Equal(len(content)))
+			Expect(tw.Close()).To(Succeed())
+			layers, open := openLayers(compress(archive.Bytes()))
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(errLayerTar))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		},
+			Entry("an uncompressed tar", func(b []byte) []byte { return b }),
+			Entry("a gzipped tar", func(b []byte) []byte {
+				var gz bytes.Buffer
+				zw := gzip.NewWriter(&gz)
+				_, err := zw.Write(b)
+				Expect(err).ToNot(HaveOccurred())
+				Expect(zw.Close()).To(Succeed())
+				return gz.Bytes()
+			}),
+		)
+
+		It("should fail on the layer being unreadable rather than report it holds no disk", func() {
+			layers, open := openLayers(nil)
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(ContainSubstring("layer unavailable")))
+			Expect(err).ToNot(MatchError(errDiskImageNotFound))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		})
+	})
+
+	Context("from a registry", func() {
+		// the fixtures follow the layout KubeVirt exports, which names each disk layer by this annotation
+		const diskNameAnnotation = "io.kubevirt.disk.name"
+		diskNamed := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{diskNameAnnotation: name})
+		}
+
+		var tmpDir string
+
+		BeforeEach(func() {
+			tmpDir = GinkgoT().TempDir()
+		})
+
+		diskImage := func() string {
+			return filepath.Join(tmpDir, containerDiskImageDir, artifactDiskImageName)
+		}
+
+		When("the artifact is an image index", func() {
+			// an OCI artifact: an image index with an amd64 manifest holding a rootdisk and a

Doesn't have to be this PR, but it would be good to know how to reproduce the test artifacts, i.e. with a script?

0xFelix 0xFelix commented on code on Nov 11, 2026 13:11 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,207 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/image"
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var supportedArtifactTypes = map[string]bool{
+	"application/vnd.kubevirt.virtualmachine.v1":         true,
+	"application/vnd.kubevirt.virtualmachinetemplate.v1": true,
+}
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+	errPlatformUndeclared = errors.New("The manifest declares no platform to check the requested architecture against")
+	errArtifactType       = errors.New("Layer selection requires a valid artifact type")
+	errLayerTar           = errors.New("The selected layer is a tar archive rather than a disk image")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
+	if err != nil {
+		return false, fmt.Errorf("Error reading layer %v: %w", layer.Digest, err)
+	}
+	defer readers.Close()
+	if readers.Tar {
+		// a tar holds no disk format qemu-img recognizes, so it would be imported as a raw disk
+		return false, fmt.Errorf("%w: %v", errLayerTar, layer.Digest)
+	}
+
+	if err := a.disk.write(readers.TopReader(), filepath.Join(a.pathPrefix, artifactDiskImageName)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func selectLayer(layers []types.BlobInfo, selector *cdiv1.LayerSelector) (types.BlobInfo, error) {
+	matchAnnotations := selector.MatchAnnotations
+	if len(matchAnnotations) == 0 {
+		// matching nothing would match every layer
+		return types.BlobInfo{}, errEmptyLayerSelector
+	}
+
+	var matched []types.BlobInfo
+	for _, layer := range layers {
+		if matchesAnnotations(layer.Annotations, matchAnnotations) {
+			matched = append(matched, layer)
+		}
+	}
+
+	switch len(matched) {
+	case 1:
+		return matched[0], nil
+	case 0:
+		return types.BlobInfo{}, fmt.Errorf("%w: %v", errLayerNotFound, matchAnnotations)
+	default:
+		digests := make([]string, 0, len(matched))
+		for _, layer := range matched {
+			digests = append(digests, layer.Digest.String())
+		}
+		return types.BlobInfo{}, fmt.Errorf("%w: %v match %d layers (%s)",
+			errLayerAmbiguous, matchAnnotations, len(matched), strings.Join(digests, ", "))
+	}
+}
+
+func matchesAnnotations(annotations, matchAnnotations map[string]string) bool {
+	for key, value := range matchAnnotations {
+		if got, ok := annotations[key]; !ok || got != value {
+			return false
+		}
+	}
+	return true
+}
+
+func resolveArtifactLayers(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, error) {
+	man, err := resolveArtifactManifest(ctx, sys, src)
+	if err != nil {
+		return nil, err
+	}
+
+	layers := make([]types.BlobInfo, 0, len(man.Layers))
+	for _, layer := range man.Layers {
+		layers = append(layers, manifest.BlobInfoFromOCI1Descriptor(layer))
+	}
+	return layers, nil
+}
+
+func resolveArtifactManifest(ctx context.Context, sys *types.SystemContext, src types.ImageSource) (*manifest.OCI1, error) {
+	manBlob, mimeType, err := getManifest(ctx, src, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	if manifest.MIMETypeIsMultiImage(mimeType) {
+		list, err := manifest.ListFromBlob(manBlob, mimeType)
+		if err != nil {
+			return nil, fmt.Errorf("Error parsing image index: %w", err)
+		}
+		instance, err := chooseArtifactInstance(sys, list)
+		if err != nil {
+			return nil, err
+		}
+		if manBlob, mimeType, err = getManifest(ctx, src, &instance); err != nil {
+			return nil, err
+		}
+	} else if sys.ArchitectureChoice != "" {

VEP table still says verify Platform on a plain manifest, will update the VEP to say refuse.

0xFelix 0xFelix commented on code on Nov 11, 2026 13:21 UTC
Code Review
pkg/importer/registry-artifact.go:1
@@ -0,0 +1,207 @@
+/*
+Copyright 2026 The CDI Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package importer
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/containers/image/v5/image"
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+// artifactDiskImageName names the disk image, since a layer blob carries no file name.
+const artifactDiskImageName = "disk.img"
+
+var supportedArtifactTypes = map[string]bool{
+	"application/vnd.kubevirt.virtualmachine.v1":         true,
+	"application/vnd.kubevirt.virtualmachinetemplate.v1": true,
+}
+
+var (
+	errEmptyLayerSelector = errors.New("The layer selector has no annotations to match")
+	errLayerNotFound      = errors.New("No layer of the manifest carries the selected annotations")
+	errLayerAmbiguous     = errors.New("The selected annotations have to match a single layer of the manifest")
+	errPlatformUndeclared = errors.New("The manifest declares no platform to check the requested architecture against")
+	errArtifactType       = errors.New("Layer selection requires a valid artifact type")
+	errLayerTar           = errors.New("The selected layer is a tar archive rather than a disk image")
+)
+
+// artifactDisk is an OCI artifact, the disk image being the whole blob of the selected layer.
+type artifactDisk struct {
+	disk       diskWriter
+	pathPrefix string
+	selector   *cdiv1.LayerSelector
+}
+
+func (a artifactDisk) resolve(ctx context.Context, sys *types.SystemContext, src types.ImageSource) ([]types.BlobInfo, *types.ImageInspectInfo, error) {
+	layers, err := resolveArtifactLayers(ctx, sys, src)
+	if err != nil {
+		return nil, nil, err
+	}
+	layer, err := selectLayer(layers, a.selector)
+	if err != nil {
+		return nil, nil, err
+	}
+	return []types.BlobInfo{layer}, nil, nil
+}
+
+func (a artifactDisk) read(ctx context.Context, layer types.BlobInfo, open blobOpener) (bool, error) {
+	readers, err := decompressLayer(ctx, layer, open)
Acedus Acedus commented on code on Nov 11, 2026 15:07 UTC
Code Review
pkg/importer/registry-artifact_test.go:1
@@ -0,0 +1,272 @@
+package importer
+
+import (
+	"archive/tar"
+	"bytes"
+	"compress/gzip"
+	"context"
+	"crypto/rand"
+	"encoding/json"
+	"os"
+	"path/filepath"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	"github.com/opencontainers/image-spec/specs-go"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+var _ = Describe("Registry artifact import", func() {
+	matching := func(annotations map[string]string) *cdiv1.LayerSelector {
+		return &cdiv1.LayerSelector{MatchAnnotations: annotations}
+	}
+
+	Context("selecting the layer", func() {
+		const (
+			nameAnnotation = "org.example.disk.name"
+			sizeAnnotation = "org.example.disk.size"
+		)
+		named := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{nameAnnotation: name})
+		}
+
+		rootdisk := types.BlobInfo{Digest: "sha256:root", Annotations: map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}}
+		datadisk := types.BlobInfo{Digest: "sha256:data", Annotations: map[string]string{nameAnnotation: "datadisk", sizeAnnotation: "10Gi"}}
+		layers := []types.BlobInfo{rootdisk, datadisk}
+
+		DescribeTable("should select the one layer carrying every annotation", func(selector *cdiv1.LayerSelector, expected types.BlobInfo) {
+			Expect(selectLayer(layers, selector)).To(Equal(expected))
+		},
+			Entry("the first layer", named("rootdisk"), rootdisk),
+			Entry("a later layer", named("datadisk"), datadisk),
+			Entry("by several annotations", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}), rootdisk),
+		)
+
+		DescribeTable("should fail when the annotations select no layer", func(selector *cdiv1.LayerSelector) {
+			_, err := selectLayer(layers, selector)
+			Expect(err).To(MatchError(errLayerNotFound))
+		},
+			Entry("when no layer carries the annotation", matching(map[string]string{"org.example.disk.unknown": "rootdisk"})),
+			Entry("when no layer annotation has the value", named("unknowndisk")),
+			Entry("when only some of the annotations match", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "5Gi"})),
+		)
+
+		It("should refuse a selector with no annotations rather than match every layer", func() {
+			_, err := selectLayer([]types.BlobInfo{rootdisk}, matching(nil))
+			Expect(err).To(MatchError(errEmptyLayerSelector))
+		})
+
+		It("should fail when the annotations select more than one layer", func() {
+			_, err := selectLayer(layers, matching(map[string]string{sizeAnnotation: "10Gi"}))
+			Expect(err).To(MatchError(errLayerAmbiguous))
+			Expect(err).To(MatchError(ContainSubstring("match 2 layers (sha256:root, sha256:data)")))
+		})
+	})
+
+	Context("parsing the artifact manifest", func() {
+		ociManifest := func(artifactType string) []byte {
+			manBlob, err := json.Marshal(imgspecv1.Manifest{
+				Versioned:    specs.Versioned{SchemaVersion: 2},
+				MediaType:    imgspecv1.MediaTypeImageManifest,
+				ArtifactType: artifactType,
+				Config:       imgspecv1.DescriptorEmptyJSON,
+			})
+			Expect(err).ToNot(HaveOccurred())
+			return manBlob
+		}
+
+		DescribeTable("should accept the KubeVirt artifact types", func(artifactType string) {
+			man, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).ToNot(HaveOccurred())
+			Expect(man.ArtifactType).To(Equal(artifactType))
+		},
+			Entry("a virtual machine", "application/vnd.kubevirt.virtualmachine.v1"),
+			Entry("a virtual machine template", "application/vnd.kubevirt.virtualmachinetemplate.v1"),
+		)
+
+		DescribeTable("should refuse any other manifest", func(artifactType string) {
+			_, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).To(MatchError(errArtifactType))
+		},
+			Entry("a container image, declaring no artifact type", ""),
+			Entry("an artifact of another type", "application/vnd.example.disk.v1"),
+			Entry("an artifact of an unknown version", "application/vnd.kubevirt.virtualmachine.v2"),
+		)
+	})
+
+	Context("choosing the manifest of an image index", func() {
+		var (
+			amd64Entry = imgspecv1.Descriptor{Digest: digest.FromString("amd64"), Platform: &imgspecv1.Platform{OS: "linux", Architecture: "amd64"}}
+			bareEntry  = imgspecv1.Descriptor{Digest: digest.FromString("no platform")}
+		)
+
+		DescribeTable("should choose the entry", func(architecture string, entries []imgspecv1.Descriptor, expected imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			Expect(chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: architecture}, index)).To(Equal(expected.Digest))
+		},
+			Entry("declaring no platform when no architecture is requested", "", []imgspecv1.Descriptor{bareEntry}, bareEntry),
+			Entry("declaring the requested architecture over one declaring no platform", "amd64", []imgspecv1.Descriptor{bareEntry, amd64Entry}, amd64Entry),
+		)
+
+		DescribeTable("should refuse an entry declaring no platform when an architecture is requested", func(entries []imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			_, err := chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: "arm64"}, index)
+			Expect(err).To(MatchError(errPlatformUndeclared))
+		},
+			Entry("when it is the only entry", []imgspecv1.Descriptor{bareEntry}),
+			Entry("when no other entry matches the architecture", []imgspecv1.Descriptor{amd64Entry, bareEntry}),
+		)
+	})
+
+	Context("extracting the disk image", func() {
+		rawDisk := func(content string) []byte {
+			return bytes.Repeat([]byte(content), 1024)
+		}
+
+		var destDir string
+
+		BeforeEach(func() {
+			destDir = GinkgoT().TempDir()
+		})
+
+		artifact := func() artifactDisk {
+			return artifactDisk{disk: diskWriter{destDir: destDir}, pathPrefix: "disk"}
+		}
+		diskImage := func() string {
+			return filepath.Join(destDir, "disk", "disk.img")
+		}
+
+		It("should write out the whole blob of the layer", func() {
+			layers, open := openLayers(rawDisk("root"))
+
+			Expect(extractDisk(context.Background(), artifact(), layers, open)).To(Succeed())
+			Expect(os.ReadFile(diskImage())).To(Equal(rawDisk("root")))
+		})
+
+		DescribeTable("should refuse a tar layer rather than import the archive as a raw disk", func(compress func([]byte) []byte) {
+			// random content keeps the compressed layer longer than the header the format is detected by
+			content := make([]byte, 4096)
+			Expect(rand.Read(content)).To(Equal(len(content)))
+			var archive bytes.Buffer
+			tw := tar.NewWriter(&archive)
+			Expect(tw.WriteHeader(&tar.Header{Name: "disk.img", Mode: 0600, Size: int64(len(content))})).To(Succeed())
+			Expect(tw.Write(content)).To(Equal(len(content)))
+			Expect(tw.Close()).To(Succeed())
+			layers, open := openLayers(compress(archive.Bytes()))
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(errLayerTar))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		},
+			Entry("an uncompressed tar", func(b []byte) []byte { return b }),
+			Entry("a gzipped tar", func(b []byte) []byte {
+				var gz bytes.Buffer
+				zw := gzip.NewWriter(&gz)
+				_, err := zw.Write(b)
+				Expect(err).ToNot(HaveOccurred())
+				Expect(zw.Close()).To(Succeed())
+				return gz.Bytes()
+			}),
+		)
+
+		It("should fail on the layer being unreadable rather than report it holds no disk", func() {
+			layers, open := openLayers(nil)
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(ContainSubstring("layer unavailable")))
+			Expect(err).ToNot(MatchError(errDiskImageNotFound))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		})
+	})
+
+	Context("from a registry", func() {
+		// the fixtures follow the layout KubeVirt exports, which names each disk layer by this annotation
+		const diskNameAnnotation = "io.kubevirt.disk.name"
+		diskNamed := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{diskNameAnnotation: name})
+		}
+
+		var tmpDir string
+
+		BeforeEach(func() {
+			tmpDir = GinkgoT().TempDir()
+		})
+
+		diskImage := func() string {
+			return filepath.Join(tmpDir, containerDiskImageDir, artifactDiskImageName)
+		}
+
+		When("the artifact is an image index", func() {
+			// an OCI artifact: an image index with an amd64 manifest holding a rootdisk and a

Sure, I can write something up.

0xFelix 0xFelix commented on code on Nov 11, 2026 15:33 UTC
Code Review
pkg/importer/registry-artifact_test.go:1
@@ -0,0 +1,272 @@
+package importer
+
+import (
+	"archive/tar"
+	"bytes"
+	"compress/gzip"
+	"context"
+	"crypto/rand"
+	"encoding/json"
+	"os"
+	"path/filepath"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	"github.com/opencontainers/image-spec/specs-go"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+var _ = Describe("Registry artifact import", func() {
+	matching := func(annotations map[string]string) *cdiv1.LayerSelector {
+		return &cdiv1.LayerSelector{MatchAnnotations: annotations}
+	}
+
+	Context("selecting the layer", func() {
+		const (
+			nameAnnotation = "org.example.disk.name"
+			sizeAnnotation = "org.example.disk.size"
+		)
+		named := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{nameAnnotation: name})
+		}
+
+		rootdisk := types.BlobInfo{Digest: "sha256:root", Annotations: map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}}
+		datadisk := types.BlobInfo{Digest: "sha256:data", Annotations: map[string]string{nameAnnotation: "datadisk", sizeAnnotation: "10Gi"}}
+		layers := []types.BlobInfo{rootdisk, datadisk}
+
+		DescribeTable("should select the one layer carrying every annotation", func(selector *cdiv1.LayerSelector, expected types.BlobInfo) {
+			Expect(selectLayer(layers, selector)).To(Equal(expected))
+		},
+			Entry("the first layer", named("rootdisk"), rootdisk),
+			Entry("a later layer", named("datadisk"), datadisk),
+			Entry("by several annotations", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}), rootdisk),
+		)
+
+		DescribeTable("should fail when the annotations select no layer", func(selector *cdiv1.LayerSelector) {
+			_, err := selectLayer(layers, selector)
+			Expect(err).To(MatchError(errLayerNotFound))
+		},
+			Entry("when no layer carries the annotation", matching(map[string]string{"org.example.disk.unknown": "rootdisk"})),
+			Entry("when no layer annotation has the value", named("unknowndisk")),
+			Entry("when only some of the annotations match", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "5Gi"})),
+		)
+
+		It("should refuse a selector with no annotations rather than match every layer", func() {
+			_, err := selectLayer([]types.BlobInfo{rootdisk}, matching(nil))
+			Expect(err).To(MatchError(errEmptyLayerSelector))
+		})
+
+		It("should fail when the annotations select more than one layer", func() {
+			_, err := selectLayer(layers, matching(map[string]string{sizeAnnotation: "10Gi"}))
+			Expect(err).To(MatchError(errLayerAmbiguous))
+			Expect(err).To(MatchError(ContainSubstring("match 2 layers (sha256:root, sha256:data)")))
+		})
+	})
+
+	Context("parsing the artifact manifest", func() {
+		ociManifest := func(artifactType string) []byte {
+			manBlob, err := json.Marshal(imgspecv1.Manifest{
+				Versioned:    specs.Versioned{SchemaVersion: 2},
+				MediaType:    imgspecv1.MediaTypeImageManifest,
+				ArtifactType: artifactType,
+				Config:       imgspecv1.DescriptorEmptyJSON,
+			})
+			Expect(err).ToNot(HaveOccurred())
+			return manBlob
+		}
+
+		DescribeTable("should accept the KubeVirt artifact types", func(artifactType string) {
+			man, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).ToNot(HaveOccurred())
+			Expect(man.ArtifactType).To(Equal(artifactType))
+		},
+			Entry("a virtual machine", "application/vnd.kubevirt.virtualmachine.v1"),
+			Entry("a virtual machine template", "application/vnd.kubevirt.virtualmachinetemplate.v1"),
+		)
+
+		DescribeTable("should refuse any other manifest", func(artifactType string) {
+			_, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).To(MatchError(errArtifactType))
+		},
+			Entry("a container image, declaring no artifact type", ""),
+			Entry("an artifact of another type", "application/vnd.example.disk.v1"),
+			Entry("an artifact of an unknown version", "application/vnd.kubevirt.virtualmachine.v2"),
+		)
+	})
+
+	Context("choosing the manifest of an image index", func() {
+		var (
+			amd64Entry = imgspecv1.Descriptor{Digest: digest.FromString("amd64"), Platform: &imgspecv1.Platform{OS: "linux", Architecture: "amd64"}}
+			bareEntry  = imgspecv1.Descriptor{Digest: digest.FromString("no platform")}
+		)
+
+		DescribeTable("should choose the entry", func(architecture string, entries []imgspecv1.Descriptor, expected imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			Expect(chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: architecture}, index)).To(Equal(expected.Digest))
+		},
+			Entry("declaring no platform when no architecture is requested", "", []imgspecv1.Descriptor{bareEntry}, bareEntry),
+			Entry("declaring the requested architecture over one declaring no platform", "amd64", []imgspecv1.Descriptor{bareEntry, amd64Entry}, amd64Entry),
+		)
+
+		DescribeTable("should refuse an entry declaring no platform when an architecture is requested", func(entries []imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			_, err := chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: "arm64"}, index)
+			Expect(err).To(MatchError(errPlatformUndeclared))
+		},
+			Entry("when it is the only entry", []imgspecv1.Descriptor{bareEntry}),
+			Entry("when no other entry matches the architecture", []imgspecv1.Descriptor{amd64Entry, bareEntry}),
+		)
+	})
+
+	Context("extracting the disk image", func() {
+		rawDisk := func(content string) []byte {
+			return bytes.Repeat([]byte(content), 1024)
+		}
+
+		var destDir string
+
+		BeforeEach(func() {
+			destDir = GinkgoT().TempDir()
+		})
+
+		artifact := func() artifactDisk {
+			return artifactDisk{disk: diskWriter{destDir: destDir}, pathPrefix: "disk"}
+		}
+		diskImage := func() string {
+			return filepath.Join(destDir, "disk", "disk.img")
+		}
+
+		It("should write out the whole blob of the layer", func() {
+			layers, open := openLayers(rawDisk("root"))
+
+			Expect(extractDisk(context.Background(), artifact(), layers, open)).To(Succeed())
+			Expect(os.ReadFile(diskImage())).To(Equal(rawDisk("root")))
+		})
+
+		DescribeTable("should refuse a tar layer rather than import the archive as a raw disk", func(compress func([]byte) []byte) {
+			// random content keeps the compressed layer longer than the header the format is detected by
+			content := make([]byte, 4096)
+			Expect(rand.Read(content)).To(Equal(len(content)))
+			var archive bytes.Buffer
+			tw := tar.NewWriter(&archive)
+			Expect(tw.WriteHeader(&tar.Header{Name: "disk.img", Mode: 0600, Size: int64(len(content))})).To(Succeed())
+			Expect(tw.Write(content)).To(Equal(len(content)))
+			Expect(tw.Close()).To(Succeed())
+			layers, open := openLayers(compress(archive.Bytes()))
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(errLayerTar))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		},
+			Entry("an uncompressed tar", func(b []byte) []byte { return b }),
+			Entry("a gzipped tar", func(b []byte) []byte {
+				var gz bytes.Buffer
+				zw := gzip.NewWriter(&gz)
+				_, err := zw.Write(b)
+				Expect(err).ToNot(HaveOccurred())
+				Expect(zw.Close()).To(Succeed())
+				return gz.Bytes()
+			}),
+		)
+
+		It("should fail on the layer being unreadable rather than report it holds no disk", func() {
+			layers, open := openLayers(nil)
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(ContainSubstring("layer unavailable")))
+			Expect(err).ToNot(MatchError(errDiskImageNotFound))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		})
+	})
+
+	Context("from a registry", func() {
+		// the fixtures follow the layout KubeVirt exports, which names each disk layer by this annotation
+		const diskNameAnnotation = "io.kubevirt.disk.name"
+		diskNamed := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{diskNameAnnotation: name})
+		}
+
+		var tmpDir string
+
+		BeforeEach(func() {
+			tmpDir = GinkgoT().TempDir()
+		})
+
+		diskImage := func() string {
+			return filepath.Join(tmpDir, containerDiskImageDir, artifactDiskImageName)
+		}
+
+		When("the artifact is an image index", func() {
+			// an OCI artifact: an image index with an amd64 manifest holding a rootdisk and a

See recent PRs in virt-import, there's code to create artifact fixtures: https://github.com/kubevirt/virt-import/pull/6

Acedus Acedus commented on code on Nov 11, 2026 15:35 UTC
Code Review
pkg/importer/registry-artifact_test.go:1
@@ -0,0 +1,272 @@
+package importer
+
+import (
+	"archive/tar"
+	"bytes"
+	"compress/gzip"
+	"context"
+	"crypto/rand"
+	"encoding/json"
+	"os"
+	"path/filepath"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+
+	"github.com/containers/image/v5/manifest"
+	"github.com/containers/image/v5/types"
+	"github.com/opencontainers/go-digest"
+	"github.com/opencontainers/image-spec/specs-go"
+	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
+
+	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
+)
+
+var _ = Describe("Registry artifact import", func() {
+	matching := func(annotations map[string]string) *cdiv1.LayerSelector {
+		return &cdiv1.LayerSelector{MatchAnnotations: annotations}
+	}
+
+	Context("selecting the layer", func() {
+		const (
+			nameAnnotation = "org.example.disk.name"
+			sizeAnnotation = "org.example.disk.size"
+		)
+		named := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{nameAnnotation: name})
+		}
+
+		rootdisk := types.BlobInfo{Digest: "sha256:root", Annotations: map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}}
+		datadisk := types.BlobInfo{Digest: "sha256:data", Annotations: map[string]string{nameAnnotation: "datadisk", sizeAnnotation: "10Gi"}}
+		layers := []types.BlobInfo{rootdisk, datadisk}
+
+		DescribeTable("should select the one layer carrying every annotation", func(selector *cdiv1.LayerSelector, expected types.BlobInfo) {
+			Expect(selectLayer(layers, selector)).To(Equal(expected))
+		},
+			Entry("the first layer", named("rootdisk"), rootdisk),
+			Entry("a later layer", named("datadisk"), datadisk),
+			Entry("by several annotations", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "10Gi"}), rootdisk),
+		)
+
+		DescribeTable("should fail when the annotations select no layer", func(selector *cdiv1.LayerSelector) {
+			_, err := selectLayer(layers, selector)
+			Expect(err).To(MatchError(errLayerNotFound))
+		},
+			Entry("when no layer carries the annotation", matching(map[string]string{"org.example.disk.unknown": "rootdisk"})),
+			Entry("when no layer annotation has the value", named("unknowndisk")),
+			Entry("when only some of the annotations match", matching(map[string]string{nameAnnotation: "rootdisk", sizeAnnotation: "5Gi"})),
+		)
+
+		It("should refuse a selector with no annotations rather than match every layer", func() {
+			_, err := selectLayer([]types.BlobInfo{rootdisk}, matching(nil))
+			Expect(err).To(MatchError(errEmptyLayerSelector))
+		})
+
+		It("should fail when the annotations select more than one layer", func() {
+			_, err := selectLayer(layers, matching(map[string]string{sizeAnnotation: "10Gi"}))
+			Expect(err).To(MatchError(errLayerAmbiguous))
+			Expect(err).To(MatchError(ContainSubstring("match 2 layers (sha256:root, sha256:data)")))
+		})
+	})
+
+	Context("parsing the artifact manifest", func() {
+		ociManifest := func(artifactType string) []byte {
+			manBlob, err := json.Marshal(imgspecv1.Manifest{
+				Versioned:    specs.Versioned{SchemaVersion: 2},
+				MediaType:    imgspecv1.MediaTypeImageManifest,
+				ArtifactType: artifactType,
+				Config:       imgspecv1.DescriptorEmptyJSON,
+			})
+			Expect(err).ToNot(HaveOccurred())
+			return manBlob
+		}
+
+		DescribeTable("should accept the KubeVirt artifact types", func(artifactType string) {
+			man, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).ToNot(HaveOccurred())
+			Expect(man.ArtifactType).To(Equal(artifactType))
+		},
+			Entry("a virtual machine", "application/vnd.kubevirt.virtualmachine.v1"),
+			Entry("a virtual machine template", "application/vnd.kubevirt.virtualmachinetemplate.v1"),
+		)
+
+		DescribeTable("should refuse any other manifest", func(artifactType string) {
+			_, err := parseOCIManifest(ociManifest(artifactType), imgspecv1.MediaTypeImageManifest)
+			Expect(err).To(MatchError(errArtifactType))
+		},
+			Entry("a container image, declaring no artifact type", ""),
+			Entry("an artifact of another type", "application/vnd.example.disk.v1"),
+			Entry("an artifact of an unknown version", "application/vnd.kubevirt.virtualmachine.v2"),
+		)
+	})
+
+	Context("choosing the manifest of an image index", func() {
+		var (
+			amd64Entry = imgspecv1.Descriptor{Digest: digest.FromString("amd64"), Platform: &imgspecv1.Platform{OS: "linux", Architecture: "amd64"}}
+			bareEntry  = imgspecv1.Descriptor{Digest: digest.FromString("no platform")}
+		)
+
+		DescribeTable("should choose the entry", func(architecture string, entries []imgspecv1.Descriptor, expected imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			Expect(chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: architecture}, index)).To(Equal(expected.Digest))
+		},
+			Entry("declaring no platform when no architecture is requested", "", []imgspecv1.Descriptor{bareEntry}, bareEntry),
+			Entry("declaring the requested architecture over one declaring no platform", "amd64", []imgspecv1.Descriptor{bareEntry, amd64Entry}, amd64Entry),
+		)
+
+		DescribeTable("should refuse an entry declaring no platform when an architecture is requested", func(entries []imgspecv1.Descriptor) {
+			index := manifest.OCI1IndexFromComponents(entries, nil)
+			_, err := chooseArtifactInstance(&types.SystemContext{ArchitectureChoice: "arm64"}, index)
+			Expect(err).To(MatchError(errPlatformUndeclared))
+		},
+			Entry("when it is the only entry", []imgspecv1.Descriptor{bareEntry}),
+			Entry("when no other entry matches the architecture", []imgspecv1.Descriptor{amd64Entry, bareEntry}),
+		)
+	})
+
+	Context("extracting the disk image", func() {
+		rawDisk := func(content string) []byte {
+			return bytes.Repeat([]byte(content), 1024)
+		}
+
+		var destDir string
+
+		BeforeEach(func() {
+			destDir = GinkgoT().TempDir()
+		})
+
+		artifact := func() artifactDisk {
+			return artifactDisk{disk: diskWriter{destDir: destDir}, pathPrefix: "disk"}
+		}
+		diskImage := func() string {
+			return filepath.Join(destDir, "disk", "disk.img")
+		}
+
+		It("should write out the whole blob of the layer", func() {
+			layers, open := openLayers(rawDisk("root"))
+
+			Expect(extractDisk(context.Background(), artifact(), layers, open)).To(Succeed())
+			Expect(os.ReadFile(diskImage())).To(Equal(rawDisk("root")))
+		})
+
+		DescribeTable("should refuse a tar layer rather than import the archive as a raw disk", func(compress func([]byte) []byte) {
+			// random content keeps the compressed layer longer than the header the format is detected by
+			content := make([]byte, 4096)
+			Expect(rand.Read(content)).To(Equal(len(content)))
+			var archive bytes.Buffer
+			tw := tar.NewWriter(&archive)
+			Expect(tw.WriteHeader(&tar.Header{Name: "disk.img", Mode: 0600, Size: int64(len(content))})).To(Succeed())
+			Expect(tw.Write(content)).To(Equal(len(content)))
+			Expect(tw.Close()).To(Succeed())
+			layers, open := openLayers(compress(archive.Bytes()))
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(errLayerTar))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		},
+			Entry("an uncompressed tar", func(b []byte) []byte { return b }),
+			Entry("a gzipped tar", func(b []byte) []byte {
+				var gz bytes.Buffer
+				zw := gzip.NewWriter(&gz)
+				_, err := zw.Write(b)
+				Expect(err).ToNot(HaveOccurred())
+				Expect(zw.Close()).To(Succeed())
+				return gz.Bytes()
+			}),
+		)
+
+		It("should fail on the layer being unreadable rather than report it holds no disk", func() {
+			layers, open := openLayers(nil)
+
+			err := extractDisk(context.Background(), artifact(), layers, open)
+			Expect(err).To(MatchError(ContainSubstring("layer unavailable")))
+			Expect(err).ToNot(MatchError(errDiskImageNotFound))
+			Expect(diskImage()).ToNot(BeAnExistingFile())
+		})
+	})
+
+	Context("from a registry", func() {
+		// the fixtures follow the layout KubeVirt exports, which names each disk layer by this annotation
+		const diskNameAnnotation = "io.kubevirt.disk.name"
+		diskNamed := func(name string) *cdiv1.LayerSelector {
+			return matching(map[string]string{diskNameAnnotation: name})
+		}
+
+		var tmpDir string
+
+		BeforeEach(func() {
+			tmpDir = GinkgoT().TempDir()
+		})
+
+		diskImage := func() string {
+			return filepath.Join(tmpDir, containerDiskImageDir, artifactDiskImageName)
+		}
+
+		When("the artifact is an image index", func() {
+			// an OCI artifact: an image index with an amd64 manifest holding a rootdisk and a

Sounds good but yeah, probably worth doing it in a follow-up.

0xFelix 0xFelix commented on Nov 13, 2026 15:30 UTC
Reviewer

Ping on two unresolved threads, otherwise LGTM!

Acedus Acedus commented on Nov 17, 2026 09:02 UTC
Author

Ping on two unresolved threads, otherwise LGTM!

Sorry, should be good now.

Acedus Acedus commented on Nov 17, 2026 09:03 UTC
Author

/cc @akalenyu

nav1s nav1s commented on Nov 17, 2026 16:06 UTC
Reviewer

Looking good, two nits below

nav1s nav1s approved these changes on Nov 18, 2026 12:15 UTC
Approved Reviewer

Thanks @Acedus!

0xFelix 0xFelix approved these changes on Nov 20, 2026 09:01 UTC
Approved Reviewer

Thanks!

akalenyu akalenyu commented on Nov 22, 2026 20:57 UTC
Contributor

/test pull-containerized-data-importer-e2e-hpp-previous

I think the failure has to do with informer not syncing the defaultsc=false change.

akalenyu akalenyu commented on Nov 24, 2026 14:23 UTC
Reviewer

great stuff! thank you!
/approve

was thinking about the JSON passing mechanism; maybe we can eventually start placing new annotations in a config map that gets mounted by importer (not too keen on converting all existing, just want to stop accumulating new data)
something for a follow up, not necessarily by the author of this PR :stuck_out_tongue:
https://github.com/kubevirt/containerized-data-importer/issues/4369

kubevirt-prow[bot] kubevirt-prow[bot] commented on Nov 24, 2026 14:23 UTC
Bot

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: <a href="https://github.com/kubevirt/containerized-data-importer/pull/4301#pullrequestreview-6040958617" title="Approved">akalenyu</a>

The full list of commands accepted by this bot can be found here.

The pull request process is described here

<details >
Needs approval from an approver in each of these files:

  • ~~OWNERS~~ [akalenyu]
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":[]} -->

    Reviewers
    Assignees
    Acedus Acedus
    Labels
    risk: low confidence: 91.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 71.5% (HIGH) · Higher = larger blast radius
    71.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.3% · Lower = rushed or shallow review
    91.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.2% · Safe to merge
    6.2%