Skip to content

Commit

Permalink
fix the nil and the unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Dec 10, 2024
1 parent 82c5607 commit 7dd6512
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion api/oci/extensions/repositories/ocireg/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/mandelsoft/goutils/errors"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"ocm.software/ocm/api/tech/regclient"

"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/extensions/attrs/cacheattr"
"ocm.software/ocm/api/tech/regclient"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
)
Expand Down
2 changes: 1 addition & 1 deletion api/oci/extensions/repositories/ocireg/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/containerd/errdefs"
"github.com/mandelsoft/goutils/errors"
"github.com/opencontainers/go-digest"
"ocm.software/ocm/api/tech/regclient"

"ocm.software/ocm/api/oci/artdesc"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/cpi/support"
"ocm.software/ocm/api/oci/extensions/actions/oci-repository-prepare"
"ocm.software/ocm/api/tech/regclient"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
"ocm.software/ocm/api/utils/logging"
Expand Down
20 changes: 14 additions & 6 deletions api/oci/extensions/repositories/ocireg/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,24 @@ func (r *RepositoryImpl) getResolver(comp string) (regclient.Resolver, error) {
logger.Trace("no credentials")
}

pass := creds.GetProperty(credentials.ATTR_IDENTITY_TOKEN)
if pass == "" {
pass = creds.GetProperty(credentials.ATTR_PASSWORD)
var (
password string
username string
)

if creds != nil {
password = creds.GetProperty(credentials.ATTR_IDENTITY_TOKEN)
if password == "" {
password = creds.GetProperty(credentials.ATTR_PASSWORD)
}
username = creds.GetProperty(credentials.ATTR_USERNAME)
}
username := creds.GetProperty(credentials.ATTR_USERNAME)

opts := regclient.ClientOptions{
Host: &regconfig.Host{
Name: "ghcr.io",
Name: "ghcr.io", //TODO: Need to figure out how to set the host.
User: username,
Pass: pass,
Pass: password,
},
Version: comp,
}
Expand Down
2 changes: 1 addition & 1 deletion api/oci/extensions/repositories/ocireg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/containerd/log"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"ocm.software/ocm/api/tech/regclient"

"ocm.software/ocm/api/oci/artdesc"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/tech/regclient"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
"ocm.software/ocm/api/utils/logging"
Expand Down
25 changes: 15 additions & 10 deletions api/tech/regclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ func (p *pushRequest) Status() (content.Status, error) {

var _ PushRequest = &pushRequest{}

var _ Resolver = &Client{}
var _ Fetcher = &Client{}
var _ Pusher = &Client{}
var _ Lister = &Client{}
var (
_ Resolver = &Client{}
_ Fetcher = &Client{}
_ Pusher = &Client{}
_ Lister = &Client{}
)

func New(opts ClientOptions) *Client {
rc := regclient.New(
Expand All @@ -65,7 +67,7 @@ func New(opts ClientOptions) *Client {
regclient.WithDockerCreds(),
regclient.WithUserAgent("containerd/"+opts.Version),
regclient.WithRegOpts(
//reg.WithCertDirs([]string{"."}),
// reg.WithCertDirs([]string{"."}),
reg.WithDelay(2*time.Second, 15*time.Second),
reg.WithRetryLimit(5),
reg.WithCache(5*time.Minute, 500), // built in cache!! Nice!
Expand Down Expand Up @@ -129,7 +131,7 @@ func (c *Client) convertDescriptorToRegClient(desc ociv1.Descriptor) descriptor.
}

func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descriptor, error) {
//TODO: figure out what to do about closing c.rc.
// TODO: figure out what to do about closing c.rc.
r, err := regref.New(ref)
if err != nil {
return "", ociv1.Descriptor{}, err
Expand All @@ -141,16 +143,15 @@ func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descrip
if strings.Contains(err.Error(), "not found") {
// fallback to finding a blob if we have a digest
if r.Digest != "" {
blob, err := c.rc.BlobHead(ctx, r, descriptor.Descriptor{
blob, err := c.rc.BlobGet(ctx, r, descriptor.Descriptor{
Digest: digest.Digest(r.Digest),
Size: -1,
})
if err != nil {
if strings.Contains(err.Error(), "not found") {
return "", ociv1.Descriptor{}, errdefs.ErrNotFound
}

return "", ociv1.Descriptor{}, err
return "", ociv1.Descriptor{}, fmt.Errorf("failed to resolve blob head: %w", err)
}

return ref, c.convertDescriptorToOCI(blob.GetDescriptor()), nil
Expand Down Expand Up @@ -249,9 +250,13 @@ func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (_ io.ReadClo
}()

// set up closing the client after fetching is done.
// -1 is not a thing in regclient.
if desc.Size < 0 {
desc.Size = 0
}
reader, err := c.rc.BlobGet(ctx, c.ref, c.convertDescriptorToRegClient(desc))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get the blob reader: %w", err)
}

return reader, nil
Expand Down

0 comments on commit 7dd6512

Please sign in to comment.