提交 b9e9c336 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!14 isula-build: add tag command

Merge pull request !14 from Vanient/master
此差异已折叠。
......@@ -38,6 +38,8 @@ service Control {
rpc Load(LoadRequest) returns (LoadResponse);
// Import requests import a new image
rpc Import(stream ImportRequest) returns (ImportResponse);
// Tag requests to tag an image
rpc tag(TagRequest) returns (google.protobuf.Empty);
}
message BuildRequest {
......@@ -104,6 +106,13 @@ message ListRequest {
string imageName = 1;
}
message TagRequest {
// image refers to the image to be tagged
string image = 1;
// tag is the tag added to image
string tag = 2;
}
message ListResponse {
message ImageInfo {
string repository = 1;
......
......@@ -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 = ExpandTag(tag, b.localStore); err != nil {
return err
}
}
if additionalTag != "" {
if b.buildOpts.AdditionalTag, err = expandTag(additionalTag, b.localStore); err != nil {
if b.buildOpts.AdditionalTag, err = ExpandTag(additionalTag, b.localStore); err != nil {
return err
}
}
......@@ -570,9 +570,9 @@ func parseOutputTag(output string) string {
return tag
}
// expandTag resolves tag name, if it not include a domain, "localhost" will be
// 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) {
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)
......
......@@ -86,6 +86,7 @@ func NewContainerImageBuildCmd() *cobra.Command {
NewRemoveCmd(),
NewLoadCmd(),
NewImportCmd(),
NewTagCmd(),
)
return ctrImgBuildCmd
......
......@@ -19,7 +19,7 @@ import (
"testing"
"time"
types "github.com/gogo/protobuf/types"
"github.com/gogo/protobuf/types"
"google.golang.org/grpc"
pb "isula.org/isula-build/api/services"
......@@ -86,6 +86,10 @@ func (gcli *mockGrpcClient) Version(ctx context.Context, in *types.Empty, opts .
}, nil
}
func (gcli *mockGrpcClient) Tag(ctx context.Context, in *pb.TagRequest, opts ...grpc.CallOption) (*types.Empty, error) {
return &types.Empty{}, nil
}
func (gcli *mockGrpcClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (pb.Control_StatusClient, error) {
if gcli.statusFunc != nil {
return gcli.statusFunc(ctx, in, opts...)
......
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Danni Xia
// Create: 2020-07-20
// Description: This file is used for tag command
package main
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
pb "isula.org/isula-build/api/services"
)
const (
tagExample = `isula-build ctr-img tag <imageID> busybox:latest
isula-build ctr-img tag <imageName> busybox:latest`
)
// NewTagCmd returns tag command
func NewTagCmd() *cobra.Command {
// tagCmd represents the "tag" command
tagCmd := &cobra.Command{
Use: "tag",
Short: "create a tag for source image",
RunE: tagCommand,
Example: tagExample,
}
return tagCmd
}
func tagCommand(cmd *cobra.Command, args []string) error {
const validTagArgsLen = 2
if len(args) != validTagArgsLen {
return errors.New("invalid args for tag command")
}
ctx := context.Background()
cli, err := NewClient(ctx)
if err != nil {
return err
}
return runTag(ctx, cli, args)
}
func runTag(ctx context.Context, cli Cli, args []string) error {
_, err := cli.Client().Tag(ctx, &pb.TagRequest{
Image: args[0],
Tag: args[1],
})
if err != nil {
return err
}
return nil
}
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Danni Xia
// Create: 2020-07-20
// Description: This file is used for testing command tag
package main
import (
"context"
"testing"
"gotest.tools/assert"
)
func TestTagCommand(t *testing.T) {
tagCmd := NewTagCmd()
args := []string{"abc", "abc"}
err := tagCommand(tagCmd, args)
assert.ErrorContains(t, err, "isula_build.sock")
args = []string{"abc", "abc", "abc"}
err = tagCommand(tagCmd, args)
assert.ErrorContains(t, err, "invalid args for tag command")
args = []string{"abc"}
err = tagCommand(tagCmd, args)
assert.ErrorContains(t, err, "invalid args for tag command")
}
func TestRuntag(t *testing.T) {
ctx := context.Background()
cli := newMockClient(&mockGrpcClient{})
args := []string{"abc", "abc"}
err := runTag(ctx, &cli, args)
assert.NilError(t, err)
}
// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
// isula-build licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
// Author: Danni Xia
// Create: 2020-07-20
// Description: This file is "tag" command for backend
package daemon
import (
"context"
gogotypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
pb "isula.org/isula-build/api/services"
"isula.org/isula-build/builder/dockerfile"
"isula.org/isula-build/image"
)
// Tag adds an additional tag to an image
func (b *Backend) Tag(ctx context.Context, req *pb.TagRequest) (*gogotypes.Empty, error) {
logrus.WithFields(logrus.Fields{
"Image": req.GetImage(),
"Tag": req.GetTag(),
}).Info("TagRequest received")
var emptyResp = &gogotypes.Empty{}
s := b.daemon.localStore
_, img, err := image.FindImage(s, req.Image)
if err != nil {
return emptyResp, errors.Wrapf(err, "find local image %v error", req.Image)
}
imageName, err := dockerfile.ExpandTag(req.Tag, s)
if err != nil {
return emptyResp, err
}
if err := s.SetNames(img.ID, append(img.Names, imageName)); err != nil {
return emptyResp, errors.Wrapf(err, "set name %v to image %q error", req.Tag, req.Image)
}
return emptyResp, nil
}
......@@ -21,6 +21,7 @@
* [Authentication of the Remote Image Repository](#authentication-of-the-remote-image-repository)
* [-a, --all](#-a---all-1)
* [Version query](#version-query)
* [Tag an image](#tag-an-image)
<!-- vim-markdown-toc -->
# Usage
......@@ -340,3 +341,29 @@ Server:
Built: Thu Jun 11 19:02:45 2020
OS/Arch: linux/amd64
```
### Tag an image
We can use the `tag` command to add an additional tag to an image.
Usage:
`isula-build ctr-img tag <imageID>/<imageName> busybox:latest`
```bash
$ sudo isula-build ctr-img images
---------------------------------------------- ----------- ----------------- -------------------------- ------------
REPOSITORY TAG IMAGE ID CREATED SIZE
---------------------------------------------- ----------- ----------------- -------------------------- ------------
docker.io/library/alpine latest a24bb4013296 2020-05-29 21:19:46 5.85 MB
---------------------------------------------- ----------- ----------------- -------------------------- ------------
$ sudo isula-build ctr-img tag a24bb4013296 alpine:latest
$ sudo isula-build ctr-img images
---------------------------------------------- ----------- ----------------- -------------------------- ------------
REPOSITORY TAG IMAGE ID CREATED SIZE
---------------------------------------------- ----------- ----------------- -------------------------- ------------
docker.io/library/alpine latest a24bb4013296 2020-05-29 21:19:46 5.85 MB
localhost/alpine latest a24bb4013296 2020-05-29 21:19:46 5.85 MB
---------------------------------------------- ----------- ----------------- -------------------------- ------------
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册