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