2018-09-11 00:08:07 +00:00
|
|
|
package contentutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2018-10-06 00:20:06 +00:00
|
|
|
"sync"
|
2018-09-11 00:08:07 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2018-10-06 00:20:06 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2018-09-11 00:08:07 +00:00
|
|
|
"github.com/containerd/containerd/remotes"
|
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
2022-02-02 06:48:01 +00:00
|
|
|
"github.com/moby/buildkit/version"
|
2020-10-30 08:25:05 +00:00
|
|
|
"github.com/moby/locker"
|
2018-10-06 00:20:06 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-10-06 00:20:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-09-11 00:08:07 +00:00
|
|
|
)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
|
2022-02-02 06:48:01 +00:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("User-Agent", version.UserAgent())
|
2018-09-11 00:08:07 +00:00
|
|
|
remote := docker.NewResolver(docker.ResolverOptions{
|
2022-02-02 06:48:01 +00:00
|
|
|
Client: http.DefaultClient,
|
|
|
|
Headers: headers,
|
2018-09-11 00:08:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
name, desc, err := remote.Resolve(context.TODO(), ref)
|
|
|
|
if err != nil {
|
2021-07-26 08:53:30 +00:00
|
|
|
return ocispecs.Descriptor{}, nil, err
|
2018-09-11 00:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetcher, err := remote.Fetcher(context.TODO(), name)
|
|
|
|
if err != nil {
|
2021-07-26 08:53:30 +00:00
|
|
|
return ocispecs.Descriptor{}, nil, err
|
2018-09-11 00:08:07 +00:00
|
|
|
}
|
|
|
|
return desc, FromFetcher(fetcher), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IngesterFromRef(ref string) (content.Ingester, error) {
|
2022-02-02 06:48:01 +00:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("User-Agent", version.UserAgent())
|
2018-09-11 00:08:07 +00:00
|
|
|
remote := docker.NewResolver(docker.ResolverOptions{
|
2022-02-02 06:48:01 +00:00
|
|
|
Client: http.DefaultClient,
|
|
|
|
Headers: headers,
|
2018-09-11 00:08:07 +00:00
|
|
|
})
|
|
|
|
|
2021-11-13 01:32:09 +00:00
|
|
|
p, err := remote.Pusher(context.TODO(), ref)
|
2018-09-11 00:08:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ingester{
|
2018-10-06 00:20:06 +00:00
|
|
|
locker: locker.New(),
|
2021-11-13 01:32:09 +00:00
|
|
|
pusher: &pusher{p},
|
2018-09-11 00:08:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-13 01:32:09 +00:00
|
|
|
type pusher struct {
|
|
|
|
remotes.Pusher
|
|
|
|
}
|
|
|
|
|
2018-09-11 00:08:07 +00:00
|
|
|
type ingester struct {
|
2018-10-06 00:20:06 +00:00
|
|
|
locker *locker.Locker
|
2018-09-11 00:08:07 +00:00
|
|
|
pusher remotes.Pusher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ingester) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
|
|
|
|
var wo content.WriterOpts
|
|
|
|
for _, o := range opts {
|
|
|
|
if err := o(&wo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 00:20:06 +00:00
|
|
|
if wo.Ref == "" {
|
|
|
|
return nil, errors.Wrap(errdefs.ErrInvalidArgument, "ref must not be empty")
|
|
|
|
}
|
|
|
|
w.locker.Lock(wo.Ref)
|
|
|
|
var once sync.Once
|
|
|
|
unlock := func() {
|
|
|
|
once.Do(func() {
|
|
|
|
w.locker.Unlock(wo.Ref)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
writer, err := w.pusher.Push(ctx, wo.Desc)
|
|
|
|
if err != nil {
|
2018-10-15 22:34:28 +00:00
|
|
|
unlock()
|
2018-10-06 00:20:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lockedWriter{unlock: unlock, Writer: writer}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type lockedWriter struct {
|
|
|
|
unlock func()
|
|
|
|
content.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *lockedWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
|
|
|
|
err := w.Writer.Commit(ctx, size, expected, opts...)
|
|
|
|
if err == nil {
|
|
|
|
w.unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *lockedWriter) Close() error {
|
|
|
|
err := w.Writer.Close()
|
|
|
|
w.unlock()
|
|
|
|
return err
|
2018-09-11 00:08:07 +00:00
|
|
|
}
|