github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/services/snapshots/snapshotters.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 snapshots
    18  
    19  import (
    20  	"context"
    21  
    22  	eventstypes "github.com/containerd/containerd/api/events"
    23  	"github.com/containerd/containerd/events"
    24  	"github.com/containerd/containerd/metadata"
    25  	"github.com/containerd/containerd/mount"
    26  	"github.com/containerd/containerd/plugin"
    27  	"github.com/containerd/containerd/services"
    28  	"github.com/containerd/containerd/snapshots"
    29  )
    30  
    31  // snapshotter wraps snapshots.Snapshotter with proper events published.
    32  type snapshotter struct {
    33  	snapshots.Snapshotter
    34  	publisher events.Publisher
    35  }
    36  
    37  func init() {
    38  	plugin.Register(&plugin.Registration{
    39  		Type: plugin.ServicePlugin,
    40  		ID:   services.SnapshotsService,
    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  			db := m.(*metadata.DB)
    51  			ss := make(map[string]snapshots.Snapshotter)
    52  			for n, sn := range db.Snapshotters() {
    53  				ss[n] = newSnapshotter(sn, ic.Events)
    54  			}
    55  			return ss, nil
    56  		},
    57  	})
    58  }
    59  
    60  func newSnapshotter(sn snapshots.Snapshotter, publisher events.Publisher) snapshots.Snapshotter {
    61  	return &snapshotter{
    62  		Snapshotter: sn,
    63  		publisher:   publisher,
    64  	}
    65  }
    66  
    67  func (s *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
    68  	mounts, err := s.Snapshotter.Prepare(ctx, key, parent, opts...)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	if err := s.publisher.Publish(ctx, "/snapshot/prepare", &eventstypes.SnapshotPrepare{
    73  		Key:    key,
    74  		Parent: parent,
    75  	}); err != nil {
    76  		return nil, err
    77  	}
    78  	return mounts, nil
    79  }
    80  
    81  func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
    82  	if err := s.Snapshotter.Commit(ctx, name, key, opts...); err != nil {
    83  		return err
    84  	}
    85  	return s.publisher.Publish(ctx, "/snapshot/commit", &eventstypes.SnapshotCommit{
    86  		Key:  key,
    87  		Name: name,
    88  	})
    89  }
    90  
    91  func (s *snapshotter) Remove(ctx context.Context, key string) error {
    92  	if err := s.Snapshotter.Remove(ctx, key); err != nil {
    93  		return err
    94  	}
    95  	return s.publisher.Publish(ctx, "/snapshot/remove", &eventstypes.SnapshotRemove{
    96  		Key: key,
    97  	})
    98  }