diff --git a/engine/virt/helper.go b/engine/virt/helper.go index e2c1118d4..e93cc6a99 100644 --- a/engine/virt/helper.go +++ b/engine/virt/helper.go @@ -37,3 +37,29 @@ func (v *Virt) parseVolumes(volumes []string) (map[string]int64, error) { return vols, nil } + +const sep = "@" + +func splitUserImage(combined string) (user, imageName string, err error) { + inputErr := fmt.Errorf("input: \"%s\" not valid", combined) + if len(combined) < 1 { + return "", "", inputErr + } + + un := strings.Split(combined, sep) + switch len(un) { + case 1: + return "", combined, nil + case 2: + if len(un[0]) < 1 || len(un[1]) < 1 { + return "", "", inputErr + } + return un[0], un[1], nil + default: + return "", "", inputErr + } +} + +func combineUserImage(user, imageName string) string { + return fmt.Sprintf("%s%s%s", user, sep, imageName) +} diff --git a/engine/virt/image.go b/engine/virt/image.go index c3ebe079b..75d197861 100644 --- a/engine/virt/image.go +++ b/engine/virt/image.go @@ -4,9 +4,7 @@ import ( "bytes" "context" "encoding/json" - "errors" "io" - "strings" virttypes "github.com/projecteru2/libyavirt/types" @@ -22,9 +20,13 @@ func (v *Virt) ImageList(ctx context.Context, image string) (imgs []*enginetypes } // ImageRemove removes a specific image. -func (v *Virt) ImageRemove(ctx context.Context, image string, force, prune bool) (names []string, err error) { - log.Warnf(ctx, "ImageRemove does not implement") - return +func (v *Virt) ImageRemove(ctx context.Context, tag string, force, prune bool) (names []string, err error) { + user, imgName, err := splitUserImage(tag) + if err != nil { + return nil, err + } + + return v.client.RemoveImage(ctx, imgName, user, force, prune) } // ImagesPrune prunes one. @@ -40,13 +42,11 @@ func (v *Virt) ImagePull(ctx context.Context, ref string, all bool) (rc io.ReadC // ImagePush pushes to central image registry. func (v *Virt) ImagePush(ctx context.Context, ref string) (rc io.ReadCloser, err error) { - un := strings.Split(ref, "\n") - if len(un) != 2 { - return nil, errors.New("ref incorrect " + ref) + user, imgName, err := splitUserImage(ref) + if err != nil { + return nil, err } - user := un[0] - imgName := un[1] msg, err := v.client.PushImage(ctx, imgName, user) if err != nil { return nil, err @@ -78,13 +78,12 @@ func (v *Virt) ImageBuildFromExist(ctx context.Context, ID string, refs []string return "", types.ErrBadRefs } - un := strings.Split(refs[0], "\n") - if len(un) != 2 { - return "", errors.New("ref incorrect " + refs[0]) + _, imgName, err := splitUserImage(refs[0]) + if err != nil { + return "", err } - name := un[1] - req := virttypes.CaptureGuestReq{Name: name, User: user} + req := virttypes.CaptureGuestReq{Name: imgName, User: user} req.ID = ID uimg, err := v.client.CaptureGuest(ctx, req) @@ -97,7 +96,7 @@ func (v *Virt) ImageBuildFromExist(ctx context.Context, ID string, refs []string // ImageBuildCachePrune prunes cached one. func (v *Virt) ImageBuildCachePrune(ctx context.Context, all bool) (reclaimed uint64, err error) { - log.Warnf(ctx, "ImageBuildCachePrune does not implement") + log.Warnf(ctx, "ImageBuildCachePrune does not implement and not required by vm") return } diff --git a/engine/virt/virt.go b/engine/virt/virt.go index c81f43ec5..c074e56df 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -158,7 +158,7 @@ func (v *Virt) NetworkList(ctx context.Context, drivers []string) (nets []*engin // BuildRefs builds references. func (v *Virt) BuildRefs(ctx context.Context, opts *enginetypes.BuildRefOptions) (refs []string) { - return []string{fmt.Sprintf("%s\n%s", opts.User, opts.Name)} + return []string{combineUserImage(opts.User, opts.Name)} } // BuildContent builds content, the use of it is similar to BuildRefs. diff --git a/go.mod b/go.mod index 3416bd61e..4d75ca4ff 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/opencontainers/runc v1.0.0-rc95 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 - github.com/projecteru2/libyavirt v0.0.0-20211130015549-c11a6e4758dd + github.com/projecteru2/libyavirt v0.0.0-20211202092239-8539e8218458 github.com/prometheus/client_golang v1.11.0 github.com/sanity-io/litter v1.5.1 github.com/sirupsen/logrus v1.7.0 diff --git a/go.sum b/go.sum index 315ca0f50..7dc0b921c 100644 --- a/go.sum +++ b/go.sum @@ -430,8 +430,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= -github.com/projecteru2/libyavirt v0.0.0-20211130015549-c11a6e4758dd h1:ZXeAZTO6tk3MR8kiYju9sIOxQ79GAVoU5BdfkimPHMs= -github.com/projecteru2/libyavirt v0.0.0-20211130015549-c11a6e4758dd/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE= +github.com/projecteru2/libyavirt v0.0.0-20211202092239-8539e8218458 h1:/vzYKTEuiRyb2izwt/ZiafWsrcBHCLDbqbJBkHWUKOQ= +github.com/projecteru2/libyavirt v0.0.0-20211202092239-8539e8218458/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=