Skip to content

Commit 15396e6

Browse files
loresusopoiana
authored andcommitted
update(pkg/oci): remove the need for artifact type in pull
also, allow to specify no tag when pulling. the default one will be used in this case (latest), exactly the same as in the push command. Signed-off-by: Lorenzo Susini <[email protected]>
1 parent 3285362 commit 15396e6

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

pkg/oci/puller/puller.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package puller
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"fmt"
21+
"io"
2022

2123
v1 "github.com/opencontainers/image-spec/specs-go/v1"
2224
"oras.land/oras-go/v2"
@@ -47,7 +49,7 @@ func NewPuller(client *auth.Client, tracker ProgressTracker) *Puller {
4749

4850
// Pull an artifact from a remote registry.
4951
// Ref format follows: REGISTRY/REPO[:TAG|@DIGEST]. Ex. localhost:5000/hello:latest.
50-
func (p *Puller) Pull(ctx context.Context, artifactType oci.ArtifactType, ref, destDir, os, arch string) (*oci.RegistryResult, error) {
52+
func (p *Puller) Pull(ctx context.Context, ref, destDir, os, arch string) (*oci.RegistryResult, error) {
5153
fileStore := file.New(destDir)
5254

5355
repo, err := remote.NewRepository(ref)
@@ -56,14 +58,20 @@ func (p *Puller) Pull(ctx context.Context, artifactType oci.ArtifactType, ref, d
5658
}
5759
repo.Client = p.Client
5860

61+
// if no tag was specified, "latest" is used
62+
if repo.Reference.Reference == "" {
63+
ref += ":" + oci.DefaultTag
64+
repo.Reference.Reference = oci.DefaultTag
65+
}
66+
5967
refDesc, _, err := repo.FetchReference(ctx, ref)
6068
if err != nil {
6169
return nil, err
6270
}
6371

6472
copyOpts := oras.CopyOptions{}
6573
copyOpts.Concurrency = 1
66-
if artifactType == oci.Plugin && refDesc.MediaType == v1.MediaTypeImageIndex {
74+
if refDesc.MediaType == v1.MediaTypeImageIndex {
6775
plt := &v1.Platform{
6876
OS: os,
6977
Architecture: arch,
@@ -83,7 +91,37 @@ func (p *Puller) Pull(ctx context.Context, artifactType oci.ArtifactType, ref, d
8391
repo.Reference.Repository, repo.Reference.Reference, repo.Reference.Repository, err)
8492
}
8593

94+
descReader, err := repo.Fetch(ctx, desc)
95+
if err != nil {
96+
return nil, fmt.Errorf("unable to fetch descriptor with digest %q: %w", desc.Digest, err)
97+
}
98+
99+
var manifest v1.Manifest
100+
descBytes, err := io.ReadAll(descReader)
101+
if err != nil {
102+
return nil, fmt.Errorf("unable to read bytes from descriptor: %w", err)
103+
}
104+
105+
if err = json.Unmarshal(descBytes, &manifest); err != nil {
106+
return nil, fmt.Errorf("unable to unmarshal manifest: %w", err)
107+
}
108+
109+
if len(manifest.Layers) < 1 {
110+
return nil, fmt.Errorf("no layers in manifest")
111+
}
112+
113+
var artifactType oci.ArtifactType
114+
switch manifest.Layers[0].MediaType {
115+
case oci.FalcoPluginLayerMediaType:
116+
artifactType = oci.Plugin
117+
case oci.FalcoRulesfileLayerMediaType:
118+
artifactType = oci.Rulesfile
119+
default:
120+
return nil, fmt.Errorf("unknown media type: %q", manifest.Layers[0].MediaType)
121+
}
122+
86123
return &oci.RegistryResult{
87124
Digest: string(desc.Digest),
125+
Type: artifactType,
88126
}, nil
89127
}

0 commit comments

Comments
 (0)