Refactor dockerResolver to make code simple
1.add resolveDockerBase helper
2.dockerBase header copy with header.Clone()
@@ -219,20 +219,15 @@ func (r *countingReader) Read(p []byte) (int, error) {
var _ remotes.Resolver = &dockerResolver{}
func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocispec.Descriptor, error) {
- refspec, err := reference.Parse(ref)
+ base, err := r.resolveDockerBase(ref)
if err != nil {
return "", ocispec.Descriptor{}, err
}
-
+ refspec := base.refspec
if refspec.Object == "" {
return "", ocispec.Descriptor{}, reference.ErrObjectRequired
}
- base, err := r.base(refspec)
- if err != nil {
- return "", ocispec.Descriptor{}, err
- }
-
var (
lastErr error
paths [][]string
@@ -387,12 +382,7 @@ func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocisp
}
func (r *dockerResolver) Fetcher(ctx context.Context, ref string) (remotes.Fetcher, error) {
- refspec, err := reference.Parse(ref)
- if err != nil {
- return nil, err
- }
-
- base, err := r.base(refspec)
+ base, err := r.resolveDockerBase(ref)
if err != nil {
return nil, err
}
@@ -403,23 +393,27 @@ func (r *dockerResolver) Fetcher(ctx context.Context, ref string) (remotes.Fetch
}
func (r *dockerResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher, error) {
- refspec, err := reference.Parse(ref)
- if err != nil {
- return nil, err
- }
-
- base, err := r.base(refspec)
+ base, err := r.resolveDockerBase(ref)
if err != nil {
return nil, err
}
return dockerPusher{
dockerBase: base,
- object: refspec.Object,
+ object: base.refspec.Object,
tracker: r.tracker,
}, nil
}
+func (r *dockerResolver) resolveDockerBase(ref string) (*dockerBase, error) {
+ refspec, err := reference.Parse(ref)
+ if err != nil {
+ return nil, err
+ }
+
+ return r.base(refspec)
+}
+
type dockerBase struct {
refspec reference.Spec
repository string
@@ -451,10 +445,8 @@ func (r *dockerBase) filterHosts(caps HostCapabilities) (hosts []RegistryHost) {
}
func (r *dockerBase) request(host RegistryHost, method string, ps ...string) *request {
- header := http.Header{}
- for key, value := range r.header {
- header[key] = append(header[key], value...)
- }
+ header := r.header.Clone()
+
for key, value := range host.Header {
header[key] = append(header[key], value...)
}
