Refactoring code for pinning image by digest

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
This commit is contained in:
Nishant Totla 2016-11-14 16:12:24 -08:00
parent d910781c66
commit dc1b6341b9
No known key found for this signature in database
GPG key ID: 7EA5781C9B3D0C19

View file

@ -16,6 +16,7 @@ import (
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
distreference "github.com/docker/distribution/reference"
apierrors "github.com/docker/docker/api/errors" apierrors "github.com/docker/docker/api/errors"
apitypes "github.com/docker/docker/api/types" apitypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/backend"
@ -1008,16 +1009,25 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
// imageWithDigestString takes an image such as name or name:tag // imageWithDigestString takes an image such as name or name:tag
// and returns the image pinned to a digest, such as name@sha256:34234... // and returns the image pinned to a digest, such as name@sha256:34234...
// Due to the difference between the docker/docker/reference, and the
// docker/distribution/reference packages, we're parsing the image twice.
// As the two packages converge, this function should be simplified.
// TODO(nishanttotla): After the packages converge, the function must
// convert distreference.Named -> distreference.Canonical, and the logic simplified.
func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authConfig *apitypes.AuthConfig) (string, error) { func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authConfig *apitypes.AuthConfig) (string, error) {
ref, err := reference.ParseNamed(image) ref, err := distreference.ParseNamed(image)
if err != nil { if err != nil {
return "", err return "", err
} }
// only query registry if not a canonical reference (i.e. with digest) // only query registry if not a canonical reference (i.e. with digest)
if _, ok := ref.(reference.Canonical); !ok { if _, ok := ref.(distreference.Canonical); !ok {
ref = reference.WithDefaultTag(ref) // create a docker/docker/reference Named object because GetRepository needs it
dockerRef, err := reference.ParseNamed(image)
namedTaggedRef, ok := ref.(reference.NamedTagged) if err != nil {
return "", err
}
dockerRef = reference.WithDefaultTag(dockerRef)
namedTaggedRef, ok := dockerRef.(reference.NamedTagged)
if !ok { if !ok {
return "", fmt.Errorf("unable to cast image to NamedTagged reference object") return "", fmt.Errorf("unable to cast image to NamedTagged reference object")
} }
@ -1031,15 +1041,11 @@ func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authC
return "", err return "", err
} }
// TODO(nishanttotla): Currently, the service would lose the tag while calling WithDigest namedDigestedRef, err := distreference.WithDigest(distreference.EnsureTagged(ref), dscrptr.Digest)
// To prevent this, we create the image string manually, which is a bad idea in general if err != nil {
// This will be fixed when https://github.com/docker/distribution/pull/2044 is vendored return "", err
// namedDigestedRef, err := reference.WithDigest(ref, dscrptr.Digest) }
// if err != nil { return namedDigestedRef.String(), nil
// return "", err
// }
// return namedDigestedRef.String(), nil
return image + "@" + dscrptr.Digest.String(), nil
} else { } else {
// reference already contains a digest, so just return it // reference already contains a digest, so just return it
return ref.String(), nil return ref.String(), nil