github.com/containerd/Containerd@v1.4.13/services/content/store.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package content
    18  
    19  import (
    20  	"context"
    21  
    22  	eventstypes "github.com/containerd/containerd/api/events"
    23  	"github.com/containerd/containerd/content"
    24  	"github.com/containerd/containerd/events"
    25  	"github.com/containerd/containerd/metadata"
    26  	"github.com/containerd/containerd/plugin"
    27  	"github.com/containerd/containerd/services"
    28  	digest "github.com/opencontainers/go-digest"
    29  )
    30  
    31  // store wraps content.Store with proper event published.
    32  type store struct {
    33  	content.Store
    34  	publisher events.Publisher
    35  }
    36  
    37  func init() {
    38  	plugin.Register(&plugin.Registration{
    39  		Type: plugin.ServicePlugin,
    40  		ID:   services.ContentService,
    41  		Requires: []plugin.Type{
    42  			plugin.MetadataPlugin,
    43  		},
    44  		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
    45  			m, err := ic.Get(plugin.MetadataPlugin)
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  
    50  			s, err := newContentStore(m.(*metadata.DB).ContentStore(), ic.Events)
    51  			return s, err
    52  		},
    53  	})
    54  }
    55  
    56  func newContentStore(cs content.Store, publisher events.Publisher) (content.Store, error) {
    57  	return &store{
    58  		Store:     cs,
    59  		publisher: publisher,
    60  	}, nil
    61  }
    62  
    63  func (s *store) Delete(ctx context.Context, dgst digest.Digest) error {
    64  	if err := s.Store.Delete(ctx, dgst); err != nil {
    65  		return err
    66  	}
    67  	// TODO: Consider whether we should return error here.
    68  	return s.publisher.Publish(ctx, "/content/delete", &eventstypes.ContentDelete{
    69  		Digest: dgst,
    70  	})
    71  }