Skip to content

Commit

Permalink
Merge pull request moby#47024 from thaJeztah/fix_daemon_side_digest_r…
Browse files Browse the repository at this point in the history
…esolve

daemon/cluster: Cluster.imageWithDigestString: include mirrors to resolve digest
  • Loading branch information
thaJeztah authored Jan 5, 2024
2 parents 9cebefa + 07d2ad3 commit c3c1ee0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
1 change: 0 additions & 1 deletion daemon/cluster/executor/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type VolumeBackend interface {
// ImageBackend is used by an executor to perform image operations
type ImageBackend interface {
PullImage(ctx context.Context, ref reference.Named, platform *ocispec.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error
GetRepository(context.Context, reference.Named, *registry.AuthConfig) (distribution.Repository, error)
GetRepositories(context.Context, reference.Named, *registry.AuthConfig) ([]distribution.Repository, error)
GetImage(ctx context.Context, refOrID string, options opts.GetImageOpts) (*image.Image, error)
}
25 changes: 20 additions & 5 deletions daemon/cluster/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
runconfigopts "github.com/docker/docker/runconfig/opts"
gogotypes "github.com/gogo/protobuf/types"
swarmapi "github.com/moby/swarmkit/v2/api"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -635,16 +636,30 @@ func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authC
return "", errors.Errorf("image reference not tagged: %s", image)
}

repo, err := c.config.ImageBackend.GetRepository(ctx, taggedRef, authConfig)
// Fetch the image manifest's digest; if a mirror is configured, try the
// mirror first, but continue with upstream on failure.
repos, err := c.config.ImageBackend.GetRepositories(ctx, taggedRef, authConfig)
if err != nil {
return "", err
}
dscrptr, err := repo.Tags(ctx).Get(ctx, taggedRef.Tag())
if err != nil {
return "", err

var (
imgDigest digest.Digest
lastErr error
)
for _, repo := range repos {
dscrptr, err := repo.Tags(ctx).Get(ctx, taggedRef.Tag())
if err != nil {
lastErr = err
continue
}
imgDigest = dscrptr.Digest
}
if lastErr != nil {
return "", lastErr
}

namedDigestedRef, err := reference.WithDigest(taggedRef, dscrptr.Digest)
namedDigestedRef, err := reference.WithDigest(taggedRef, imgDigest)
if err != nil {
return "", err
}
Expand Down
10 changes: 0 additions & 10 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,16 +1586,6 @@ type imageBackend struct {
registryService *registry.Service
}

// GetRepository returns a repository from the registry.
func (i *imageBackend) GetRepository(ctx context.Context, ref reference.Named, authConfig *registrytypes.AuthConfig) (dist.Repository, error) {
return distribution.GetRepository(ctx, ref, &distribution.ImagePullConfig{
Config: distribution.Config{
AuthConfig: authConfig,
RegistryService: i.registryService,
},
})
}

// GetRepositories returns a list of repositories configured for the given
// reference. Multiple repositories can be returned if the reference is for
// the default (Docker Hub) registry and a mirror is configured, but it omits
Expand Down
16 changes: 0 additions & 16 deletions distribution/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"github.com/docker/docker/errdefs"
)

// GetRepository returns a repository from the registry.
func GetRepository(ctx context.Context, ref reference.Named, config *ImagePullConfig) (repository distribution.Repository, lastError error) {
repos, err := getRepositories(ctx, ref, config, true)
if len(repos) == 0 {
return nil, err
}
return repos[0], nil
}

// GetRepositories returns a list of repositories configured for the given
// reference. Multiple repositories can be returned if the reference is for
// the default (Docker Hub) registry and a mirror is configured, but it omits
Expand All @@ -26,10 +17,6 @@ func GetRepository(ctx context.Context, ref reference.Named, config *ImagePullCo
// It returns an error if it was unable to reach any of the registries for
// the given reference, or if the provided reference is invalid.
func GetRepositories(ctx context.Context, ref reference.Named, config *ImagePullConfig) ([]distribution.Repository, error) {
return getRepositories(ctx, ref, config, false)
}

func getRepositories(ctx context.Context, ref reference.Named, config *ImagePullConfig, firstOnly bool) ([]distribution.Repository, error) {
repoInfo, err := config.RegistryService.ResolveRepository(ref)
if err != nil {
return nil, errdefs.InvalidParameter(err)
Expand All @@ -56,9 +43,6 @@ func getRepositories(ctx context.Context, ref reference.Named, config *ImagePull
continue
}
repositories = append(repositories, repo)
if firstOnly {
return repositories, nil
}
}
if len(repositories) == 0 {
return nil, lastError
Expand Down

0 comments on commit c3c1ee0

Please sign in to comment.