提交 eb426252 编写于 作者: X xiadanni

isula-build: fix build FROM local store image failed

Reason: When we resolve "FROM" image, ResolveName will first return a list of
candidates which includes localhost/<FROM image name>, then search and
get it from local store. But the image tag we add to built image before
only includes image name, without head "localhost", so it can not be
found.
Signed-off-by: Nxiadanni <xiadanni1@huawei.com>
上级 cf69507f
...@@ -104,7 +104,15 @@ func NewBuilder(ctx context.Context, store store.Store, req *pb.BuildRequest, ru ...@@ -104,7 +104,15 @@ func NewBuilder(ctx context.Context, store store.Store, req *pb.BuildRequest, ru
Iidfile: req.GetIidfile(), Iidfile: req.GetIidfile(),
} }
b.parseStaticBuildOpts(req) b.parseStaticBuildOpts(req)
b.buildOpts.Tag = parseTag(req.Output)
tag := parseTag(req.Output)
if tag != "" {
candidates, _, rerr := image.ResolveName(tag, nil, b.localStore)
if rerr != nil || len(candidates) == 0 {
return nil, errors.Wrapf(rerr, "parse target tag %v err", tag)
}
b.buildOpts.Tag = candidates[0]
}
// prepare workdirs for dockerfile builder // prepare workdirs for dockerfile builder
for _, dir := range []string{buildDir, runDir} { for _, dir := range []string{buildDir, runDir} {
...@@ -511,25 +519,29 @@ func (b *Builder) OutputPipeWrapper() *exporter.PipeWrapper { ...@@ -511,25 +519,29 @@ func (b *Builder) OutputPipeWrapper() *exporter.PipeWrapper {
func parseTag(output string) string { func parseTag(output string) string {
outputFields := strings.Split(output, ":") outputFields := strings.Split(output, ":")
if (outputFields[0] == "docker-daemon" || outputFields[0] == "isulad") && len(outputFields) > 1 {
return strings.Join(outputFields[1:], ":")
}
const archiveOutputWithoutTagLen = 2 const archiveOutputWithoutTagLen = 2
if outputFields[0] == "docker-archive" && len(outputFields) > archiveOutputWithoutTagLen {
var tag string
switch {
case (outputFields[0] == "docker-daemon" || outputFields[0] == "isulad") && len(outputFields) > 1:
tag = strings.Join(outputFields[1:], ":")
case outputFields[0] == "docker-archive" && len(outputFields) > archiveOutputWithoutTagLen:
if len(outputFields[archiveOutputWithoutTagLen:]) == 1 { if len(outputFields[archiveOutputWithoutTagLen:]) == 1 {
outputFields = append(outputFields, "latest") outputFields = append(outputFields, "latest")
} }
return strings.Join(outputFields[archiveOutputWithoutTagLen:], ":") tag = strings.Join(outputFields[archiveOutputWithoutTagLen:], ":")
} case outputFields[0] == "docker" && len(outputFields) > 1:
if outputFields[0] == "docker" && len(outputFields) > 1 {
repoAndTag := strings.Join(outputFields[1:], ":") repoAndTag := strings.Join(outputFields[1:], ":")
// repo format regexp, "//registry.example.com/" for example // repo format regexp, "//registry.example.com/" for example
repo := regexp.MustCompile(`^\/\/[\w\.\-\:]+\/`).FindString(repoAndTag) repo := regexp.MustCompile(`^\/\/[\w\.\-\:]+\/`).FindString(repoAndTag)
if repo == "" { if repo == "" {
return "" return ""
} }
return repoAndTag[len(repo):] tag = repoAndTag[len(repo):]
if len(strings.Split(tag, ":")) == 1 {
tag += ":latest"
}
} }
return "" return tag
} }
...@@ -1295,6 +1295,11 @@ func TestParseTag(t *testing.T) { ...@@ -1295,6 +1295,11 @@ func TestParseTag(t *testing.T) {
output: "docker://localhost:5000/isula/test:latest", output: "docker://localhost:5000/isula/test:latest",
tag: "isula/test:latest", tag: "isula/test:latest",
}, },
{
name: "docker output",
output: "docker://localhost:5000/isula/test",
tag: "isula/test:latest",
},
{ {
name: "invalid docker output", name: "invalid docker output",
output: "docker:localhost:5000/isula/test:latest", output: "docker:localhost:5000/isula/test:latest",
......
...@@ -132,7 +132,7 @@ func pullImage(opt pullOption) (types.ImageReference, error) { ...@@ -132,7 +132,7 @@ func pullImage(opt pullOption) (types.ImageReference, error) {
func pullAndGetImageInfo(opt *PrepareImageOptions) (types.ImageReference, *storage.Image, error) { func pullAndGetImageInfo(opt *PrepareImageOptions) (types.ImageReference, *storage.Image, error) {
pLog := logrus.WithField(util.LogKeyBuildID, opt.Ctx.Value(util.LogFieldKey(util.LogKeyBuildID))) pLog := logrus.WithField(util.LogKeyBuildID, opt.Ctx.Value(util.LogFieldKey(util.LogKeyBuildID)))
candidates, transport, err := resolveName(opt.FromImage, opt.SystemContext, opt.Store) candidates, transport, err := ResolveName(opt.FromImage, opt.SystemContext, opt.Store)
if err != nil { if err != nil {
return nil, nil, errors.Wrapf(err, "error parsing reference to image %q", opt.FromImage) return nil, nil, errors.Wrapf(err, "error parsing reference to image %q", opt.FromImage)
} }
...@@ -476,7 +476,7 @@ func ResolveImageName(s string, resolveArg func(string) string) (string, error) ...@@ -476,7 +476,7 @@ func ResolveImageName(s string, resolveArg func(string) string) (string, error)
// FindImage get the image from storage by image describe // FindImage get the image from storage by image describe
func FindImage(store store.Store, image string) (types.ImageReference, *storage.Image, error) { func FindImage(store store.Store, image string) (types.ImageReference, *storage.Image, error) {
names, _, err := resolveName(image, nil, store) names, _, err := ResolveName(image, nil, store)
if err != nil { if err != nil {
return nil, nil, errors.Wrapf(err, "error parsing name %q", image) return nil, nil, errors.Wrapf(err, "error parsing name %q", image)
} }
...@@ -508,7 +508,9 @@ func FindImage(store store.Store, image string) (types.ImageReference, *storage. ...@@ -508,7 +508,9 @@ func FindImage(store store.Store, image string) (types.ImageReference, *storage.
return ref, img, nil return ref, img, nil
} }
func resolveName(name string, sc *types.SystemContext, store store.Store) ([]string, string, error) { // ResolveName checks whether the image name is valid, if the name does not include a domain,
// returns a list of candidates it might be
func ResolveName(name string, sc *types.SystemContext, store store.Store) ([]string, string, error) {
// 1. check name valid // 1. check name valid
if name == "" { if name == "" {
return nil, "", nil return nil, "", nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册