From 3f1a61f76afdf481bd1790569c6f0354d42721cd Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 13 Nov 2017 16:09:29 -0800 Subject: [PATCH] Add synchronous image delete Synchronous image delete provides an option image delete to wait until the next garbage collection deletes after an image is removed before returning success to the caller. Signed-off-by: Derek McGowan --- api/next.pb.txt | 7 ++ api/services/images/v1/images.pb.go | 122 ++++++++++++++++++---------- api/services/images/v1/images.proto | 4 + cmd/containerd/builtins.go | 2 +- cmd/ctr/commands/images/images.go | 16 +++- image_store.go | 9 +- images/image.go | 19 ++++- metadata/images.go | 2 +- services/images/service.go | 24 +++++- 9 files changed, 154 insertions(+), 51 deletions(-) diff --git a/api/next.pb.txt b/api/next.pb.txt index 369d947ac..6343a3de5 100755 --- a/api/next.pb.txt +++ b/api/next.pb.txt @@ -2193,6 +2193,13 @@ file { type: TYPE_STRING json_name: "name" } + field { + name: "sync" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_BOOL + json_name: "sync" + } } service { name: "Images" diff --git a/api/services/images/v1/images.pb.go b/api/services/images/v1/images.pb.go index 4c9f0f982..8bbfcc8ba 100644 --- a/api/services/images/v1/images.pb.go +++ b/api/services/images/v1/images.pb.go @@ -163,6 +163,9 @@ func (*ListImagesResponse) Descriptor() ([]byte, []int) { return fileDescriptorI type DeleteImageRequest struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + // Sync indicates that the delete and cleanup should be done + // synchronously before returning to the caller + Sync bool `protobuf:"varint,2,opt,name=sync,proto3" json:"sync,omitempty"` } func (m *DeleteImageRequest) Reset() { *m = DeleteImageRequest{} } @@ -717,6 +720,16 @@ func (m *DeleteImageRequest) MarshalTo(dAtA []byte) (int, error) { i = encodeVarintImages(dAtA, i, uint64(len(m.Name))) i += copy(dAtA[i:], m.Name) } + if m.Sync { + dAtA[i] = 0x10 + i++ + if m.Sync { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i++ + } return i, nil } @@ -840,6 +853,9 @@ func (m *DeleteImageRequest) Size() (n int) { if l > 0 { n += 1 + l + sovImages(uint64(l)) } + if m.Sync { + n += 2 + } return n } @@ -967,6 +983,7 @@ func (this *DeleteImageRequest) String() string { } s := strings.Join([]string{`&DeleteImageRequest{`, `Name:` + fmt.Sprintf("%v", this.Name) + `,`, + `Sync:` + fmt.Sprintf("%v", this.Sync) + `,`, `}`, }, "") return s @@ -1999,6 +2016,26 @@ func (m *DeleteImageRequest) Unmarshal(dAtA []byte) error { } m.Name = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Sync", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowImages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + m.Sync = bool(v != 0) default: iNdEx = preIndex skippy, err := skipImages(dAtA[iNdEx:]) @@ -2130,46 +2167,47 @@ func init() { } var fileDescriptorImages = []byte{ - // 650 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x55, 0x4f, 0x6f, 0xd3, 0x4e, - 0x10, 0x8d, 0x93, 0xd4, 0x6d, 0x27, 0x87, 0x5f, 0x7f, 0x4b, 0x85, 0x2c, 0x03, 0x69, 0x14, 0x81, - 0x94, 0x0b, 0x6b, 0x1a, 0x2e, 0xd0, 0x4a, 0x88, 0xa6, 0x2d, 0x05, 0xa9, 0x70, 0x30, 0xff, 0x2a, - 0x2e, 0xd5, 0x26, 0x99, 0x18, 0x2b, 0x76, 0x6c, 0xbc, 0x9b, 0x48, 0xb9, 0xf1, 0x11, 0x90, 0xe0, - 0x43, 0xf5, 0xc8, 0x91, 0x13, 0xd0, 0x1c, 0xf8, 0x1c, 0xc8, 0xbb, 0x1b, 0x9a, 0x26, 0x11,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0xed, 0xdd, 0x56, 0xd1, 0x51, 0xae, 0x69, 0x4a, 0xad, 0xfb, 0xbf, 0x03, 0x00, 0x00, - 0xff, 0xff, 0x86, 0xe6, 0x32, 0x0a, 0xc6, 0x07, 0x00, 0x00, + // 659 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x55, 0xcd, 0x6e, 0xd3, 0x40, + 0x10, 0x8e, 0x93, 0xd4, 0x6d, 0x27, 0x07, 0xca, 0x52, 0x21, 0xcb, 0x40, 0x1a, 0x45, 0x20, 0xe5, + 0xc2, 0x9a, 0x86, 0x0b, 0xb4, 0x08, 0xd1, 0xb4, 0xa5, 0x20, 0x15, 0x0e, 0xe6, 0xaf, 0xe2, 0x52, + 0x6d, 0x92, 0x89, 0xb1, 0x62, 0xc7, 0xc6, 0xbb, 0x89, 0x94, 0x1b, 0x8f, 0x80, 0x04, 0x0f, 0xd5, + 0x23, 0x47, 0x4e, 0x40, 0x73, 0xe0, 0x39, 0x90, 0x77, 0x37, 0x34, 0x4d, 0x22, 0x92, 0x94, 0xde, + 0x66, 0xed, 0xef, 0x9b, 0x9f, 0x6f, 0x66, 0x76, 0x61, 0xcf, 0xf3, 0xc5, 0x87, 0x6e, 0x9d, 0x36, + 0xa2, 0xd0, 0x69, 0x44, 0x1d, 0xc1, 0xfc, 0x0e, 0x26, 0xcd, 0x51, 0x93, 0xc5, 0xbe, 0xc3, 0x31, + 0xe9, 0xf9, 0x0d, 0xe4, 0x8e, 0x1f, 0x32, 0x0f, 0xb9, 0xd3, 0xdb, 0xd4, 0x16, 0x8d, 0x93, 0x48, + 0x44, 0xe4, 0xd6, 0x19, 0x9e, 0x0e, 0xb1, 0x54, 0x23, 0x7a, 0x9b, 0xf6, 0xba, 0x17, 0x79, 0x91, + 0x44, 0x3a, 0xa9, 0xa5, 0x48, 0xf6, 0x0d, 0x2f, 0x8a, 0xbc, 0x00, 0x1d, 0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0x48, 0x05, 0x57, 0x5c, 0x69, 0x57, 0xbf, 0xe6, + 0xc1, 0x54, 0x49, 0x91, 0x16, 0xe4, 0x0e, 0x50, 0x10, 0x3a, 0x23, 0x87, 0xb1, 0x65, 0xb0, 0x9d, + 0xb9, 0xf1, 0xba, 0xe8, 0x36, 0xe4, 0x53, 0x29, 0xc8, 0xac, 0x3b, 0x69, 0x42, 0x5e, 0x7b, 0x73, + 0x01, 0x86, 0x0e, 0x16, 0x81, 0xa9, 0xc6, 0x9d, 0xcc, 0x22, 0x4f, 0x6e, 0x9b, 0x5d, 0x5d, 0x84, + 0x72, 0x16, 0x50, 0x0d, 0xdc, 0xcc, 0x80, 0x93, 0xcb, 0x32, 0x33, 0xe0, 0xb4, 0x51, 0x7e, 0x05, + 0xa6, 0xea, 0xff, 0xcc, 0x80, 0x93, 0x63, 0x62, 0x5f, 0x9f, 0x58, 0xa3, 0xfd, 0xf4, 0x8d, 0xab, + 0x1d, 0x9d, 0x9c, 0x16, 0x33, 0xdf, 0x4f, 0x8b, 0x99, 0x4f, 0x83, 0xa2, 0x71, 0x32, 0x28, 0x1a, + 0xdf, 0x06, 0x45, 0xe3, 0xd7, 0xa0, 0x68, 0xbc, 0x7f, 0x7c, 0xc1, 0xf7, 0x78, 0x5b, 0x59, 0x47, + 0x99, 0xba, 0x29, 0x63, 0xdd, 0xff, 0x13, 0x00, 0x00, 0xff, 0xff, 0x24, 0x4e, 0xca, 0x64, 0xda, + 0x07, 0x00, 0x00, } diff --git a/api/services/images/v1/images.proto b/api/services/images/v1/images.proto index 9e6444c27..3d013abfc 100644 --- a/api/services/images/v1/images.proto +++ b/api/services/images/v1/images.proto @@ -115,4 +115,8 @@ message ListImagesResponse { message DeleteImageRequest { string name = 1; + + // Sync indicates that the delete and cleanup should be done + // synchronously before returning to the caller + bool sync = 2; } diff --git a/cmd/containerd/builtins.go b/cmd/containerd/builtins.go index 78c477d2d..1a8268ada 100644 --- a/cmd/containerd/builtins.go +++ b/cmd/containerd/builtins.go @@ -3,7 +3,7 @@ package main // register containerd builtins here import ( _ "github.com/containerd/containerd/diff/walking" - _ "github.com/containerd/containerd/gc/policy" + _ "github.com/containerd/containerd/gc/scheduler" _ "github.com/containerd/containerd/services/containers" _ "github.com/containerd/containerd/services/content" _ "github.com/containerd/containerd/services/diff" diff --git a/cmd/ctr/commands/images/images.go b/cmd/ctr/commands/images/images.go index 86a3649ee..958c25ef5 100644 --- a/cmd/ctr/commands/images/images.go +++ b/cmd/ctr/commands/images/images.go @@ -270,8 +270,14 @@ var removeCommand = cli.Command{ Name: "remove", Aliases: []string{"rm"}, Usage: "remove one or more images by reference", - ArgsUsage: " [, ...]", + ArgsUsage: "[flags] [, ...]", Description: "remove one or more images by reference", + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "sync", + Usage: "Synchronously remove image and all associated resources", + }, + }, Action: func(context *cli.Context) error { client, ctx, cancel, err := commands.NewClient(context) if err != nil { @@ -282,8 +288,12 @@ var removeCommand = cli.Command{ exitErr error imageStore = client.ImageService() ) - for _, target := range context.Args() { - if err := imageStore.Delete(ctx, target); err != nil { + for i, target := range context.Args() { + var opts []images.DeleteOpt + if context.Bool("sync") && i == context.NArg()-1 { + opts = append(opts, images.SynchronousDelete()) + } + if err := imageStore.Delete(ctx, target, opts...); err != nil { if !errdefs.IsNotFound(err) { if exitErr == nil { exitErr = errors.Wrapf(err, "unable to delete %v", target) diff --git a/image_store.go b/image_store.go index daa18f7f5..9a3aafc84 100644 --- a/image_store.go +++ b/image_store.go @@ -74,9 +74,16 @@ func (s *remoteImages) Update(ctx context.Context, image images.Image, fieldpath return imageFromProto(&updated.Image), nil } -func (s *remoteImages) Delete(ctx context.Context, name string) error { +func (s *remoteImages) Delete(ctx context.Context, name string, opts ...images.DeleteOpt) error { + var do images.DeleteOptions + for _, opt := range opts { + if err := opt(ctx, &do); err != nil { + return err + } + } _, err := s.client.Delete(ctx, &imagesapi.DeleteImageRequest{ Name: name, + Sync: do.Synchronous, }) return errdefs.FromGRPC(err) diff --git a/images/image.go b/images/image.go index 4c78c6cc2..e0d6990c4 100644 --- a/images/image.go +++ b/images/image.go @@ -38,6 +38,23 @@ type Image struct { CreatedAt, UpdatedAt time.Time } +// DeleteOptions provide options on image delete +type DeleteOptions struct { + Synchronous bool +} + +// DeleteOpt allows configuring a delete operation +type DeleteOpt func(context.Context, *DeleteOptions) error + +// SynchronousDelete is used to indicate that an image deletion and removal of +// the image resources should occur synchronously before returning a result. +func SynchronousDelete() DeleteOpt { + return func(ctx context.Context, o *DeleteOptions) error { + o.Synchronous = true + return nil + } +} + // Store and interact with images type Store interface { Get(ctx context.Context, name string) (Image, error) @@ -48,7 +65,7 @@ type Store interface { // one or more fieldpaths are provided, only those fields will be updated. Update(ctx context.Context, image Image, fieldpaths ...string) (Image, error) - Delete(ctx context.Context, name string) error + Delete(ctx context.Context, name string, opts ...DeleteOpt) error } // TODO(stevvooe): Many of these functions make strong platform assumptions, diff --git a/metadata/images.go b/metadata/images.go index 7e5e3c76e..32f7d4e43 100644 --- a/metadata/images.go +++ b/metadata/images.go @@ -187,7 +187,7 @@ func (s *imageStore) Update(ctx context.Context, image images.Image, fieldpaths }) } -func (s *imageStore) Delete(ctx context.Context, name string) error { +func (s *imageStore) Delete(ctx context.Context, name string, opts ...images.DeleteOpt) error { namespace, err := namespaces.NamespaceRequired(ctx) if err != nil { return err diff --git a/services/images/service.go b/services/images/service.go index 4e52d51de..ad90d6abe 100644 --- a/services/images/service.go +++ b/services/images/service.go @@ -1,6 +1,8 @@ package images import ( + gocontext "context" + "github.com/boltdb/bolt" eventstypes "github.com/containerd/containerd/api/events" imagesapi "github.com/containerd/containerd/api/services/images/v1" @@ -22,26 +24,38 @@ func init() { ID: "images", Requires: []plugin.Type{ plugin.MetadataPlugin, + plugin.GCPlugin, }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { m, err := ic.Get(plugin.MetadataPlugin) if err != nil { return nil, err } - return NewService(m.(*metadata.DB), ic.Events), nil + g, err := ic.Get(plugin.GCPlugin) + if err != nil { + return nil, err + } + + return NewService(m.(*metadata.DB), g.(gcScheduler), ic.Events), nil }, }) } +type gcScheduler interface { + ScheduleAndWait(gocontext.Context) (metadata.GCStats, error) +} + type service struct { db *metadata.DB + gc gcScheduler publisher events.Publisher } // NewService returns the GRPC image server -func NewService(db *metadata.DB, publisher events.Publisher) imagesapi.ImagesServer { +func NewService(db *metadata.DB, gc gcScheduler, publisher events.Publisher) imagesapi.ImagesServer { return &service{ db: db, + gc: gc, publisher: publisher, } } @@ -162,6 +176,12 @@ func (s *service) Delete(ctx context.Context, req *imagesapi.DeleteImageRequest) return nil, err } + if req.Sync { + if _, err := s.gc.ScheduleAndWait(ctx); err != nil { + return nil, err + } + } + return &ptypes.Empty{}, nil }