resolver: set buildkit own user-agent
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>master
parent
2633c96bac
commit
dd992414a3
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/moby/buildkit/client/llb"
|
"github.com/moby/buildkit/client/llb"
|
||||||
"github.com/moby/buildkit/util/contentutil"
|
"github.com/moby/buildkit/util/contentutil"
|
||||||
"github.com/moby/buildkit/util/imageutil"
|
"github.com/moby/buildkit/util/imageutil"
|
||||||
|
"github.com/moby/buildkit/version"
|
||||||
"github.com/moby/locker"
|
"github.com/moby/locker"
|
||||||
digest "github.com/opencontainers/go-digest"
|
digest "github.com/opencontainers/go-digest"
|
||||||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
@ -40,9 +41,12 @@ func New(with ...ImageMetaResolverOpt) llb.ImageMetaResolver {
|
||||||
for _, f := range with {
|
for _, f := range with {
|
||||||
f(&opts)
|
f(&opts)
|
||||||
}
|
}
|
||||||
|
headers := http.Header{}
|
||||||
|
headers.Set("User-Agent", version.UserAgent())
|
||||||
return &imageMetaResolver{
|
return &imageMetaResolver{
|
||||||
resolver: docker.NewResolver(docker.ResolverOptions{
|
resolver: docker.NewResolver(docker.ResolverOptions{
|
||||||
Client: http.DefaultClient,
|
Client: http.DefaultClient,
|
||||||
|
Headers: headers,
|
||||||
}),
|
}),
|
||||||
platform: opts.platform,
|
platform: opts.platform,
|
||||||
buffer: contentutil.NewBuffer(),
|
buffer: contentutil.NewBuffer(),
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/remotes"
|
"github.com/containerd/containerd/remotes"
|
||||||
"github.com/containerd/containerd/remotes/docker"
|
"github.com/containerd/containerd/remotes/docker"
|
||||||
|
"github.com/moby/buildkit/version"
|
||||||
"github.com/moby/locker"
|
"github.com/moby/locker"
|
||||||
digest "github.com/opencontainers/go-digest"
|
digest "github.com/opencontainers/go-digest"
|
||||||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
@ -16,8 +17,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
|
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
|
||||||
|
headers := http.Header{}
|
||||||
|
headers.Set("User-Agent", version.UserAgent())
|
||||||
remote := docker.NewResolver(docker.ResolverOptions{
|
remote := docker.NewResolver(docker.ResolverOptions{
|
||||||
Client: http.DefaultClient,
|
Client: http.DefaultClient,
|
||||||
|
Headers: headers,
|
||||||
})
|
})
|
||||||
|
|
||||||
name, desc, err := remote.Resolve(context.TODO(), ref)
|
name, desc, err := remote.Resolve(context.TODO(), ref)
|
||||||
|
@ -33,8 +37,11 @@ func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IngesterFromRef(ref string) (content.Ingester, error) {
|
func IngesterFromRef(ref string) (content.Ingester, error) {
|
||||||
|
headers := http.Header{}
|
||||||
|
headers.Set("User-Agent", version.UserAgent())
|
||||||
remote := docker.NewResolver(docker.ResolverOptions{
|
remote := docker.NewResolver(docker.ResolverOptions{
|
||||||
Client: http.DefaultClient,
|
Client: http.DefaultClient,
|
||||||
|
Headers: headers,
|
||||||
})
|
})
|
||||||
|
|
||||||
p, err := remote.Pusher(context.TODO(), ref)
|
p, err := remote.Pusher(context.TODO(), ref)
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
sessionauth "github.com/moby/buildkit/session/auth"
|
sessionauth "github.com/moby/buildkit/session/auth"
|
||||||
log "github.com/moby/buildkit/util/bklog"
|
log "github.com/moby/buildkit/util/bklog"
|
||||||
"github.com/moby/buildkit/util/flightcontrol"
|
"github.com/moby/buildkit/util/flightcontrol"
|
||||||
|
"github.com/moby/buildkit/version"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -355,6 +356,9 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdr := http.Header{}
|
||||||
|
hdr.Set("User-Agent", version.UserAgent())
|
||||||
|
|
||||||
// fetch token for the resource scope
|
// fetch token for the resource scope
|
||||||
if to.Secret != "" {
|
if to.Secret != "" {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -370,7 +374,7 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
|
||||||
// As of September 2017, GCR is known to return 404.
|
// As of September 2017, GCR is known to return 404.
|
||||||
// As of February 2018, JFrog Artifactory is known to return 401.
|
// As of February 2018, JFrog Artifactory is known to return 401.
|
||||||
if (errStatus.StatusCode == 405 && to.Username != "") || errStatus.StatusCode == 404 || errStatus.StatusCode == 401 {
|
if (errStatus.StatusCode == 405 && to.Username != "") || errStatus.StatusCode == 404 || errStatus.StatusCode == 401 {
|
||||||
resp, err := auth.FetchTokenWithOAuth(ctx, ah.client, nil, "buildkit-client", to)
|
resp, err := auth.FetchTokenWithOAuth(ctx, ah.client, hdr, "buildkit-client", to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -390,7 +394,7 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
// do request anonymously
|
// do request anonymously
|
||||||
resp, err := auth.FetchToken(ctx, ah.client, nil, to)
|
resp, err := auth.FetchToken(ctx, ah.client, hdr, to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to fetch anonymous token")
|
return nil, errors.Wrap(err, "failed to fetch anonymous token")
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package resolver
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -14,6 +15,7 @@ import (
|
||||||
distreference "github.com/docker/distribution/reference"
|
distreference "github.com/docker/distribution/reference"
|
||||||
"github.com/moby/buildkit/session"
|
"github.com/moby/buildkit/session"
|
||||||
"github.com/moby/buildkit/source"
|
"github.com/moby/buildkit/source"
|
||||||
|
"github.com/moby/buildkit/version"
|
||||||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -104,8 +106,11 @@ func newResolver(hosts docker.RegistryHosts, handler *authHandlerNS, sm *session
|
||||||
g: g,
|
g: g,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
}
|
}
|
||||||
|
headers := http.Header{}
|
||||||
|
headers.Set("User-Agent", version.UserAgent())
|
||||||
r.Resolver = docker.NewResolver(docker.ResolverOptions{
|
r.Resolver = docker.NewResolver(docker.ResolverOptions{
|
||||||
Hosts: r.HostsFunc,
|
Hosts: r.HostsFunc,
|
||||||
|
Headers: headers,
|
||||||
})
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,14 +17,46 @@
|
||||||
|
|
||||||
package version
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultVersion = "0.0.0+unknown"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Package is filled at linking time
|
// Package is filled at linking time
|
||||||
Package = "github.com/moby/buildkit"
|
Package = "github.com/moby/buildkit"
|
||||||
|
|
||||||
// Version holds the complete version number. Filled in at linking time.
|
// Version holds the complete version number. Filled in at linking time.
|
||||||
Version = "0.0.0+unknown"
|
Version = defaultVersion
|
||||||
|
|
||||||
// Revision is filled with the VCS (e.g. git) revision being used to build
|
// Revision is filled with the VCS (e.g. git) revision being used to build
|
||||||
// the program at linking time.
|
// the program at linking time.
|
||||||
Revision = ""
|
Revision = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
reRelease *regexp.Regexp
|
||||||
|
reDev *regexp.Regexp
|
||||||
|
reOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserAgent() string {
|
||||||
|
version := defaultVersion
|
||||||
|
|
||||||
|
reOnce.Do(func() {
|
||||||
|
reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`)
|
||||||
|
reDev = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+`)
|
||||||
|
})
|
||||||
|
|
||||||
|
if matches := reRelease.FindAllStringSubmatch(version, 1); len(matches) > 0 {
|
||||||
|
version = matches[0][1]
|
||||||
|
} else if matches := reDev.FindAllStringSubmatch(version, 1); len(matches) > 0 {
|
||||||
|
version = matches[0][1] + "-dev"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "buildkit/" + version
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue