Skip to content

Commit

Permalink
feat: replace docker client code with regclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Dec 10, 2024
1 parent 1032698 commit 82c5607
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 89 deletions.
16 changes: 8 additions & 8 deletions 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/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
)
Expand All @@ -23,20 +23,20 @@ type BlobContainer interface {

type blobContainer struct {
accessio.StaticAllocatable
fetcher resolve.Fetcher
pusher resolve.Pusher
fetcher regclient.Fetcher
pusher regclient.Pusher
mime string
}

type BlobContainers struct {
lock sync.Mutex
cache accessio.BlobCache
fetcher resolve.Fetcher
pusher resolve.Pusher
fetcher regclient.Fetcher
pusher regclient.Pusher
mimes map[string]BlobContainer
}

func NewBlobContainers(ctx cpi.Context, fetcher remotes.Fetcher, pusher resolve.Pusher) *BlobContainers {
func NewBlobContainers(ctx cpi.Context, fetcher remotes.Fetcher, pusher regclient.Pusher) *BlobContainers {
return &BlobContainers{
cache: cacheattr.Get(ctx),
fetcher: fetcher,
Expand Down Expand Up @@ -73,15 +73,15 @@ func (c *BlobContainers) Release() error {
return list.Result()
}

func newBlobContainer(mime string, fetcher resolve.Fetcher, pusher resolve.Pusher) *blobContainer {
func newBlobContainer(mime string, fetcher regclient.Fetcher, pusher regclient.Pusher) *blobContainer {
return &blobContainer{
mime: mime,
fetcher: fetcher,
pusher: pusher,
}
}

func NewBlobContainer(cache accessio.BlobCache, mime string, fetcher resolve.Fetcher, pusher resolve.Pusher) (BlobContainer, error) {
func NewBlobContainer(cache accessio.BlobCache, mime string, fetcher regclient.Fetcher, pusher regclient.Pusher) (BlobContainer, error) {
c := newBlobContainer(mime, fetcher, pusher)

if cache == nil {
Expand Down
12 changes: 6 additions & 6 deletions 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/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
"ocm.software/ocm/api/utils/logging"
Expand All @@ -22,10 +22,10 @@ import (
type NamespaceContainer struct {
impl support.NamespaceAccessImpl
repo *RepositoryImpl
resolver resolve.Resolver
lister resolve.Lister
fetcher resolve.Fetcher
pusher resolve.Pusher
resolver regclient.Resolver
lister regclient.Lister
fetcher regclient.Fetcher
pusher regclient.Pusher
blobs *BlobContainers
checked bool
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (n *NamespaceContainer) SetImplementation(impl support.NamespaceAccessImpl)
n.impl = impl
}

func (n *NamespaceContainer) getPusher(vers string) (resolve.Pusher, error) {
func (n *NamespaceContainer) getPusher(vers string) (regclient.Pusher, error) {
err := n.assureCreated()
if err != nil {
return nil, err
Expand Down
124 changes: 66 additions & 58 deletions api/oci/extensions/repositories/ocireg/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package ocireg

import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"path"
"strings"

"github.com/containerd/containerd/remotes/docker/config"
"github.com/containerd/errdefs"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/logging"
regconfig "github.com/regclient/regclient/config"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/datacontext/attrs/rootcertsattr"
"ocm.software/ocm/api/oci/artdesc"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/tech/docker"
"ocm.software/ocm/api/tech/docker/resolve"
"ocm.software/ocm/api/tech/oci/identity"
"ocm.software/ocm/api/tech/regclient"
"ocm.software/ocm/api/utils"
ocmlog "ocm.software/ocm/api/utils/logging"
"ocm.software/ocm/api/utils/refmgmt"
Expand Down Expand Up @@ -114,7 +109,7 @@ func (r *RepositoryImpl) getCreds(comp string) (credentials.Credentials, error)
return identity.GetCredentials(r.GetContext(), r.info.Locator, comp)
}

func (r *RepositoryImpl) getResolver(comp string) (resolve.Resolver, error) {
func (r *RepositoryImpl) getResolver(comp string) (regclient.Resolver, error) {
creds, err := r.getCreds(comp)
if err != nil {
if !errors.IsErrUnknownKind(err, credentials.KIND_CONSUMER) {
Expand All @@ -126,57 +121,70 @@ func (r *RepositoryImpl) getResolver(comp string) (resolve.Resolver, error) {
logger.Trace("no credentials")
}

opts := docker.ResolverOptions{
Hosts: docker.ConvertHosts(config.ConfigureHosts(context.Background(), config.HostOptions{
UpdateClient: func(client *http.Client) error {
// copy from http.DefaultTransport with a roundtripper injection
client.Transport = ocmlog.NewRoundTripper(client.Transport, logger)
return nil
},
Credentials: func(host string) (string, string, error) {
if creds != nil {
p := creds.GetProperty(credentials.ATTR_IDENTITY_TOKEN)
if p == "" {
p = creds.GetProperty(credentials.ATTR_PASSWORD)
}
pw := ""
if p != "" {
pw = "***"
}
logger.Trace("query credentials", ocmlog.ATTR_USER, creds.GetProperty(credentials.ATTR_USERNAME), "pass", pw)
return creds.GetProperty(credentials.ATTR_USERNAME), p, nil
}
logger.Trace("no credentials")
return "", "", nil
},
DefaultScheme: r.info.Scheme,
//nolint:gosec // used like the default, there are OCI servers (quay.io) not working with min version.
DefaultTLS: func() *tls.Config {
if r.info.Scheme == "http" {
return nil
}
return &tls.Config{
// MinVersion: tls.VersionTLS13,
RootCAs: func() *x509.CertPool {
var rootCAs *x509.CertPool
if creds != nil {
c := creds.GetProperty(credentials.ATTR_CERTIFICATE_AUTHORITY)
if c != "" {
rootCAs = x509.NewCertPool()
rootCAs.AppendCertsFromPEM([]byte(c))
}
}
if rootCAs == nil {
rootCAs = rootcertsattr.Get(r.GetContext()).GetRootCertPool(true)
}
return rootCAs
}(),
}
}(),
})),
pass := creds.GetProperty(credentials.ATTR_IDENTITY_TOKEN)
if pass == "" {
pass = creds.GetProperty(credentials.ATTR_PASSWORD)
}

return docker.NewResolver(opts), nil
username := creds.GetProperty(credentials.ATTR_USERNAME)
opts := regclient.ClientOptions{
Host: &regconfig.Host{
Name: "ghcr.io",
User: username,
Pass: pass,
},
Version: comp,
}
//opts := docker.ResolverOptions{

Check failure on line 137 in api/oci/extensions/repositories/ocireg/repository.go

View workflow job for this annotation

GitHub Actions / Lint Golang

commentFormatting: put a space between `//` and comment text (gocritic)
// Hosts: docker.ConvertHosts(config.ConfigureHosts(context.Background(), config.HostOptions{
// UpdateClient: func(client *http.Client) error {
// // copy from http.DefaultTransport with a roundtripper injection
// client.Transport = ocmlog.NewRoundTripper(client.Transport, logger)
// return nil
// },
// Credentials: func(host string) (string, string, error) {
// if creds != nil {
// p := creds.GetProperty(credentials.ATTR_IDENTITY_TOKEN)
// if p == "" {
// p = creds.GetProperty(credentials.ATTR_PASSWORD)
// }
// pw := ""
// if p != "" {
// pw = "***"
// }
// logger.Trace("query credentials", ocmlog.ATTR_USER, creds.GetProperty(credentials.ATTR_USERNAME), "pass", pw)
// return creds.GetProperty(credentials.ATTR_USERNAME), p, nil
// }
// logger.Trace("no credentials")
// return "", "", nil
// },
// DefaultScheme: r.info.Scheme,
// //nolint:gosec // used like the default, there are OCI servers (quay.io) not working with min version.
// DefaultTLS: func() *tls.Config {
// if r.info.Scheme == "http" {
// return nil
// }
// return &tls.Config{
// // MinVersion: tls.VersionTLS13,
// RootCAs: func() *x509.CertPool {
// var rootCAs *x509.CertPool
// if creds != nil {
// c := creds.GetProperty(credentials.ATTR_CERTIFICATE_AUTHORITY)
// if c != "" {
// rootCAs = x509.NewCertPool()
// rootCAs.AppendCertsFromPEM([]byte(c))
// }
// }
// if rootCAs == nil {
// rootCAs = rootcertsattr.Get(r.GetContext()).GetRootCertPool(true)
// }
// return rootCAs
// }(),
// }
// }(),
// })),
//}

return regclient.New(opts), nil
}

func (r *RepositoryImpl) GetRef(comp, vers string) string {
Expand Down
8 changes: 5 additions & 3 deletions 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/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
"ocm.software/ocm/api/utils/logging"
Expand Down Expand Up @@ -81,12 +81,12 @@ func readAll(reader io.ReadCloser, err error) ([]byte, error) {
return data, nil
}

func push(ctx context.Context, p resolve.Pusher, blob blobaccess.BlobAccess) error {
func push(ctx context.Context, p regclient.Pusher, blob blobaccess.BlobAccess) error {
desc := *artdesc.DefaultBlobDescriptor(blob)
return pushData(ctx, p, desc, blob)
}

func pushData(ctx context.Context, p resolve.Pusher, desc artdesc.Descriptor, data blobaccess.DataAccess) error {
func pushData(ctx context.Context, p regclient.Pusher, desc artdesc.Descriptor, data blobaccess.DataAccess) error {
key := remotes.MakeRefKey(ctx, desc)
if desc.Size == 0 {
desc.Size = -1
Expand All @@ -100,8 +100,10 @@ func pushData(ctx context.Context, p resolve.Pusher, desc artdesc.Descriptor, da

return nil
}

return fmt.Errorf("failed to push: %w", err)
}

return req.Commit(ctx, desc.Size, desc.Digest)
}

Expand Down
5 changes: 2 additions & 3 deletions api/tech/docker/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"

"ocm.software/ocm/api/tech/docker/resolve"
"ocm.software/ocm/api/tech/regclient"
)

var ErrObjectNotRequired = errors.New("object not required")
Expand All @@ -24,7 +23,7 @@ type dockerLister struct {
dockerBase *dockerBase
}

func (r *dockerResolver) Lister(ctx context.Context, ref string) (resolve.Lister, error) {
func (r *dockerResolver) Lister(ctx context.Context, ref string) (regclient.Lister, error) {
base, err := r.resolveDockerBase(ref)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions api/tech/docker/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"ocm.software/ocm/api/tech/regclient"

remoteserrors "ocm.software/ocm/api/tech/docker/errors"
"ocm.software/ocm/api/tech/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
)

Expand All @@ -37,11 +37,11 @@ type dockerPusher struct {
tracker StatusTracker
}

func (p dockerPusher) Push(ctx context.Context, desc ocispec.Descriptor, src resolve.Source) (resolve.PushRequest, error) {
func (p dockerPusher) Push(ctx context.Context, desc ocispec.Descriptor, src regclient.Source) (regclient.PushRequest, error) {
return p.push(ctx, desc, src, remotes.MakeRefKey(ctx, desc), false)
}

func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, src resolve.Source, ref string, unavailableOnFail bool) (resolve.PushRequest, error) {
func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, src regclient.Source, ref string, unavailableOnFail bool) (regclient.PushRequest, error) {
if l, ok := p.tracker.(StatusTrackLocker); ok {
l.Lock(ref)
defer l.Unlock(ref)
Expand Down Expand Up @@ -322,7 +322,7 @@ type pushRequest struct {
ref string

responseC <-chan response
source resolve.Source
source regclient.Source
isManifest bool

expected digest.Digest
Expand Down
10 changes: 5 additions & 5 deletions api/tech/docker/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context/ctxhttp"
"ocm.software/ocm/api/tech/regclient"

"ocm.software/ocm/api/tech/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
)

Expand Down Expand Up @@ -118,7 +118,7 @@ type dockerResolver struct {
}

// NewResolver returns a new resolver to a Docker registry.
func NewResolver(options ResolverOptions) resolve.Resolver {
func NewResolver(options ResolverOptions) regclient.Resolver {
if options.Tracker == nil {
options.Tracker = NewInMemoryTracker()
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (r *countingReader) Read(p []byte) (int, error) {
return n, err
}

var _ resolve.Resolver = &dockerResolver{}
var _ regclient.Resolver = &dockerResolver{}

func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocispec.Descriptor, error) {
base, err := r.resolveDockerBase(ref)
Expand Down Expand Up @@ -382,7 +382,7 @@ func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocisp
return "", ocispec.Descriptor{}, firstErr
}

func (r *dockerResolver) Fetcher(ctx context.Context, ref string) (resolve.Fetcher, error) {
func (r *dockerResolver) Fetcher(ctx context.Context, ref string) (regclient.Fetcher, error) {
base, err := r.resolveDockerBase(ref)
if err != nil {
return nil, err
Expand All @@ -393,7 +393,7 @@ func (r *dockerResolver) Fetcher(ctx context.Context, ref string) (resolve.Fetch
}, nil
}

func (r *dockerResolver) Pusher(ctx context.Context, ref string) (resolve.Pusher, error) {
func (r *dockerResolver) Pusher(ctx context.Context, ref string) (regclient.Pusher, error) {
base, err := r.resolveDockerBase(ref)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions api/tech/oras/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package oras
Loading

0 comments on commit 82c5607

Please sign in to comment.