github.com/rish1988/moby@v25.0.2+incompatible/integration/internal/container/ops.go (about)

     1  package container
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/api/types/container"
     7  	"github.com/docker/docker/api/types/mount"
     8  	"github.com/docker/docker/api/types/network"
     9  	"github.com/docker/docker/api/types/strslice"
    10  	"github.com/docker/go-connections/nat"
    11  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    12  )
    13  
    14  // WithName sets the name of the container
    15  func WithName(name string) func(*TestContainerConfig) {
    16  	return func(c *TestContainerConfig) {
    17  		c.Name = name
    18  	}
    19  }
    20  
    21  // WithLinks sets the links of the container
    22  func WithLinks(links ...string) func(*TestContainerConfig) {
    23  	return func(c *TestContainerConfig) {
    24  		c.HostConfig.Links = links
    25  	}
    26  }
    27  
    28  // WithImage sets the image of the container
    29  func WithImage(image string) func(*TestContainerConfig) {
    30  	return func(c *TestContainerConfig) {
    31  		c.Config.Image = image
    32  	}
    33  }
    34  
    35  // WithCmd sets the comannds of the container
    36  func WithCmd(cmds ...string) func(*TestContainerConfig) {
    37  	return func(c *TestContainerConfig) {
    38  		c.Config.Cmd = strslice.StrSlice(cmds)
    39  	}
    40  }
    41  
    42  // WithNetworkMode sets the network mode of the container
    43  func WithNetworkMode(mode string) func(*TestContainerConfig) {
    44  	return func(c *TestContainerConfig) {
    45  		c.HostConfig.NetworkMode = container.NetworkMode(mode)
    46  	}
    47  }
    48  
    49  // WithExposedPorts sets the exposed ports of the container
    50  func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
    51  	return func(c *TestContainerConfig) {
    52  		c.Config.ExposedPorts = map[nat.Port]struct{}{}
    53  		for _, port := range ports {
    54  			c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
    55  		}
    56  	}
    57  }
    58  
    59  // WithTty sets the TTY mode of the container
    60  func WithTty(tty bool) func(*TestContainerConfig) {
    61  	return func(c *TestContainerConfig) {
    62  		c.Config.Tty = tty
    63  	}
    64  }
    65  
    66  // WithWorkingDir sets the working dir of the container
    67  func WithWorkingDir(dir string) func(*TestContainerConfig) {
    68  	return func(c *TestContainerConfig) {
    69  		c.Config.WorkingDir = dir
    70  	}
    71  }
    72  
    73  // WithMount adds an mount
    74  func WithMount(m mount.Mount) func(*TestContainerConfig) {
    75  	return func(c *TestContainerConfig) {
    76  		c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
    77  	}
    78  }
    79  
    80  // WithVolume sets the volume of the container
    81  func WithVolume(target string) func(*TestContainerConfig) {
    82  	return func(c *TestContainerConfig) {
    83  		if c.Config.Volumes == nil {
    84  			c.Config.Volumes = map[string]struct{}{}
    85  		}
    86  		c.Config.Volumes[target] = struct{}{}
    87  	}
    88  }
    89  
    90  // WithBind sets the bind mount of the container
    91  func WithBind(src, target string) func(*TestContainerConfig) {
    92  	return func(c *TestContainerConfig) {
    93  		c.HostConfig.Binds = append(c.HostConfig.Binds, src+":"+target)
    94  	}
    95  }
    96  
    97  // WithBindRaw sets the bind mount of the container
    98  func WithBindRaw(s string) func(*TestContainerConfig) {
    99  	return func(c *TestContainerConfig) {
   100  		c.HostConfig.Binds = append(c.HostConfig.Binds, s)
   101  	}
   102  }
   103  
   104  // WithTmpfs sets a target path in the container to a tmpfs, with optional options
   105  // (separated with a colon).
   106  func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) {
   107  	return func(c *TestContainerConfig) {
   108  		if c.HostConfig.Tmpfs == nil {
   109  			c.HostConfig.Tmpfs = make(map[string]string)
   110  		}
   111  
   112  		target, opts, _ := strings.Cut(targetAndOpts, ":")
   113  		c.HostConfig.Tmpfs[target] = opts
   114  	}
   115  }
   116  
   117  func WithMacAddress(networkName, mac string) func(config *TestContainerConfig) {
   118  	return func(c *TestContainerConfig) {
   119  		if c.NetworkingConfig.EndpointsConfig == nil {
   120  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   121  		}
   122  		if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
   123  			c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
   124  		}
   125  		c.NetworkingConfig.EndpointsConfig[networkName].MacAddress = mac
   126  	}
   127  }
   128  
   129  // WithIPv4 sets the specified ip for the specified network of the container
   130  func WithIPv4(networkName, ip string) func(*TestContainerConfig) {
   131  	return func(c *TestContainerConfig) {
   132  		if c.NetworkingConfig.EndpointsConfig == nil {
   133  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   134  		}
   135  		if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
   136  			c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
   137  		}
   138  		if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
   139  			c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
   140  		}
   141  		c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip
   142  	}
   143  }
   144  
   145  // WithIPv6 sets the specified ip6 for the specified network of the container
   146  func WithIPv6(networkName, ip string) func(*TestContainerConfig) {
   147  	return func(c *TestContainerConfig) {
   148  		if c.NetworkingConfig.EndpointsConfig == nil {
   149  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   150  		}
   151  		if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
   152  			c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
   153  		}
   154  		if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
   155  			c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
   156  		}
   157  		c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip
   158  	}
   159  }
   160  
   161  func WithEndpointSettings(nw string, config *network.EndpointSettings) func(*TestContainerConfig) {
   162  	return func(c *TestContainerConfig) {
   163  		if c.NetworkingConfig.EndpointsConfig == nil {
   164  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   165  		}
   166  		if _, ok := c.NetworkingConfig.EndpointsConfig[nw]; !ok {
   167  			c.NetworkingConfig.EndpointsConfig[nw] = config
   168  		}
   169  	}
   170  }
   171  
   172  // WithLogDriver sets the log driver to use for the container
   173  func WithLogDriver(driver string) func(*TestContainerConfig) {
   174  	return func(c *TestContainerConfig) {
   175  		c.HostConfig.LogConfig.Type = driver
   176  	}
   177  }
   178  
   179  // WithAutoRemove sets the container to be removed on exit
   180  func WithAutoRemove(c *TestContainerConfig) {
   181  	c.HostConfig.AutoRemove = true
   182  }
   183  
   184  // WithPidsLimit sets the container's "pids-limit
   185  func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
   186  	return func(c *TestContainerConfig) {
   187  		if c.HostConfig == nil {
   188  			c.HostConfig = &container.HostConfig{}
   189  		}
   190  		c.HostConfig.PidsLimit = limit
   191  	}
   192  }
   193  
   194  // WithRestartPolicy sets container's restart policy
   195  func WithRestartPolicy(policy container.RestartPolicyMode) func(c *TestContainerConfig) {
   196  	return func(c *TestContainerConfig) {
   197  		c.HostConfig.RestartPolicy.Name = policy
   198  	}
   199  }
   200  
   201  // WithUser sets the user
   202  func WithUser(user string) func(c *TestContainerConfig) {
   203  	return func(c *TestContainerConfig) {
   204  		c.Config.User = user
   205  	}
   206  }
   207  
   208  // WithAdditionalGroups sets the additional groups for the container
   209  func WithAdditionalGroups(groups ...string) func(c *TestContainerConfig) {
   210  	return func(c *TestContainerConfig) {
   211  		c.HostConfig.GroupAdd = groups
   212  	}
   213  }
   214  
   215  // WithPrivileged sets privileged mode for the container
   216  func WithPrivileged(privileged bool) func(*TestContainerConfig) {
   217  	return func(c *TestContainerConfig) {
   218  		if c.HostConfig == nil {
   219  			c.HostConfig = &container.HostConfig{}
   220  		}
   221  		c.HostConfig.Privileged = privileged
   222  	}
   223  }
   224  
   225  // WithCgroupnsMode sets the cgroup namespace mode for the container
   226  func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
   227  	return func(c *TestContainerConfig) {
   228  		if c.HostConfig == nil {
   229  			c.HostConfig = &container.HostConfig{}
   230  		}
   231  		c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode)
   232  	}
   233  }
   234  
   235  // WithExtraHost sets the user defined IP:Host mappings in the container's
   236  // /etc/hosts file
   237  func WithExtraHost(extraHost string) func(*TestContainerConfig) {
   238  	return func(c *TestContainerConfig) {
   239  		c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
   240  	}
   241  }
   242  
   243  // WithPlatform specifies the desired platform the image should have.
   244  func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
   245  	return func(c *TestContainerConfig) {
   246  		c.Platform = p
   247  	}
   248  }
   249  
   250  // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
   251  func WithWindowsDevice(device string) func(*TestContainerConfig) {
   252  	return func(c *TestContainerConfig) {
   253  		c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device})
   254  	}
   255  }
   256  
   257  // WithIsolation specifies the isolation technology to apply to the container
   258  func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) {
   259  	return func(c *TestContainerConfig) {
   260  		c.HostConfig.Isolation = isolation
   261  	}
   262  }
   263  
   264  // WithConsoleSize sets the initial console size of the container
   265  func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
   266  	return func(c *TestContainerConfig) {
   267  		c.HostConfig.ConsoleSize = [2]uint{height, width}
   268  	}
   269  }
   270  
   271  // WithRuntime sets the runtime to use to start the container
   272  func WithRuntime(name string) func(*TestContainerConfig) {
   273  	return func(c *TestContainerConfig) {
   274  		c.HostConfig.Runtime = name
   275  	}
   276  }
   277  
   278  // WithCDIDevices sets the CDI devices to use to start the container
   279  func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) {
   280  	return func(c *TestContainerConfig) {
   281  		request := container.DeviceRequest{
   282  			Driver:    "cdi",
   283  			DeviceIDs: cdiDeviceNames,
   284  		}
   285  		c.HostConfig.DeviceRequests = append(c.HostConfig.DeviceRequests, request)
   286  	}
   287  }
   288  
   289  func WithCapability(capabilities ...string) func(*TestContainerConfig) {
   290  	return func(c *TestContainerConfig) {
   291  		c.HostConfig.CapAdd = append(c.HostConfig.CapAdd, capabilities...)
   292  	}
   293  }
   294  
   295  func WithDropCapability(capabilities ...string) func(*TestContainerConfig) {
   296  	return func(c *TestContainerConfig) {
   297  		c.HostConfig.CapDrop = append(c.HostConfig.CapDrop, capabilities...)
   298  	}
   299  }
   300  
   301  func WithSecurityOpt(opt string) func(*TestContainerConfig) {
   302  	return func(c *TestContainerConfig) {
   303  		c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt)
   304  	}
   305  }
   306  
   307  // WithPIDMode sets the PID-mode for the container.
   308  func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) {
   309  	return func(c *TestContainerConfig) {
   310  		c.HostConfig.PidMode = mode
   311  	}
   312  }
   313  
   314  func WithStopSignal(stopSignal string) func(c *TestContainerConfig) {
   315  	return func(c *TestContainerConfig) {
   316  		c.Config.StopSignal = stopSignal
   317  	}
   318  }
   319  
   320  func WithContainerWideMacAddress(address string) func(c *TestContainerConfig) {
   321  	return func(c *TestContainerConfig) {
   322  		c.Config.MacAddress = address //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
   323  	}
   324  }