Skip to content

Commit

Permalink
Expose image 'DeleteTag' method in the 'daemon'
Browse files Browse the repository at this point in the history
The method is needed to enable image cleanup in the container-structure-tests.

The problem I'm trying to solve is that container-structure-tests are writing an image to docker
https://github.com/GoogleContainerTools/container-structure-test/blob/c35e48dcd5dd[…]ec6d50d688235d4a36/cmd/container-structure-test/app/cmd/test.go
but they are not deleting it at all. This leads to leaked registry.structure_test.oci.local/image that accumulate in CICD.
  • Loading branch information
sfc-gh-ptabor committed Oct 11, 2024
1 parent 808e354 commit ef75fbc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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)
}
16 changes: 14 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,15 @@ func Write(tag name.Tag, img v1.Image, options ...Option) (string, error) {
}
return response, nil
}

func RemoveByTag(tag name.Tag, options ...Option) error {
o, err := makeOptions(options...)
if err != nil {
return err
}
_, err = o.client.ImageRemove(o.ctx, tag.Name(), types.ImageRemoveOptions{PruneChildren: true, Force: false})
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 := RemoveByTag(tag, WithContext(ctx)); err != nil {
t.Fatal(err)
}
}

0 comments on commit ef75fbc

Please sign in to comment.