ops.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package daemon
  2. import (
  3. "os/user"
  4. "github.com/docker/docker/testutil/environment"
  5. )
  6. // Option is used to configure a daemon.
  7. type Option func(*Daemon)
  8. // WithContainerdSocket sets the --containerd option on the daemon.
  9. // Use an empty string to remove the option.
  10. //
  11. // If unset the --containerd option will be used with a default value.
  12. func WithContainerdSocket(socket string) Option {
  13. return func(d *Daemon) {
  14. d.containerdSocket = socket
  15. }
  16. }
  17. // WithDefaultCgroupNamespaceMode sets the default cgroup namespace mode for the daemon
  18. func WithDefaultCgroupNamespaceMode(mode string) Option {
  19. return func(d *Daemon) {
  20. d.defaultCgroupNamespaceMode = mode
  21. }
  22. }
  23. // WithTestLogger causes the daemon to log certain actions to the provided test.
  24. func WithTestLogger(t LogT) Option {
  25. return func(d *Daemon) {
  26. d.log = t
  27. }
  28. }
  29. // WithExperimental sets the daemon in experimental mode
  30. func WithExperimental() Option {
  31. return func(d *Daemon) {
  32. d.experimental = true
  33. }
  34. }
  35. // WithInit sets the daemon init
  36. func WithInit() Option {
  37. return func(d *Daemon) {
  38. d.init = true
  39. }
  40. }
  41. // WithDockerdBinary sets the dockerd binary to the specified one
  42. func WithDockerdBinary(dockerdBinary string) Option {
  43. return func(d *Daemon) {
  44. d.dockerdBinary = dockerdBinary
  45. }
  46. }
  47. // WithSwarmPort sets the swarm port to use for swarm mode
  48. func WithSwarmPort(port int) Option {
  49. return func(d *Daemon) {
  50. d.SwarmPort = port
  51. }
  52. }
  53. // WithSwarmListenAddr sets the swarm listen addr to use for swarm mode
  54. func WithSwarmListenAddr(listenAddr string) Option {
  55. return func(d *Daemon) {
  56. d.swarmListenAddr = listenAddr
  57. }
  58. }
  59. // WithSwarmDefaultAddrPool sets the swarm default address pool to use for swarm mode
  60. func WithSwarmDefaultAddrPool(defaultAddrPool []string) Option {
  61. return func(d *Daemon) {
  62. d.DefaultAddrPool = defaultAddrPool
  63. }
  64. }
  65. // WithSwarmDefaultAddrPoolSubnetSize sets the subnet length mask of swarm default address pool to use for swarm mode
  66. func WithSwarmDefaultAddrPoolSubnetSize(subnetSize uint32) Option {
  67. return func(d *Daemon) {
  68. d.SubnetSize = subnetSize
  69. }
  70. }
  71. // WithSwarmDataPathPort sets the swarm datapath port to use for swarm mode
  72. func WithSwarmDataPathPort(datapathPort uint32) Option {
  73. return func(d *Daemon) {
  74. d.DataPathPort = datapathPort
  75. }
  76. }
  77. // WithEnvironment sets options from testutil/environment.Execution struct
  78. func WithEnvironment(e environment.Execution) Option {
  79. return func(d *Daemon) {
  80. if e.DaemonInfo.ExperimentalBuild {
  81. d.experimental = true
  82. }
  83. }
  84. }
  85. // WithStorageDriver sets store driver option
  86. func WithStorageDriver(driver string) Option {
  87. return func(d *Daemon) {
  88. d.storageDriver = driver
  89. }
  90. }
  91. // WithRootlessUser sets the daemon to be rootless
  92. func WithRootlessUser(username string) Option {
  93. return func(d *Daemon) {
  94. u, err := user.Lookup(username)
  95. if err != nil {
  96. panic(err)
  97. }
  98. d.rootlessUser = u
  99. }
  100. }
  101. // WithOOMScoreAdjust sets OOM score for the daemon
  102. func WithOOMScoreAdjust(score int) Option {
  103. return func(d *Daemon) {
  104. d.OOMScoreAdjust = score
  105. }
  106. }
  107. // WithEnvVars sets additional environment variables for the daemon
  108. func WithEnvVars(vars ...string) Option {
  109. return func(d *Daemon) {
  110. d.extraEnv = append(d.extraEnv, vars...)
  111. }
  112. }