github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/libcontainerd/remote_daemon_options.go (about)

     1  // +build !windows
     2  
     3  package libcontainerd
     4  
     5  import "fmt"
     6  
     7  // WithRemoteAddr sets the external containerd socket to connect to.
     8  func WithRemoteAddr(addr string) RemoteOption {
     9  	return rpcAddr(addr)
    10  }
    11  
    12  type rpcAddr string
    13  
    14  func (a rpcAddr) Apply(r Remote) error {
    15  	if remote, ok := r.(*remote); ok {
    16  		remote.GRPC.Address = string(a)
    17  		return nil
    18  	}
    19  	return fmt.Errorf("WithRemoteAddr option not supported for this remote")
    20  }
    21  
    22  // WithRemoteAddrUser sets the uid and gid to create the RPC address with
    23  func WithRemoteAddrUser(uid, gid int) RemoteOption {
    24  	return rpcUser{uid, gid}
    25  }
    26  
    27  type rpcUser struct {
    28  	uid int
    29  	gid int
    30  }
    31  
    32  func (u rpcUser) Apply(r Remote) error {
    33  	if remote, ok := r.(*remote); ok {
    34  		remote.GRPC.UID = u.uid
    35  		remote.GRPC.GID = u.gid
    36  		return nil
    37  	}
    38  	return fmt.Errorf("WithRemoteAddr option not supported for this remote")
    39  }
    40  
    41  // WithStartDaemon defines if libcontainerd should also run containerd daemon.
    42  func WithStartDaemon(start bool) RemoteOption {
    43  	return startDaemon(start)
    44  }
    45  
    46  type startDaemon bool
    47  
    48  func (s startDaemon) Apply(r Remote) error {
    49  	if remote, ok := r.(*remote); ok {
    50  		remote.startDaemon = bool(s)
    51  		return nil
    52  	}
    53  	return fmt.Errorf("WithStartDaemon option not supported for this remote")
    54  }
    55  
    56  // WithLogLevel defines which log level to starts containerd with.
    57  // This only makes sense if WithStartDaemon() was set to true.
    58  func WithLogLevel(lvl string) RemoteOption {
    59  	return logLevel(lvl)
    60  }
    61  
    62  type logLevel string
    63  
    64  func (l logLevel) Apply(r Remote) error {
    65  	if remote, ok := r.(*remote); ok {
    66  		remote.Debug.Level = string(l)
    67  		return nil
    68  	}
    69  	return fmt.Errorf("WithDebugLog option not supported for this remote")
    70  }
    71  
    72  // WithDebugAddress defines at which location the debug GRPC connection
    73  // should be made
    74  func WithDebugAddress(addr string) RemoteOption {
    75  	return debugAddress(addr)
    76  }
    77  
    78  type debugAddress string
    79  
    80  func (d debugAddress) Apply(r Remote) error {
    81  	if remote, ok := r.(*remote); ok {
    82  		remote.Debug.Address = string(d)
    83  		return nil
    84  	}
    85  	return fmt.Errorf("WithDebugAddress option not supported for this remote")
    86  }
    87  
    88  // WithMetricsAddress defines at which location the debug GRPC connection
    89  // should be made
    90  func WithMetricsAddress(addr string) RemoteOption {
    91  	return metricsAddress(addr)
    92  }
    93  
    94  type metricsAddress string
    95  
    96  func (m metricsAddress) Apply(r Remote) error {
    97  	if remote, ok := r.(*remote); ok {
    98  		remote.Metrics.Address = string(m)
    99  		return nil
   100  	}
   101  	return fmt.Errorf("WithMetricsAddress option not supported for this remote")
   102  }
   103  
   104  // WithSnapshotter defines snapshotter driver should be used
   105  func WithSnapshotter(name string) RemoteOption {
   106  	return snapshotter(name)
   107  }
   108  
   109  type snapshotter string
   110  
   111  func (s snapshotter) Apply(r Remote) error {
   112  	if remote, ok := r.(*remote); ok {
   113  		remote.snapshotter = string(s)
   114  		return nil
   115  	}
   116  	return fmt.Errorf("WithSnapshotter option not supported for this remote")
   117  }
   118  
   119  // WithPlugin allow configuring a containerd plugin
   120  // configuration values passed needs to be quoted if quotes are needed in
   121  // the toml format.
   122  func WithPlugin(name string, conf interface{}) RemoteOption {
   123  	return pluginConf{
   124  		name: name,
   125  		conf: conf,
   126  	}
   127  }
   128  
   129  type pluginConf struct {
   130  	// Name is the name of the plugin
   131  	name string
   132  	conf interface{}
   133  }
   134  
   135  func (p pluginConf) Apply(r Remote) error {
   136  	if remote, ok := r.(*remote); ok {
   137  		remote.pluginConfs.Plugins[p.name] = p.conf
   138  		return nil
   139  	}
   140  	return fmt.Errorf("WithPlugin option not supported for this remote")
   141  }