github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/services/leases/local.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 leases
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/containerd/containerd/gc"
    23  	"github.com/containerd/containerd/leases"
    24  	"github.com/containerd/containerd/metadata"
    25  	"github.com/containerd/containerd/plugin"
    26  	"github.com/containerd/containerd/services"
    27  )
    28  
    29  func init() {
    30  	plugin.Register(&plugin.Registration{
    31  		Type: plugin.ServicePlugin,
    32  		ID:   services.LeasesService,
    33  		Requires: []plugin.Type{
    34  			plugin.MetadataPlugin,
    35  		},
    36  		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
    37  			m, err := ic.Get(plugin.MetadataPlugin)
    38  			if err != nil {
    39  				return nil, err
    40  			}
    41  			g, err := ic.Get(plugin.GCPlugin)
    42  			if err != nil {
    43  				return nil, err
    44  			}
    45  			return &local{
    46  				Manager: metadata.NewLeaseManager(m.(*metadata.DB)),
    47  				gc:      g.(gcScheduler),
    48  			}, nil
    49  		},
    50  	})
    51  }
    52  
    53  type gcScheduler interface {
    54  	ScheduleAndWait(context.Context) (gc.Stats, error)
    55  }
    56  
    57  type local struct {
    58  	leases.Manager
    59  	gc gcScheduler
    60  }
    61  
    62  func (l *local) Delete(ctx context.Context, lease leases.Lease, opts ...leases.DeleteOpt) error {
    63  	var do leases.DeleteOptions
    64  	for _, opt := range opts {
    65  		if err := opt(ctx, &do); err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	if err := l.Manager.Delete(ctx, lease); err != nil {
    71  		return err
    72  	}
    73  
    74  	if do.Synchronous {
    75  		if _, err := l.gc.ScheduleAndWait(ctx); err != nil {
    76  			return err
    77  		}
    78  	}
    79  
    80  	return nil
    81  
    82  }