Skip to content

Commit

Permalink
resolver: add docs to new functions
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Aug 12, 2020
1 parent 6926260 commit 4994d45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
10 changes: 10 additions & 0 deletions util/resolver/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// DefaultPool is the default shared resolver pool instance
var DefaultPool = NewPool()

// Pool is a cache of recently used resolvers
type Pool struct {
mu sync.Mutex
m map[string]*authHandlerNS
}

// NewPool creates a new pool for caching resolvers
func NewPool() *Pool {
p := &Pool{
m: map[string]*authHandlerNS{},
Expand Down Expand Up @@ -61,12 +64,14 @@ func (p *Pool) gc() {
time.AfterFunc(5*time.Minute, p.gc)
}

// Clear deletes currently cached items. This may be called on config changes for example.
func (p *Pool) Clear() {
p.mu.Lock()
defer p.mu.Unlock()
p.m = map[string]*authHandlerNS{}
}

// GetResolver gets a resolver for a specified scope from the pool
func (p *Pool) GetResolver(hosts docker.RegistryHosts, ref, scope string, sm *session.Manager, g session.Group) *Resolver {
name := ref
named, err := distreference.ParseNormalizedNamed(ref)
Expand Down Expand Up @@ -105,6 +110,7 @@ func newResolver(hosts docker.RegistryHosts, handler *authHandlerNS, sm *session
return r
}

// Resolver is a wrapper around remotes.Resolver
type Resolver struct {
remotes.Resolver
hosts docker.RegistryHosts
Expand Down Expand Up @@ -145,13 +151,15 @@ func (r *Resolver) hostsFunc(host string) ([]docker.RegistryHost, error) {
}(host)
}

// WithSession returns a new resolver that works with new session group
func (r *Resolver) WithSession(s session.Group) *Resolver {
r2 := *r
r2.auth = nil
r2.g = s
return &r2
}

// WithImageStore returns new resolver that can also resolve from local images store
func (r *Resolver) WithImageStore(is images.Store, mode source.ResolveMode) *Resolver {
r2 := *r
r2.Resolver = r.Resolver
Expand All @@ -160,13 +168,15 @@ func (r *Resolver) WithImageStore(is images.Store, mode source.ResolveMode) *Res
return &r2
}

// Fetcher returns a new fetcher for the provided reference.
func (r *Resolver) Fetcher(ctx context.Context, ref string) (remotes.Fetcher, error) {
if atomic.LoadInt64(&r.handler.counter) == 0 {
r.Resolve(ctx, ref)
}
return r.Resolver.Fetcher(ctx, ref)
}

// Resolve attempts to resolve the reference into a name and descriptor.
func (r *Resolver) Resolve(ctx context.Context, ref string) (string, ocispec.Descriptor, error) {
if r.mode == source.ResolveModePreferLocal && r.is != nil {
if img, err := r.is.Get(ctx, ref); err == nil {
Expand Down
17 changes: 1 addition & 16 deletions util/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"time"

"github.com/containerd/containerd/remotes/docker"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/tracing"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -117,6 +115,7 @@ func loadTLSConfig(c config.RegistryConfig) (*tls.Config, error) {
return tc, nil
}

// NewRegistryConfig converts registry config to docker.RegistryHosts callback
func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts {
return docker.Registries(
func(host string) ([]docker.RegistryHost, error) {
Expand Down Expand Up @@ -171,20 +170,6 @@ func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts
)
}

type SessionAuthenticator struct {
sm *session.Manager
groups []session.Group
mu sync.RWMutex
cache map[string]credentials
cacheMu sync.RWMutex
}

type credentials struct {
user string
secret string
created time.Time
}

func newDefaultClient() *http.Client {
return &http.Client{
Transport: tracing.NewTransport(newDefaultTransport()),
Expand Down

0 comments on commit 4994d45

Please sign in to comment.