Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose image 'DeleteTag' method in the 'daemon' #2020

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/v1/daemon/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type MockClient struct {
inspectBody []byte

tagErr error

imageRemoveErr error
}

func (m *MockClient) NegotiateAPIVersion(_ context.Context) {
Expand Down
1 change: 1 addition & 0 deletions pkg/v1/daemon/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ type Client interface {
ImageTag(context.Context, string, string) error
ImageInspectWithRaw(context.Context, string) (types.ImageInspect, []byte, error)
ImageHistory(context.Context, string) ([]api.HistoryResponseItem, error)
ImageRemove(context.Context, string, types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
}
19 changes: 17 additions & 2 deletions pkg/v1/daemon/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package daemon

import (
"fmt"
"io"

"github.com/docker/docker/api/types"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"io"
)

// Tag adds a tag to an already existent image.
Expand Down Expand Up @@ -76,3 +76,18 @@ func Write(tag name.Tag, img v1.Image, options ...Option) (string, error) {
}
return response, nil
}

// DeleteTag - Deletes given tag:
// - force - delete the image even if it is being used by stopped containers or has other tags
// - pruneChildren - deletes untagged parent images
func DeleteTag(tag name.Tag, pruneChildren bool, force bool, options ...Option) error {
o, err := makeOptions(options...)
if err != nil {
return err
}
_, err = o.client.ImageRemove(o.ctx, tag.Name(), types.ImageRemoveOptions{PruneChildren: pruneChildren, Force: force})
if err != nil {
return fmt.Errorf("error deleting image %s: %w", tag.Name(), err)
}
return nil
}
7 changes: 7 additions & 0 deletions pkg/v1/daemon/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (m *MockClient) ImageTag(ctx context.Context, _, _ string) error {
return m.tagErr
}

func (m *MockClient) ImageRemove(context.Context, string, types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
return nil, m.imageRemoveErr
}

func TestWriteImage(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down Expand Up @@ -165,4 +169,7 @@ func TestWriteDefaultClient(t *testing.T) {
if _, err := Write(tag, empty.Image, WithContext(ctx)); err != nil {
t.Fatal(err)
}
if err := DeleteTag(tag, false, false, WithContext(ctx)); err != nil {
t.Fatal(err)
}
}
Loading