ops.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. func WithUserNsRemap(remap string) Option {
  18. return func(d *Daemon) {
  19. d.usernsRemap = remap
  20. }
  21. }
  22. // WithDefaultCgroupNamespaceMode sets the default cgroup namespace mode for the daemon
  23. func WithDefaultCgroupNamespaceMode(mode string) Option {
  24. return func(d *Daemon) {
  25. d.defaultCgroupNamespaceMode = mode
  26. }
  27. }
  28. // WithTestLogger causes the daemon to log certain actions to the provided test.
  29. func WithTestLogger(t LogT) Option {
  30. return func(d *Daemon) {
  31. d.log = t
  32. }
  33. }
  34. // WithExperimental sets the daemon in experimental mode
  35. func WithExperimental() Option {
  36. return func(d *Daemon) {
  37. d.experimental = true
  38. }
  39. }
  40. // WithInit sets the daemon init
  41. func WithInit() Option {
  42. return func(d *Daemon) {
  43. d.init = true
  44. }
  45. }
  46. // WithDockerdBinary sets the dockerd binary to the specified one
  47. func WithDockerdBinary(dockerdBinary string) Option {
  48. return func(d *Daemon) {
  49. d.dockerdBinary = dockerdBinary
  50. }
  51. }
  52. // WithSwarmPort sets the swarm port to use for swarm mode
  53. func WithSwarmPort(port int) Option {
  54. return func(d *Daemon) {
  55. d.SwarmPort = port
  56. }
  57. }
  58. // WithSwarmListenAddr sets the swarm listen addr to use for swarm mode
  59. func WithSwarmListenAddr(listenAddr string) Option {
  60. return func(d *Daemon) {
  61. d.swarmListenAddr = listenAddr
  62. }
  63. }
  64. // WithSwarmDefaultAddrPool sets the swarm default address pool to use for swarm mode
  65. func WithSwarmDefaultAddrPool(defaultAddrPool []string) Option {
  66. return func(d *Daemon) {
  67. d.DefaultAddrPool = defaultAddrPool
  68. }
  69. }
  70. // WithSwarmDefaultAddrPoolSubnetSize sets the subnet length mask of swarm default address pool to use for swarm mode
  71. func WithSwarmDefaultAddrPoolSubnetSize(subnetSize uint32) Option {
  72. return func(d *Daemon) {
  73. d.SubnetSize = subnetSize
  74. }
  75. }
  76. // WithSwarmDataPathPort sets the swarm datapath port to use for swarm mode
  77. func WithSwarmDataPathPort(datapathPort uint32) Option {
  78. return func(d *Daemon) {
  79. d.DataPathPort = datapathPort
  80. }
  81. }
  82. // WithEnvironment sets options from testutil/environment.Execution struct
  83. func WithEnvironment(e environment.Execution) Option {
  84. return func(d *Daemon) {
  85. if e.DaemonInfo.ExperimentalBuild {
  86. d.experimental = true
  87. }
  88. }
  89. }
  90. // WithStorageDriver sets store driver option
  91. func WithStorageDriver(driver string) Option {
  92. return func(d *Daemon) {
  93. d.storageDriver = driver
  94. }
  95. }
  96. // WithRootlessUser sets the daemon to be rootless
  97. func WithRootlessUser(username string) Option {
  98. return func(d *Daemon) {
  99. u, err := user.Lookup(username)
  100. if err != nil {
  101. panic(err)
  102. }
  103. d.rootlessUser = u
  104. }
  105. }
  106. // WithOOMScoreAdjust sets OOM score for the daemon
  107. func WithOOMScoreAdjust(score int) Option {
  108. return func(d *Daemon) {
  109. d.OOMScoreAdjust = score
  110. }
  111. }
  112. // WithEnvVars sets additional environment variables for the daemon
  113. func WithEnvVars(vars ...string) Option {
  114. return func(d *Daemon) {
  115. d.extraEnv = append(d.extraEnv, vars...)
  116. }
  117. }