Skip to content

Commit 053d33d

Browse files
Add layer offset (WIP)
Signed-off-by: Ilya Dmitrichenko <[email protected]>
1 parent eff5a07 commit 053d33d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

api/v1beta2/ocirepository_types.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ type OCIRepositoryRef struct {
166166
type OCILayerSelector struct {
167167
// MediaType specifies the OCI media type of the layer
168168
// which should be extracted from the OCI Artifact. The
169-
// first layer matching this type is selected.
169+
// first layer matching this type is selected by default,
170+
// otherwise 'offest' needs to be specified.
170171
// +optional
171172
MediaType string `json:"mediaType,omitempty"`
172173

@@ -177,6 +178,11 @@ type OCILayerSelector struct {
177178
// +kubebuilder:validation:Enum=extract;copy
178179
// +optional
179180
Operation string `json:"operation,omitempty"`
181+
182+
// Offset specifies the index of the layer which should be extracted.
183+
// +kubebuilder:validation:Minimum=0
184+
// +optional
185+
Offset *int `json:"offset,omitempty"`
180186
}
181187

182188
// OCIRepositoryVerification verifies the authenticity of an OCI Artifact
@@ -307,6 +313,14 @@ func (in *OCIRepository) GetLayerOperation() string {
307313
return in.Spec.LayerSelector.Operation
308314
}
309315

316+
func (in *OCIRepository) GetLayerOffset() int {
317+
if in.Spec.LayerSelector == nil || in.Spec.LayerSelector.Offset == nil {
318+
return 0
319+
}
320+
321+
return *in.Spec.LayerSelector.Offset
322+
}
323+
310324
// +genclient
311325
// +genclient:Namespaced
312326
// +kubebuilder:storageversion

internal/controller/ocirepository_controller.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,30 +539,40 @@ func (r *OCIRepositoryReconciler) selectLayer(obj *ociv1.OCIRepository, image gc
539539
return nil, fmt.Errorf("failed to parse artifact layers: %w", err)
540540
}
541541

542-
if len(layers) < 1 {
542+
offset := obj.GetLayerOffset()
543+
mediaType := obj.GetLayerMediaType()
544+
545+
if len(layers) == 0 {
543546
return nil, fmt.Errorf("no layers found in artifact")
544547
}
545548

549+
if len(layers) <= offset {
550+
return nil, fmt.Errorf("layer offset %d is out of bounds", offset)
551+
}
552+
546553
var layer gcrv1.Layer
547554
switch {
548-
case obj.GetLayerMediaType() != "":
555+
case mediaType != "":
549556
var found bool
550557
for i, l := range layers {
558+
if i < offset {
559+
continue
560+
}
551561
md, err := l.MediaType()
552562
if err != nil {
553563
return nil, fmt.Errorf("failed to determine the media type of layer[%v] from artifact: %w", i, err)
554564
}
555-
if string(md) == obj.GetLayerMediaType() {
565+
if string(md) == mediaType {
556566
layer = layers[i]
557567
found = true
558568
break
559569
}
560570
}
561571
if !found {
562-
return nil, fmt.Errorf("failed to find layer with media type '%s' in artifact", obj.GetLayerMediaType())
572+
return nil, fmt.Errorf("failed to find layer with media type '%s' in artifact at offset %d", obj.GetLayerMediaType(), offset)
563573
}
564574
default:
565-
layer = layers[0]
575+
layer = layers[offset]
566576
}
567577

568578
blob, err := layer.Compressed()

0 commit comments

Comments
 (0)