Replace util.NormalizeImageRef with reference.ParseDockerRef
Using the utility caused other project to have containerd/cri as a dependency, only for this utility. The new `reference.ParseDockerRef` function does the same (it's a copy of this function). Tests were kept for now, but could be removed in future. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
0ad60d4d9e
commit
51affb8839
@ -32,13 +32,13 @@ import (
|
|||||||
"github.com/containerd/containerd/images"
|
"github.com/containerd/containerd/images"
|
||||||
"github.com/containerd/containerd/leases"
|
"github.com/containerd/containerd/leases"
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
"github.com/opencontainers/image-spec/specs-go"
|
"github.com/opencontainers/image-spec/specs-go"
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
ctrdutil "github.com/containerd/cri/pkg/containerd/util"
|
ctrdutil "github.com/containerd/cri/pkg/containerd/util"
|
||||||
"github.com/containerd/cri/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// This code reuses the docker import code from containerd/containerd#1602.
|
// This code reuses the docker import code from containerd/containerd#1602.
|
||||||
@ -220,7 +220,7 @@ func Import(ctx context.Context, client *containerd.Client, reader io.Reader, op
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, ref := range mfst.RepoTags {
|
for _, ref := range mfst.RepoTags {
|
||||||
normalized, err := util.NormalizeImageRef(ref)
|
normalized, err := reference.ParseDockerRef(ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return refs, errors.Wrapf(err, "normalize image ref %q", ref)
|
return refs, errors.Wrapf(err, "normalize image ref %q", ref)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,6 @@ import (
|
|||||||
containerstore "github.com/containerd/cri/pkg/store/container"
|
containerstore "github.com/containerd/cri/pkg/store/container"
|
||||||
imagestore "github.com/containerd/cri/pkg/store/image"
|
imagestore "github.com/containerd/cri/pkg/store/image"
|
||||||
sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
|
sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
|
||||||
"github.com/containerd/cri/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -264,7 +263,7 @@ func (c *criService) localResolve(refOrID string) (imagestore.Image, error) {
|
|||||||
return func(ref string) string {
|
return func(ref string) string {
|
||||||
// ref is not image id, try to resolve it locally.
|
// ref is not image id, try to resolve it locally.
|
||||||
// TODO(random-liu): Handle this error better for debugging.
|
// TODO(random-liu): Handle this error better for debugging.
|
||||||
normalized, err := util.NormalizeImageRef(ref)
|
normalized, err := reference.ParseDockerRef(ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/containerd/containerd/runtime/linux/runctypes"
|
"github.com/containerd/containerd/runtime/linux/runctypes"
|
||||||
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
|
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
imagedigest "github.com/opencontainers/go-digest"
|
imagedigest "github.com/opencontainers/go-digest"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -31,7 +32,6 @@ import (
|
|||||||
criconfig "github.com/containerd/cri/pkg/config"
|
criconfig "github.com/containerd/cri/pkg/config"
|
||||||
"github.com/containerd/cri/pkg/store"
|
"github.com/containerd/cri/pkg/store"
|
||||||
imagestore "github.com/containerd/cri/pkg/store/image"
|
imagestore "github.com/containerd/cri/pkg/store/image"
|
||||||
"github.com/containerd/cri/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGetUserFromImage tests the logic of getting image uid or user name of image user.
|
// TestGetUserFromImage tests the logic of getting image uid or user name of image user.
|
||||||
@ -104,7 +104,7 @@ func TestGetRepoDigestAndTag(t *testing.T) {
|
|||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Logf("TestCase %q", desc)
|
||||||
named, err := util.NormalizeImageRef(test.ref)
|
named, err := reference.ParseDockerRef(test.ref)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
repoDigest, repoTag := getRepoDigestAndTag(named, digest, test.schema1)
|
repoDigest, repoTag := getRepoDigestAndTag(named, digest, test.schema1)
|
||||||
assert.Equal(t, test.expectedRepoDigest, repoDigest)
|
assert.Equal(t, test.expectedRepoDigest, repoDigest)
|
||||||
|
@ -28,13 +28,12 @@ import (
|
|||||||
"github.com/containerd/containerd/reference"
|
"github.com/containerd/containerd/reference"
|
||||||
"github.com/containerd/containerd/remotes"
|
"github.com/containerd/containerd/remotes"
|
||||||
"github.com/containerd/containerd/remotes/docker"
|
"github.com/containerd/containerd/remotes/docker"
|
||||||
|
distribution "github.com/docker/distribution/reference"
|
||||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
||||||
|
|
||||||
"github.com/containerd/cri/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// For image management:
|
// For image management:
|
||||||
@ -81,7 +80,7 @@ import (
|
|||||||
// PullImage pulls an image with authentication config.
|
// PullImage pulls an image with authentication config.
|
||||||
func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (*runtime.PullImageResponse, error) {
|
func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (*runtime.PullImageResponse, error) {
|
||||||
imageRef := r.GetImage().GetImage()
|
imageRef := r.GetImage().GetImage()
|
||||||
namedRef, err := util.NormalizeImageRef(imageRef)
|
namedRef, err := distribution.ParseDockerRef(imageRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "failed to parse image reference %q", imageRef)
|
return nil, errors.Wrapf(err, "failed to parse image reference %q", imageRef)
|
||||||
}
|
}
|
||||||
|
@ -26,25 +26,8 @@ import (
|
|||||||
// and digest, the function returns digested reference, e.g. docker.io/library/busybox:latest@
|
// and digest, the function returns digested reference, e.g. docker.io/library/busybox:latest@
|
||||||
// sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa will be returned as
|
// sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa will be returned as
|
||||||
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa.
|
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa.
|
||||||
|
//
|
||||||
|
// Deprecated: use github.com/docker/reference.ParseDockerRef() instead
|
||||||
func NormalizeImageRef(ref string) (reference.Named, error) {
|
func NormalizeImageRef(ref string) (reference.Named, error) {
|
||||||
named, err := reference.ParseNormalizedNamed(ref)
|
return reference.ParseDockerRef(ref)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if _, ok := named.(reference.NamedTagged); ok {
|
|
||||||
if canonical, ok := named.(reference.Canonical); ok {
|
|
||||||
// The reference is both tagged and digested, only
|
|
||||||
// return digested.
|
|
||||||
newNamed, err := reference.WithName(canonical.Name())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
newCanonical, err := reference.WithDigest(newNamed, canonical.Digest())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return newCanonical, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return reference.TagNameOnly(named), nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user