Skip to content

Commit

Permalink
added a Oci function to support generic image
Browse files Browse the repository at this point in the history
Signed-off-by: 7h3-3mp7y-m4n <[email protected]>
  • Loading branch information
7h3-3mp7y-m4n committed Oct 13, 2024
1 parent 3693bf2 commit 7b48327
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sync v0.6.0 // indirect
oras.land/oras-go/v2 v2.5.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
oras.land/oras-go/v2 v2.5.0 h1:o8Me9kLY74Vp5uw07QXPiitjsw7qNXi8Twd+19Zf02c=
oras.land/oras-go/v2 v2.5.0/go.mod h1:z4eisnLP530vwIOUOJeBIj0aGI0L1C3d53atvCBqZHg=
34 changes: 34 additions & 0 deletions uriget/uriget.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"path/filepath"
"strings"
"time"

"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/oci"
"oras.land/oras-go/v2/registry/remote"
)

// options is a struct holding fields that may need to have overrides in certain environments or during unit testing.
Expand Down Expand Up @@ -110,6 +114,8 @@ func GetFile(ctx context.Context, rawUri string, optionFuncs ...Option) ([]byte,
fallthrough
case "git-https":
return opts.getGit(ctx, u)
case "oci":
return opts.getOci(ctx, u)
default:
return nil, fmt.Errorf("unsupported scheme '%s'", u.Scheme)
}
Expand Down Expand Up @@ -233,3 +239,31 @@ func (o *options) getGit(ctx context.Context, u *url.URL) ([]byte, error) {
o.logger.Printf("Read %d bytes from %s", len(buff), filepath.Join(td, subPath))
return buff, nil
}

func (o *options) getOci(ctx context.Context, u *url.URL) ([]byte, error) {
parts := strings.Split(u.Host+u.Path, "/")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid OCI URL format")
}
registry := parts[0]
repo := strings.Join(parts[1:], "/")
tag := "latest"
if u.Fragment != "" {
tag = u.Fragment
}
store, err := oci.New(o.tempDir)
if err != nil {
return nil, fmt.Errorf("failed to create OCI layout store: %w", err)
}
repoUrl := fmt.Sprintf("%s/%s", registry, repo)
remoteRepo, err := remote.NewRepository(repoUrl)
if err != nil {
return nil, fmt.Errorf("failed to connect to remote repository: %w", err)
}
manifestDescriptor, err := oras.Copy(ctx, remoteRepo, tag, store, tag, oras.DefaultCopyOptions)
if err != nil {
return nil, fmt.Errorf("failed to pull OCI image: %w", err)
}
o.logger.Printf("Pulled OCI image: %s with manifest descriptor : %v", u.String(), manifestDescriptor.Digest)
return []byte(manifestDescriptor.Digest), nil
}

0 comments on commit 7b48327

Please sign in to comment.