github.com/containerd/Containerd@v1.4.13/client_opts.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 containerd
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/containerd/containerd/images"
    23  	"github.com/containerd/containerd/platforms"
    24  	"github.com/containerd/containerd/remotes"
    25  	"github.com/containerd/containerd/snapshots"
    26  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    27  
    28  	"google.golang.org/grpc"
    29  )
    30  
    31  type clientOpts struct {
    32  	defaultns       string
    33  	defaultRuntime  string
    34  	defaultPlatform platforms.MatchComparer
    35  	services        *services
    36  	dialOptions     []grpc.DialOption
    37  	timeout         time.Duration
    38  }
    39  
    40  // ClientOpt allows callers to set options on the containerd client
    41  type ClientOpt func(c *clientOpts) error
    42  
    43  // WithDefaultNamespace sets the default namespace on the client
    44  //
    45  // Any operation that does not have a namespace set on the context will
    46  // be provided the default namespace
    47  func WithDefaultNamespace(ns string) ClientOpt {
    48  	return func(c *clientOpts) error {
    49  		c.defaultns = ns
    50  		return nil
    51  	}
    52  }
    53  
    54  // WithDefaultRuntime sets the default runtime on the client
    55  func WithDefaultRuntime(rt string) ClientOpt {
    56  	return func(c *clientOpts) error {
    57  		c.defaultRuntime = rt
    58  		return nil
    59  	}
    60  }
    61  
    62  // WithDefaultPlatform sets the default platform matcher on the client
    63  func WithDefaultPlatform(platform platforms.MatchComparer) ClientOpt {
    64  	return func(c *clientOpts) error {
    65  		c.defaultPlatform = platform
    66  		return nil
    67  	}
    68  }
    69  
    70  // WithDialOpts allows grpc.DialOptions to be set on the connection
    71  func WithDialOpts(opts []grpc.DialOption) ClientOpt {
    72  	return func(c *clientOpts) error {
    73  		c.dialOptions = opts
    74  		return nil
    75  	}
    76  }
    77  
    78  // WithServices sets services used by the client.
    79  func WithServices(opts ...ServicesOpt) ClientOpt {
    80  	return func(c *clientOpts) error {
    81  		c.services = &services{}
    82  		for _, o := range opts {
    83  			o(c.services)
    84  		}
    85  		return nil
    86  	}
    87  }
    88  
    89  // WithTimeout sets the connection timeout for the client
    90  func WithTimeout(d time.Duration) ClientOpt {
    91  	return func(c *clientOpts) error {
    92  		c.timeout = d
    93  		return nil
    94  	}
    95  }
    96  
    97  // RemoteOpt allows the caller to set distribution options for a remote
    98  type RemoteOpt func(*Client, *RemoteContext) error
    99  
   100  // WithPlatform allows the caller to specify a platform to retrieve
   101  // content for
   102  func WithPlatform(platform string) RemoteOpt {
   103  	if platform == "" {
   104  		platform = platforms.DefaultString()
   105  	}
   106  	return func(_ *Client, c *RemoteContext) error {
   107  		for _, p := range c.Platforms {
   108  			if p == platform {
   109  				return nil
   110  			}
   111  		}
   112  
   113  		c.Platforms = append(c.Platforms, platform)
   114  		return nil
   115  	}
   116  }
   117  
   118  // WithPlatformMatcher specifies the matcher to use for
   119  // determining which platforms to pull content for.
   120  // This value supersedes anything set with `WithPlatform`.
   121  func WithPlatformMatcher(m platforms.MatchComparer) RemoteOpt {
   122  	return func(_ *Client, c *RemoteContext) error {
   123  		c.PlatformMatcher = m
   124  		return nil
   125  	}
   126  }
   127  
   128  // WithPullUnpack is used to unpack an image after pull. This
   129  // uses the snapshotter, content store, and diff service
   130  // configured for the client.
   131  func WithPullUnpack(_ *Client, c *RemoteContext) error {
   132  	c.Unpack = true
   133  	return nil
   134  }
   135  
   136  // WithUnpackOpts is used to add unpack options to the unpacker.
   137  func WithUnpackOpts(opts []UnpackOpt) RemoteOpt {
   138  	return func(_ *Client, c *RemoteContext) error {
   139  		c.UnpackOpts = append(c.UnpackOpts, opts...)
   140  		return nil
   141  	}
   142  }
   143  
   144  // WithPullSnapshotter specifies snapshotter name used for unpacking.
   145  func WithPullSnapshotter(snapshotterName string, opts ...snapshots.Opt) RemoteOpt {
   146  	return func(_ *Client, c *RemoteContext) error {
   147  		c.Snapshotter = snapshotterName
   148  		c.SnapshotterOpts = opts
   149  		return nil
   150  	}
   151  }
   152  
   153  // WithPullLabel sets a label to be associated with a pulled reference
   154  func WithPullLabel(key, value string) RemoteOpt {
   155  	return func(_ *Client, rc *RemoteContext) error {
   156  		if rc.Labels == nil {
   157  			rc.Labels = make(map[string]string)
   158  		}
   159  
   160  		rc.Labels[key] = value
   161  		return nil
   162  	}
   163  }
   164  
   165  // WithPullLabels associates a set of labels to a pulled reference
   166  func WithPullLabels(labels map[string]string) RemoteOpt {
   167  	return func(_ *Client, rc *RemoteContext) error {
   168  		if rc.Labels == nil {
   169  			rc.Labels = make(map[string]string)
   170  		}
   171  
   172  		for k, v := range labels {
   173  			rc.Labels[k] = v
   174  		}
   175  		return nil
   176  	}
   177  }
   178  
   179  // WithChildLabelMap sets the map function used to define the labels set
   180  // on referenced child content in the content store. This can be used
   181  // to overwrite the default GC labels or filter which labels get set
   182  // for content.
   183  // The default is `images.ChildGCLabels`.
   184  func WithChildLabelMap(fn func(ocispec.Descriptor) []string) RemoteOpt {
   185  	return func(_ *Client, c *RemoteContext) error {
   186  		c.ChildLabelMap = fn
   187  		return nil
   188  	}
   189  }
   190  
   191  // WithSchema1Conversion is used to convert Docker registry schema 1
   192  // manifests to oci manifests on pull. Without this option schema 1
   193  // manifests will return a not supported error.
   194  func WithSchema1Conversion(client *Client, c *RemoteContext) error {
   195  	c.ConvertSchema1 = true
   196  	return nil
   197  }
   198  
   199  // WithResolver specifies the resolver to use.
   200  func WithResolver(resolver remotes.Resolver) RemoteOpt {
   201  	return func(client *Client, c *RemoteContext) error {
   202  		c.Resolver = resolver
   203  		return nil
   204  	}
   205  }
   206  
   207  // WithImageHandler adds a base handler to be called on dispatch.
   208  func WithImageHandler(h images.Handler) RemoteOpt {
   209  	return func(client *Client, c *RemoteContext) error {
   210  		c.BaseHandlers = append(c.BaseHandlers, h)
   211  		return nil
   212  	}
   213  }
   214  
   215  // WithImageHandlerWrapper wraps the handlers to be called on dispatch.
   216  func WithImageHandlerWrapper(w func(images.Handler) images.Handler) RemoteOpt {
   217  	return func(client *Client, c *RemoteContext) error {
   218  		c.HandlerWrapper = w
   219  		return nil
   220  	}
   221  }
   222  
   223  // WithMaxConcurrentDownloads sets max concurrent download limit.
   224  func WithMaxConcurrentDownloads(max int) RemoteOpt {
   225  	return func(client *Client, c *RemoteContext) error {
   226  		c.MaxConcurrentDownloads = max
   227  		return nil
   228  	}
   229  }
   230  
   231  // WithAllMetadata downloads all manifests and known-configuration files
   232  func WithAllMetadata() RemoteOpt {
   233  	return func(_ *Client, c *RemoteContext) error {
   234  		c.AllMetadata = true
   235  		return nil
   236  	}
   237  }