Fix use of docker removed ParseRepositoryTag() function

Docker has removed the ParseRepositoryTag() function in
leading to failures using the kubernetes Go client API.

Lets use github.com/docker/distribution reference.ParseNamed()
instead.

Failure:

../k8s.io/kubernetes/pkg/util/parsers/parsers.go:30: undefined: parsers.ParseRepositoryTag
This commit is contained in:
Stef Walter
2016-03-30 16:23:07 +02:00
parent 04da473594
commit 481dbca8bc
7 changed files with 505 additions and 224 deletions

View File

@@ -66,7 +66,9 @@ func addDefaultingFuncs(scheme *runtime.Scheme) {
},
func(obj *Container) {
if obj.ImagePullPolicy == "" {
_, tag := parsers.ParseImageName(obj.Image)
// Ignore error and assume it has been validated elsewhere
_, tag, _ := parsers.ParseImageName(obj.Image)
// Check image tag
if tag == "latest" {

View File

@@ -145,7 +145,10 @@ func filterHTTPError(err error, image string) error {
func (p dockerPuller) Pull(image string, secrets []api.Secret) error {
// If no tag was specified, use the default "latest".
imageID, tag := parsers.ParseImageName(image)
imageID, tag, err := parsers.ParseImageName(image)
if err != nil {
return err
}
keyring, err := credentialprovider.MakeDockerKeyring(secrets, p.keyring)
if err != nil {

View File

@@ -171,8 +171,10 @@ func TestParseImageName(t *testing.T) {
{"registry.example.com:5000/foobar:latest", "registry.example.com:5000/foobar", "latest"},
}
for _, test := range tests {
name, tag := parsers.ParseImageName(test.imageName)
if name != test.name || tag != test.tag {
name, tag, err := parsers.ParseImageName(test.imageName)
if err != nil {
t.Errorf("ParseImageName(%s) failed: %v", test.imageName, err)
} else if name != test.name || tag != test.tag {
t.Errorf("Expected name/tag: %s/%s, got %s/%s", test.name, test.tag, name, tag)
}
}

View File

@@ -47,7 +47,11 @@ func (r *Runtime) PullImage(image kubecontainer.ImageSpec, pullSecrets []api.Sec
img := image.Image
// TODO(yifan): The credential operation is a copy from dockertools package,
// Need to resolve the code duplication.
repoToPull, _ := parsers.ParseImageName(img)
repoToPull, _, err := parsers.ParseImageName(img)
if err != nil {
return err
}
keyring, err := credentialprovider.MakeDockerKeyring(pullSecrets, r.dockerKeyring)
if err != nil {
return err
@@ -138,7 +142,11 @@ func (s sortByImportTime) Less(i, j int) bool { return s[i].ImportTimestamp < s[
// will return the result reversely sorted by the import time, so that the latest
// image comes first.
func (r *Runtime) listImages(image string, detail bool) ([]*rktapi.Image, error) {
repoToPull, tag := parsers.ParseImageName(image)
repoToPull, tag, err := parsers.ParseImageName(image)
if err != nil {
return nil, err
}
listResp, err := r.apisvc.ListImages(context.Background(), &rktapi.ListImagesRequest{
Detail: detail,
Filters: []*rktapi.ImageFilter{

View File

@@ -17,7 +17,9 @@ limitations under the License.
package parsers
import (
"github.com/docker/docker/pkg/parsers"
"fmt"
"github.com/docker/distribution/reference"
)
const (
@@ -26,11 +28,28 @@ const (
// parseImageName parses a docker image string into two parts: repo and tag.
// If tag is empty, return the defaultImageTag.
func ParseImageName(image string) (string, string) {
repoToPull, tag := parsers.ParseRepositoryTag(image)
func ParseImageName(image string) (string, string, error) {
named, err := reference.ParseNamed(image)
if err != nil {
return "", "", fmt.Errorf("couldn't parse image name: %v", err)
}
repoToPull := named.Name()
var tag string
tagged, ok := named.(reference.Tagged)
if ok {
tag = tagged.Tag()
}
digested, ok := named.(reference.Digested)
if ok {
tag = digested.Digest().String()
}
// If no tag was specified, use the default "latest".
if len(tag) == 0 {
tag = defaultImageTag
}
return repoToPull, tag
return repoToPull, tag, nil
}

View File

@@ -0,0 +1,49 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 parsers
import (
"testing"
)
// Based on Docker test case removed in:
// https://github.com/docker/docker/commit/4352da7803d182a6013a5238ce20a7c749db979a
func TestParseImageName(t *testing.T) {
testCases := []struct {
Input string
Repo string
Image string
}{
{Input: "root", Repo: "root", Image: "latest"},
{Input: "root:tag", Repo: "root", Image: "tag"},
{Input: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "root", Image: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{Input: "user/repo", Repo: "user/repo", Image: "latest"},
{Input: "user/repo:tag", Repo: "user/repo", Image: "tag"},
{Input: "user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "user/repo", Image: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{Input: "url:5000/repo", Repo: "url:5000/repo", Image: "latest"},
{Input: "url:5000/repo:tag", Repo: "url:5000/repo", Image: "tag"},
{Input: "url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "url:5000/repo", Image: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
}
for _, testCase := range testCases {
repo, image, err := ParseImageName(testCase.Input)
if err != nil {
t.Errorf("ParseImageName(%s) failed: %v", testCase.Input, err)
} else if repo != testCase.Repo || image != testCase.Image {
t.Errorf("Expected repo: '%s' and image: '%s', got '%s' and '%s'", "root", "", repo, image)
}
}
}