提交 16d4be92 编写于 作者: X xiadanni

tag: modify tag expand method

do not add "localhost" if tag does not include repo
Signed-off-by: Nxiadanni <xiadanni1@huawei.com>
上级 f5768402
......@@ -136,13 +136,13 @@ func NewBuilder(ctx context.Context, store store.Store, req *pb.BuildRequest, ru
func (b *Builder) parseTag(output, additionalTag string) error {
var err error
if tag := parseOutputTag(output); tag != "" {
if b.buildOpts.Tag, err = ExpandTag(tag, b.localStore); err != nil {
if b.buildOpts.Tag, err = CheckAndExpandTag(tag); err != nil {
return err
}
}
if additionalTag != "" {
if b.buildOpts.AdditionalTag, err = ExpandTag(additionalTag, b.localStore); err != nil {
if b.buildOpts.AdditionalTag, err = CheckAndExpandTag(additionalTag); err != nil {
return err
}
}
......@@ -570,19 +570,35 @@ func parseOutputTag(output string) string {
return tag
}
// ExpandTag resolves tag name, if it not include a domain, "localhost" will be
// added, and if it not include a tag, "latest" will be added.
func ExpandTag(tag string, store store.Store) (string, error) {
candidates, _, err := image.ResolveName(tag, nil, store)
if err != nil || len(candidates) == 0 {
return "", errors.Errorf("resolve tag %v err: %v", tag, err)
// CheckAndExpandTag checks tag name. If it not include a tag, "latest" will be added.
func CheckAndExpandTag(tag string) (string, error) {
if tag == "" {
return "<none>:<none>", nil
}
tagNamed, err := reference.ParseNormalizedNamed(candidates[0])
slashLastIndex := strings.LastIndex(tag, "/")
sepLastIndex := strings.LastIndex(tag, ":")
if sepLastIndex == -1 || (sepLastIndex < slashLastIndex) {
// isula
// localhost:5000/isula
tag += ":latest"
}
const longestTagFieldsLen = 3
if len(strings.Split(tag, ":")) > longestTagFieldsLen {
// localhost:5000:5000/isula:latest
return "", errors.Errorf("invalid tag: %v", tag)
}
tagWithoutRepo := tag[slashLastIndex+1:]
_, err := reference.ParseNormalizedNamed(tagWithoutRepo)
if err != nil {
return "", errors.Wrapf(err, "parse tag %v err", candidates[0])
// isula:latest:latest
// localhost/isula:latest:latest
// isula!@#:latest
// isula :latest
return "", errors.Wrapf(err, "parse tag err, invalid tag: %v", tag)
}
tagNamed = reference.TagNameOnly(tagNamed)
return tagNamed.String(), nil
return tag, nil
}
......@@ -1397,3 +1397,87 @@ func TestNewBuilder(t *testing.T) {
})
}
}
func TestCheckAndExpandTag(t *testing.T) {
type testcase struct {
name string
tag string
output string
wantErr bool
}
testcases := []testcase{
{
name: "test 1",
tag: "isula/test",
output: "isula/test:latest",
wantErr: false,
},
{
name: "test 2",
tag: "localhost:5000/test",
output: "localhost:5000/test:latest",
wantErr: false,
},
{
name: "test 3",
tag: "isula/test:latest",
output: "isula/test:latest",
wantErr: false,
},
{
name: "test 4",
tag: "localhost:5000/test:latest",
output: "localhost:5000/test:latest",
wantErr: false,
},
{
name: "test 5",
tag: "localhost:5000:aaa/test:latest",
output: "",
wantErr: true,
},
{
name: "test 6",
tag: "localhost:5000:aaa/test",
output: "",
wantErr: true,
},
{
name: "test 7",
tag: "localhost:5000/test:latest:latest",
output: "",
wantErr: true,
},
{
name: "test 8",
tag: "test:latest:latest",
output: "",
wantErr: true,
},
{
name: "test 9",
tag: "",
output: "<none>:<none>",
wantErr: false,
},
{
name: "test 10",
tag: "abc efg:latest",
output: "",
wantErr: true,
},
{
name: "test 10",
tag: "abc!@#:latest",
output: "",
wantErr: true,
},
}
for _, tc := range testcases {
tag, err := CheckAndExpandTag(tc.tag)
assert.Equal(t, tag, tc.output, tc.name)
if (err != nil) != tc.wantErr {
t.Errorf("getCheckAndExpandTag() error = %v, wantErr %v", err, tc.wantErr)
}
}
}
......@@ -57,7 +57,7 @@ func (b *Backend) Import(serv pb.Control_ImportServer) error {
logrus.Infof("Received and import image %q", reference)
reference, err := dockerfile.ExpandTag(reference, localStore)
reference, err := dockerfile.CheckAndExpandTag(reference)
if err != nil {
return err
}
......
......@@ -40,7 +40,7 @@ func (b *Backend) Tag(ctx context.Context, req *pb.TagRequest) (*gogotypes.Empty
return emptyResp, errors.Wrapf(err, "find local image %v error", req.Image)
}
imageName, err := dockerfile.ExpandTag(req.Tag, s)
imageName, err := dockerfile.CheckAndExpandTag(req.Tag)
if err != nil {
return emptyResp, err
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册