github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/diff/diff.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 diff
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/containerd/containerd/mount"
    23  	"github.com/gogo/protobuf/types"
    24  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  // Config is used to hold parameters needed for a diff operation
    28  type Config struct {
    29  	// MediaType is the type of diff to generate
    30  	// Default depends on the differ,
    31  	// i.e. application/vnd.oci.image.layer.v1.tar+gzip
    32  	MediaType string
    33  
    34  	// Reference is the content upload reference
    35  	// Default will use a random reference string
    36  	Reference string
    37  
    38  	// Labels are the labels to apply to the generated content
    39  	Labels map[string]string
    40  }
    41  
    42  // Opt is used to configure a diff operation
    43  type Opt func(*Config) error
    44  
    45  // Comparer allows creation of filesystem diffs between mounts
    46  type Comparer interface {
    47  	// Compare computes the difference between two mounts and returns a
    48  	// descriptor for the computed diff. The options can provide
    49  	// a ref which can be used to track the content creation of the diff.
    50  	// The media type which is used to determine the format of the created
    51  	// content can also be provided as an option.
    52  	Compare(ctx context.Context, lower, upper []mount.Mount, opts ...Opt) (ocispec.Descriptor, error)
    53  }
    54  
    55  // ApplyConfig is used to hold parameters needed for a apply operation
    56  type ApplyConfig struct {
    57  	// ProcessorPayloads specifies the payload sent to various processors
    58  	ProcessorPayloads map[string]*types.Any
    59  }
    60  
    61  // ApplyOpt is used to configure an Apply operation
    62  type ApplyOpt func(context.Context, ocispec.Descriptor, *ApplyConfig) error
    63  
    64  // Applier allows applying diffs between mounts
    65  type Applier interface {
    66  	// Apply applies the content referred to by the given descriptor to
    67  	// the provided mount. The method of applying is based on the
    68  	// implementation and content descriptor. For example, in the common
    69  	// case the descriptor is a file system difference in tar format,
    70  	// that tar would be applied on top of the mounts.
    71  	Apply(ctx context.Context, desc ocispec.Descriptor, mount []mount.Mount, opts ...ApplyOpt) (ocispec.Descriptor, error)
    72  }
    73  
    74  // WithMediaType sets the media type to use for creating the diff, without
    75  // specifying the differ will choose a default.
    76  func WithMediaType(m string) Opt {
    77  	return func(c *Config) error {
    78  		c.MediaType = m
    79  		return nil
    80  	}
    81  }
    82  
    83  // WithReference is used to set the content upload reference used by
    84  // the diff operation. This allows the caller to track the upload through
    85  // the content store.
    86  func WithReference(ref string) Opt {
    87  	return func(c *Config) error {
    88  		c.Reference = ref
    89  		return nil
    90  	}
    91  }
    92  
    93  // WithLabels is used to set content labels on the created diff content.
    94  func WithLabels(labels map[string]string) Opt {
    95  	return func(c *Config) error {
    96  		c.Labels = labels
    97  		return nil
    98  	}
    99  }
   100  
   101  // WithPayloads sets the apply processor payloads to the config
   102  func WithPayloads(payloads map[string]*types.Any) ApplyOpt {
   103  	return func(_ context.Context, _ ocispec.Descriptor, c *ApplyConfig) error {
   104  		c.ProcessorPayloads = payloads
   105  		return nil
   106  	}
   107  }