Rename imageservice.IsImagePresent to GetImageRef

This commit is contained in:
Pengfei Ni 2016-12-31 08:36:55 +08:00
parent 1de92a91e9
commit 67a5bf8454
12 changed files with 36 additions and 33 deletions

View File

@ -148,8 +148,9 @@ type ImageService interface {
// PullImage pulls an image from the network to local storage using the supplied
// secrets if necessary. It returns a reference (digest or ID) to the pulled image.
PullImage(image ImageSpec, pullSecrets []v1.Secret) (string, error)
// IsImagePresent checks whether the container image is already in the local storage.
IsImagePresent(image ImageSpec) (string, error)
// GetImageRef gets the reference (digest or ID) of the image which has already been in
// the local storage. It returns ("", nil) if the image isn't in the local storage.
GetImageRef(image ImageSpec) (string, error)
// Gets all images currently on the machine.
ListImages() ([]Image, error)
// Removes the specified image.

View File

@ -356,11 +356,11 @@ func (f *FakeRuntime) PullImage(image ImageSpec, pullSecrets []v1.Secret) (strin
return image.Image, f.Err
}
func (f *FakeRuntime) IsImagePresent(image ImageSpec) (string, error) {
func (f *FakeRuntime) GetImageRef(image ImageSpec) (string, error) {
f.Lock()
defer f.Unlock()
f.CalledFunctions = append(f.CalledFunctions, "IsImagePresent")
f.CalledFunctions = append(f.CalledFunctions, "GetImageRef")
for _, i := range f.ImageList {
if i.ID == image.Image {
return i.ID, nil

View File

@ -110,7 +110,7 @@ func (r *Mock) PullImage(image ImageSpec, pullSecrets []v1.Secret) (string, erro
return image.Image, args.Error(0)
}
func (r *Mock) IsImagePresent(image ImageSpec) (string, error) {
func (r *Mock) GetImageRef(image ImageSpec) (string, error) {
args := r.Called(image)
return args.Get(0).(string), args.Error(1)
}

View File

@ -94,7 +94,7 @@ func SetContainerNamePrefix(prefix string) {
// DockerPuller is an abstract interface for testability. It abstracts image pull operations.
type DockerPuller interface {
Pull(image string, secrets []v1.Secret) error
IsImagePresent(image string) (string, error)
GetImageRef(image string) (string, error)
}
// dockerPuller is the default implementation of DockerPuller.
@ -241,7 +241,7 @@ func (p dockerPuller) Pull(image string, secrets []v1.Secret) error {
err := p.client.PullImage(image, dockertypes.AuthConfig{}, opts)
if err == nil {
// Sometimes PullImage failed with no error returned.
imageRef, ierr := p.IsImagePresent(image)
imageRef, ierr := p.GetImageRef(image)
if ierr != nil {
glog.Warningf("Failed to inspect image %s: %v", image, ierr)
}
@ -277,7 +277,7 @@ func (p dockerPuller) Pull(image string, secrets []v1.Secret) error {
return utilerrors.NewAggregate(pullErrs)
}
func (p dockerPuller) IsImagePresent(image string) (string, error) {
func (p dockerPuller) GetImageRef(image string) (string, error) {
resp, err := p.client.InspectImageByRef(image)
if err == nil {
if resp == nil {

View File

@ -992,9 +992,10 @@ func (dm *DockerManager) PullImage(image kubecontainer.ImageSpec, secrets []v1.S
return GetImageRef(dm.client, image.Image)
}
// IsImagePresent checks whether the container image is already in the local storage.
func (dm *DockerManager) IsImagePresent(image kubecontainer.ImageSpec) (string, error) {
return dm.dockerPuller.IsImagePresent(image.Image)
// GetImageRef gets the reference (digest or ID) of the image which has already been in
// the local storage. It returns ("", nil) if the image isn't in the local storage.
func (dm *DockerManager) GetImageRef(image kubecontainer.ImageSpec) (string, error) {
return dm.dockerPuller.GetImageRef(image.Image)
}
// Removes the specified image.

View File

@ -705,12 +705,12 @@ func (f *imageTrackingDockerClient) InspectImageByRef(name string) (image *docke
return
}
func TestIsImagePresent(t *testing.T) {
func TestGetImageRef(t *testing.T) {
cl := &imageTrackingDockerClient{NewFakeDockerClient(), ""}
puller := &dockerPuller{
client: cl,
}
_, _ = puller.IsImagePresent("abc:123")
_, _ = puller.GetImageRef("abc:123")
if cl.imageName != "abc:123" {
t.Errorf("expected inspection of image abc:123, instead inspected image %v", cl.imageName)
}

View File

@ -596,7 +596,7 @@ func (f *FakeDockerPuller) Pull(image string, secrets []v1.Secret) (err error) {
return err
}
func (f *FakeDockerPuller) IsImagePresent(name string) (string, error) {
func (f *FakeDockerPuller) GetImageRef(name string) (string, error) {
f.Lock()
defer f.Unlock()
if f.HasImages == nil {

View File

@ -99,7 +99,7 @@ func (m *imageManager) EnsureImageExists(pod *v1.Pod, container *v1.Container, p
}
spec := kubecontainer.ImageSpec{Image: image}
imageRef, err := m.imageService.IsImagePresent(spec)
imageRef, err := m.imageService.GetImageRef(spec)
if err != nil {
msg := fmt.Sprintf("Failed to inspect image %q: %v", container.Image, err)
m.logIt(ref, v1.EventTypeWarning, events.FailedToInspectImage, logPrefix, msg, glog.Warning)
@ -128,20 +128,20 @@ func (m *imageManager) EnsureImageExists(pod *v1.Pod, container *v1.Container, p
m.logIt(ref, v1.EventTypeNormal, events.PullingImage, logPrefix, fmt.Sprintf("pulling image %q", container.Image), glog.Info)
pullChan := make(chan pullResult)
m.puller.pullImage(spec, pullSecrets, pullChan)
imageRefWithErr := <-pullChan
if imageRefWithErr.err != nil {
m.logIt(ref, v1.EventTypeWarning, events.FailedToPullImage, logPrefix, fmt.Sprintf("Failed to pull image %q: %v", container.Image, imageRefWithErr.err), glog.Warning)
imagePullResult := <-pullChan
if imagePullResult.err != nil {
m.logIt(ref, v1.EventTypeWarning, events.FailedToPullImage, logPrefix, fmt.Sprintf("Failed to pull image %q: %v", container.Image, imagePullResult.err), glog.Warning)
m.backOff.Next(backOffKey, m.backOff.Clock.Now())
if imageRefWithErr.err == RegistryUnavailable {
if imagePullResult.err == RegistryUnavailable {
msg := fmt.Sprintf("image pull failed for %s because the registry is unavailable.", container.Image)
return "", msg, imageRefWithErr.err
return "", msg, imagePullResult.err
}
return "", imageRefWithErr.err.Error(), ErrImagePull
return "", imagePullResult.err.Error(), ErrImagePull
}
m.logIt(ref, v1.EventTypeNormal, events.PulledImage, logPrefix, fmt.Sprintf("Successfully pulled image %q", container.Image), glog.Info)
m.backOff.GC()
return imageRefWithErr.imageRef, "", nil
return imagePullResult.imageRef, "", nil
}
// applyDefaultImageTag parses a docker image string, if it doesn't contain any tag or digest,

View File

@ -44,7 +44,7 @@ func pullerTestCases() []pullerTestCase {
{ // pull missing image
containerImage: "missing_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"IsImagePresent", "PullImage"},
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil}},
@ -52,35 +52,35 @@ func pullerTestCases() []pullerTestCase {
{ // image present, don't pull
containerImage: "present_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"IsImagePresent"},
calledFunctions: []string{"GetImageRef"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil, nil, nil}},
// image present, pull it
{containerImage: "present_image",
policy: v1.PullAlways,
calledFunctions: []string{"IsImagePresent", "PullImage"},
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil, nil, nil}},
// missing image, error PullNever
{containerImage: "missing_image",
policy: v1.PullNever,
calledFunctions: []string{"IsImagePresent"},
calledFunctions: []string{"GetImageRef"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{ErrImageNeverPull, ErrImageNeverPull, ErrImageNeverPull}},
// missing image, unable to inspect
{containerImage: "missing_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"IsImagePresent"},
calledFunctions: []string{"GetImageRef"},
inspectErr: errors.New("unknown inspectError"),
pullerErr: nil,
expectedErr: []error{ErrImageInspect, ErrImageInspect, ErrImageInspect}},
// missing image, unable to fetch
{containerImage: "typo_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"IsImagePresent", "PullImage"},
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: errors.New("404"),
expectedErr: []error{ErrImagePull, ErrImagePull, ErrImagePullBackOff, ErrImagePull, ErrImagePullBackOff, ErrImagePullBackOff}},

View File

@ -78,8 +78,9 @@ func (m *kubeGenericRuntimeManager) PullImage(image kubecontainer.ImageSpec, pul
return "", utilerrors.NewAggregate(pullErrs)
}
// IsImagePresent checks whether the container image is already in the local storage.
func (m *kubeGenericRuntimeManager) IsImagePresent(image kubecontainer.ImageSpec) (string, error) {
// GetImageRef gets the reference (digest or ID) of the image which has already been in
// the local storage. It returns ("", nil) if the image isn't in the local storage.
func (m *kubeGenericRuntimeManager) GetImageRef(image kubecontainer.ImageSpec) (string, error) {
status, err := m.imageService.ImageStatus(&runtimeapi.ImageSpec{Image: &image.Image})
if err != nil {
glog.Errorf("ImageStatus for image %q failed: %v", image, err)

View File

@ -56,13 +56,13 @@ func TestListImages(t *testing.T) {
assert.Equal(t, expected.List(), actual.List())
}
func TestIsImagePresent(t *testing.T) {
func TestGetImageRef(t *testing.T) {
_, fakeImageService, fakeManager, err := createTestRuntimeManager()
assert.NoError(t, err)
image := "busybox"
fakeImageService.SetFakeImages([]string{image})
imageRef, err := fakeManager.IsImagePresent(kubecontainer.ImageSpec{Image: image})
imageRef, err := fakeManager.GetImageRef(kubecontainer.ImageSpec{Image: image})
assert.NoError(t, err)
assert.Equal(t, image, imageRef)
}

View File

@ -87,7 +87,7 @@ func (r *Runtime) PullImage(image kubecontainer.ImageSpec, pullSecrets []v1.Secr
return r.getImageID(img)
}
func (r *Runtime) IsImagePresent(image kubecontainer.ImageSpec) (string, error) {
func (r *Runtime) GetImageRef(image kubecontainer.ImageSpec) (string, error) {
images, err := r.listImages(image.Image, false)
if err != nil {
return "", err