github.com/demonoid81/containerd@v1.3.4/leases/lease.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  	"time"
    22  )
    23  
    24  // Opt is used to set options on a lease
    25  type Opt func(*Lease) error
    26  
    27  // DeleteOpt allows configuring a delete operation
    28  type DeleteOpt func(context.Context, *DeleteOptions) error
    29  
    30  // Manager is used to create, list, and remove leases
    31  type Manager interface {
    32  	Create(context.Context, ...Opt) (Lease, error)
    33  	Delete(context.Context, Lease, ...DeleteOpt) error
    34  	List(context.Context, ...string) ([]Lease, error)
    35  	AddResource(context.Context, Lease, Resource) error
    36  	DeleteResource(context.Context, Lease, Resource) error
    37  	ListResources(context.Context, Lease) ([]Resource, error)
    38  }
    39  
    40  // Lease retains resources to prevent cleanup before
    41  // the resources can be fully referenced.
    42  type Lease struct {
    43  	ID        string
    44  	CreatedAt time.Time
    45  	Labels    map[string]string
    46  }
    47  
    48  // Resource represents low level resource of image, like content, ingest and
    49  // snapshotter.
    50  type Resource struct {
    51  	ID   string
    52  	Type string
    53  }
    54  
    55  // DeleteOptions provide options on image delete
    56  type DeleteOptions struct {
    57  	Synchronous bool
    58  }
    59  
    60  // SynchronousDelete is used to indicate that a lease deletion and removal of
    61  // any unreferenced resources should occur synchronously before returning the
    62  // result.
    63  func SynchronousDelete(ctx context.Context, o *DeleteOptions) error {
    64  	o.Synchronous = true
    65  	return nil
    66  }
    67  
    68  // WithLabels sets labels on a lease
    69  func WithLabels(labels map[string]string) Opt {
    70  	return func(l *Lease) error {
    71  		l.Labels = labels
    72  		return nil
    73  	}
    74  }
    75  
    76  // WithExpiration sets an expiration on the lease
    77  func WithExpiration(d time.Duration) Opt {
    78  	return func(l *Lease) error {
    79  		if l.Labels == nil {
    80  			l.Labels = map[string]string{}
    81  		}
    82  		l.Labels["containerd.io/gc.expire"] = time.Now().Add(d).Format(time.RFC3339)
    83  
    84  		return nil
    85  	}
    86  }