Skip to content

Commit

Permalink
implement ImageRemove (#510)
Browse files Browse the repository at this point in the history
add helper for tag
  • Loading branch information
Aceralon authored Dec 8, 2021
1 parent 4315e4b commit dae0e91
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
26 changes: 26 additions & 0 deletions engine/virt/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 15 additions & 16 deletions engine/virt/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"strings"

virttypes "github.com/projecteru2/libyavirt/types"

Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit dae0e91

Please sign in to comment.