Use docker/distribution library to resolve image reference.
Signed-off-by: Random-Liu <lantaol@google.com>
This commit is contained in:
@@ -20,28 +20,42 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
||||
|
||||
"github.com/containerd/containerd/content"
|
||||
containerdimages "github.com/containerd/containerd/images"
|
||||
"github.com/containerd/containerd/remotes"
|
||||
"github.com/containerd/containerd/remotes/docker"
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/golang/glog"
|
||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"golang.org/x/net/context"
|
||||
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
||||
)
|
||||
|
||||
// PullImage pulls an image with authentication config.
|
||||
// TODO(mikebrow): add authentication
|
||||
// TODO(mikebrow): harden api (including figuring out at what layer we should be blocking on duplicate requests.)
|
||||
func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (*runtime.PullImageResponse, error) {
|
||||
func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (retRes *runtime.PullImageResponse, retErr error) {
|
||||
glog.V(2).Infof("PullImage %q with auth config %+v", r.GetImage().GetImage(), r.GetAuth())
|
||||
defer func() {
|
||||
if retErr == nil {
|
||||
glog.V(2).Infof("PullImage returns image reference %q", retRes.GetImageRef())
|
||||
}
|
||||
}()
|
||||
|
||||
var (
|
||||
err error
|
||||
size int64
|
||||
desc imagespec.Descriptor
|
||||
)
|
||||
|
||||
image := r.GetImage().Image
|
||||
image, err := normalizeImageRef(r.GetImage().GetImage())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse image reference %q: %v", r.GetImage().GetImage(), err)
|
||||
}
|
||||
if r.GetImage().GetImage() != image {
|
||||
glog.V(4).Info("PullImage using normalized image ref: %q", image)
|
||||
}
|
||||
|
||||
if desc, size, err = c.pullImage(ctx, image); err != nil {
|
||||
return nil, fmt.Errorf("failed to pull image %q: %v", image, err)
|
||||
@@ -66,6 +80,17 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
|
||||
return &runtime.PullImageResponse{ImageRef: digest}, err
|
||||
}
|
||||
|
||||
// normalizeImageRef normalizes the image reference following the docker convention. This is added
|
||||
// mainly for backward compatibility.
|
||||
func normalizeImageRef(ref string) (string, error) {
|
||||
named, err := reference.ParseNormalizedNamed(ref)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
named = reference.TagNameOnly(named)
|
||||
return named.String(), nil
|
||||
}
|
||||
|
||||
// imageReferenceResolver attempts to resolve the image reference into a name
|
||||
// and manifest via the containerd library call..
|
||||
//
|
||||
|
||||
45
pkg/server/image_pull_test.go
Normal file
45
pkg/server/image_pull_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/containerd/containerd/reference"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNormalizeImageRef(t *testing.T) {
|
||||
for _, ref := range []string{
|
||||
"busybox", // has nothing
|
||||
"busybox:latest", // only has tag
|
||||
"busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582", // only has digest
|
||||
"library/busybox", // only has path
|
||||
"docker.io/busybox", // only has hostname
|
||||
"docker.io/library/busybox", // has no tag
|
||||
"docker.io/busybox:latest", // has no path
|
||||
"library/busybox:latest", // has no hostname
|
||||
"docker.io/library/busybox:latest", // full reference
|
||||
"gcr.io/library/busybox", // gcr reference
|
||||
} {
|
||||
t.Logf("TestCase %q", ref)
|
||||
normalized, err := normalizeImageRef(ref)
|
||||
assert.NoError(t, err)
|
||||
_, err = reference.Parse(normalized)
|
||||
assert.NoError(t, err, "%q should be containerd supported reference", normalized)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user