13
"github.com/containers/buildah/pkg/parse"
14
nettypes "github.com/containers/common/libnetwork/types"
15
"github.com/containers/common/pkg/config"
16
"github.com/containers/common/pkg/secrets"
17
"github.com/containers/image/v5/manifest"
18
"github.com/containers/image/v5/types"
19
"github.com/containers/podman/v5/libpod/define"
20
"github.com/containers/podman/v5/libpod/events"
21
"github.com/containers/podman/v5/pkg/namespaces"
22
"github.com/containers/podman/v5/pkg/specgen"
23
"github.com/containers/podman/v5/pkg/util"
24
"github.com/containers/storage"
25
"github.com/containers/storage/pkg/fileutils"
26
"github.com/containers/storage/pkg/idtools"
27
"github.com/containers/storage/pkg/regexp"
28
"github.com/opencontainers/runtime-spec/specs-go"
29
"github.com/opencontainers/runtime-tools/generate"
30
"github.com/sirupsen/logrus"
33
var umaskRegex = regexp.Delayed(`^[0-7]{1,4}$`)
35
// WithStorageConfig uses the given configuration to set up container storage.
36
// If this is not specified, the system default configuration will be used
38
func WithStorageConfig(config storage.StoreOptions) RuntimeOption {
39
return func(rt *Runtime) error {
41
return define.ErrRuntimeFinalized
46
if config.RunRoot != "" {
47
rt.storageConfig.RunRoot = config.RunRoot
48
rt.storageSet.RunRootSet = true
52
if config.GraphRoot != "" {
53
rt.storageConfig.GraphRoot = config.GraphRoot
54
rt.storageSet.GraphRootSet = true
58
graphDriverChanged := false
59
if config.GraphDriverName != "" {
60
rt.storageConfig.GraphDriverName = config.GraphDriverName
61
rt.storageSet.GraphDriverNameSet = true
63
graphDriverChanged = true
66
if config.GraphDriverOptions != nil {
67
if graphDriverChanged {
68
rt.storageConfig.GraphDriverOptions = make([]string, len(config.GraphDriverOptions))
69
copy(rt.storageConfig.GraphDriverOptions, config.GraphDriverOptions)
71
rt.storageConfig.GraphDriverOptions = config.GraphDriverOptions
76
if config.UIDMap != nil {
77
rt.storageConfig.UIDMap = make([]idtools.IDMap, len(config.UIDMap))
78
copy(rt.storageConfig.UIDMap, config.UIDMap)
81
if config.GIDMap != nil {
82
rt.storageConfig.GIDMap = make([]idtools.IDMap, len(config.GIDMap))
83
copy(rt.storageConfig.GIDMap, config.GIDMap)
86
// If any one of runroot, graphroot, graphdrivername,
87
// or graphdriveroptions are set, then GraphRoot and RunRoot
90
storeOpts, err := storage.DefaultStoreOptions()
94
if rt.storageConfig.GraphRoot == "" {
95
rt.storageConfig.GraphRoot = storeOpts.GraphRoot
97
if rt.storageConfig.RunRoot == "" {
98
rt.storageConfig.RunRoot = storeOpts.RunRoot
106
func WithTransientStore(transientStore bool) RuntimeOption {
107
return func(rt *Runtime) error {
109
return define.ErrRuntimeFinalized
112
rt.storageConfig.TransientStore = transientStore
118
func WithImageStore(imageStore string) RuntimeOption {
119
return func(rt *Runtime) error {
121
return define.ErrRuntimeFinalized
124
rt.storageConfig.ImageStore = imageStore
130
// WithSignaturePolicy specifies the path of a file which decides how trust is
131
// managed for images we've pulled.
132
// If this is not specified, the system default configuration will be used
134
func WithSignaturePolicy(path string) RuntimeOption {
135
return func(rt *Runtime) error {
137
return define.ErrRuntimeFinalized
140
rt.config.Engine.SignaturePolicyPath = path
146
// WithOCIRuntime specifies an OCI runtime to use for running containers.
147
func WithOCIRuntime(runtime string) RuntimeOption {
148
return func(rt *Runtime) error {
150
return define.ErrRuntimeFinalized
154
return fmt.Errorf("must provide a valid path: %w", define.ErrInvalidArg)
157
rt.config.Engine.OCIRuntime = runtime
163
// WithCtrOCIRuntime specifies an OCI runtime in container's config.
164
func WithCtrOCIRuntime(runtime string) CtrCreateOption {
165
return func(ctr *Container) error {
167
return define.ErrCtrFinalized
169
ctr.config.OCIRuntime = runtime
174
// WithConmonPath specifies the path to the conmon binary which manages the
176
func WithConmonPath(path string) RuntimeOption {
177
return func(rt *Runtime) error {
179
return define.ErrRuntimeFinalized
183
return fmt.Errorf("must provide a valid path: %w", define.ErrInvalidArg)
186
rt.config.Engine.ConmonPath.Set([]string{path})
192
// WithConmonEnv specifies the environment variable list for the conmon process.
193
func WithConmonEnv(environment []string) RuntimeOption {
194
return func(rt *Runtime) error {
196
return define.ErrRuntimeFinalized
199
rt.config.Engine.ConmonEnvVars.Set(environment)
205
// WithNetworkCmdPath specifies the path to the slirp4netns binary which manages the
207
func WithNetworkCmdPath(path string) RuntimeOption {
208
return func(rt *Runtime) error {
210
return define.ErrRuntimeFinalized
213
rt.config.Engine.NetworkCmdPath = path
219
// WithNetworkBackend specifies the name of the network backend.
220
func WithNetworkBackend(name string) RuntimeOption {
221
return func(rt *Runtime) error {
223
return define.ErrRuntimeFinalized
226
rt.config.Network.NetworkBackend = name
232
// WithCgroupManager specifies the manager implementation name which is used to
233
// handle cgroups for containers.
234
// Current valid values are "cgroupfs" and "systemd".
235
func WithCgroupManager(manager string) RuntimeOption {
236
return func(rt *Runtime) error {
238
return define.ErrRuntimeFinalized
241
if manager != config.CgroupfsCgroupsManager && manager != config.SystemdCgroupsManager {
242
return fmt.Errorf("cgroup manager must be one of %s and %s: %w",
243
config.CgroupfsCgroupsManager, config.SystemdCgroupsManager, define.ErrInvalidArg)
246
rt.config.Engine.CgroupManager = manager
252
// WithStaticDir sets the directory that static runtime files which persist
253
// across reboots will be stored.
254
func WithStaticDir(dir string) RuntimeOption {
255
return func(rt *Runtime) error {
257
return define.ErrRuntimeFinalized
260
rt.config.Engine.StaticDir = dir
266
// WithRegistriesConf configures the runtime to always use specified
267
// registries.conf for image processing.
268
func WithRegistriesConf(path string) RuntimeOption {
269
logrus.Debugf("Setting custom registries.conf: %q", path)
270
return func(rt *Runtime) error {
271
if err := fileutils.Exists(path); err != nil {
272
return fmt.Errorf("locating specified registries.conf: %w", err)
274
if rt.imageContext == nil {
275
rt.imageContext = &types.SystemContext{
276
BigFilesTemporaryDir: parse.GetTempDir(),
280
rt.imageContext.SystemRegistriesConfPath = path
285
// WithDatabaseBackend configures the runtime's database backend.
286
func WithDatabaseBackend(value string) RuntimeOption {
287
logrus.Debugf("Setting custom database backend: %q", value)
288
return func(rt *Runtime) error {
289
// The value will be parsed later on.
290
rt.config.Engine.DBBackend = value
295
// WithHooksDir sets the directories to look for OCI runtime hook configuration.
296
func WithHooksDir(hooksDirs ...string) RuntimeOption {
297
return func(rt *Runtime) error {
299
return define.ErrRuntimeFinalized
302
for _, hooksDir := range hooksDirs {
304
return fmt.Errorf("empty-string hook directories are not supported: %w", define.ErrInvalidArg)
308
rt.config.Engine.HooksDir.Set(hooksDirs)
313
// WithCDI sets the devices to check for CDI configuration.
314
func WithCDI(devices []string) CtrCreateOption {
315
return func(ctr *Container) error {
317
return define.ErrCtrFinalized
319
ctr.config.CDIDevices = devices
324
// WithStorageOpts sets the devices to check for CDI configuration.
325
func WithStorageOpts(storageOpts map[string]string) CtrCreateOption {
326
return func(ctr *Container) error {
328
return define.ErrCtrFinalized
330
ctr.config.StorageOpts = storageOpts
335
// WithDefaultMountsFile sets the file to look at for default mounts (mainly
337
// Note we are not saving this in the database as it is for testing purposes
339
func WithDefaultMountsFile(mountsFile string) RuntimeOption {
340
return func(rt *Runtime) error {
342
return define.ErrRuntimeFinalized
345
if mountsFile == "" {
346
return define.ErrInvalidArg
348
rt.config.Containers.DefaultMountsFile = mountsFile
353
// WithTmpDir sets the directory that temporary runtime files which are not
354
// expected to survive across reboots will be stored.
355
// This should be located on a tmpfs mount (/tmp or /run for example).
356
func WithTmpDir(dir string) RuntimeOption {
357
return func(rt *Runtime) error {
359
return define.ErrRuntimeFinalized
361
rt.config.Engine.TmpDir = dir
367
// WithNoPivotRoot sets the runtime to use MS_MOVE instead of PIVOT_ROOT when
368
// starting containers.
369
func WithNoPivotRoot() RuntimeOption {
370
return func(rt *Runtime) error {
372
return define.ErrRuntimeFinalized
375
rt.config.Engine.NoPivotRoot = true
381
// WithNetworkConfigDir sets the network configuration directory.
382
func WithNetworkConfigDir(dir string) RuntimeOption {
383
return func(rt *Runtime) error {
385
return define.ErrRuntimeFinalized
388
rt.config.Network.NetworkConfigDir = dir
394
// WithCNIPluginDir sets the CNI plugins directory.
395
func WithCNIPluginDir(dir string) RuntimeOption {
396
return func(rt *Runtime) error {
398
return define.ErrRuntimeFinalized
401
rt.config.Network.CNIPluginDirs.Set([]string{dir})
407
// WithNamespace sets the namespace for libpod.
408
// Namespaces are used to create scopes to separate containers and pods
410
// When namespace is set, libpod will only view containers and pods in
411
// the same namespace. All containers and pods created will default to
412
// the namespace set here.
413
// A namespace of "", the empty string, is equivalent to no namespace,
414
// and all containers and pods will be visible.
415
func WithNamespace(ns string) RuntimeOption {
416
return func(rt *Runtime) error {
418
return define.ErrRuntimeFinalized
421
rt.config.Engine.Namespace = ns
427
// WithVolumePath sets the path under which all named volumes
429
// The path changes based on whether the user is running as root or not.
430
func WithVolumePath(volPath string) RuntimeOption {
431
return func(rt *Runtime) error {
433
return define.ErrRuntimeFinalized
436
rt.config.Engine.VolumePath = volPath
437
rt.storageSet.VolumePathSet = true
443
// WithDefaultInfraCommand sets the command to
444
// run on pause container start up.
445
func WithDefaultInfraCommand(cmd string) RuntimeOption {
446
return func(rt *Runtime) error {
448
return define.ErrRuntimeFinalized
451
rt.config.Engine.InfraCommand = cmd
457
// WithReset tells Libpod that the runtime will be used to perform a system
458
// reset. A number of checks at initialization are relaxed as the runtime is
459
// going to be used to remove all containers, pods, volumes, images, and
461
func WithReset() RuntimeOption {
462
return func(rt *Runtime) error {
464
return define.ErrRuntimeFinalized
473
// WithRenumber tells Libpod that the runtime will be used to perform a system
474
// renumber. A number of checks on initialization related to locks are relaxed.
475
func WithRenumber() RuntimeOption {
476
return func(rt *Runtime) error {
478
return define.ErrRuntimeFinalized
487
// WithEventsLogger sets the events backend to use.
488
// Currently supported values are "file" for file backend and "journald" for
490
func WithEventsLogger(logger string) RuntimeOption {
491
return func(rt *Runtime) error {
493
return define.ErrRuntimeFinalized
496
if !events.IsValidEventer(logger) {
497
return fmt.Errorf("%q is not a valid events backend: %w", logger, define.ErrInvalidArg)
500
rt.config.Engine.EventsLogger = logger
505
// WithEnableSDNotify sets a runtime option so we know whether to disable socket/FD
507
func WithEnableSDNotify() RuntimeOption {
508
return func(rt *Runtime) error {
509
rt.config.Engine.SDNotify = true
514
// WithSyslog sets a runtime option so we know that we have to log to the syslog as well
515
func WithSyslog() RuntimeOption {
516
return func(rt *Runtime) error {
522
// WithRuntimeFlags adds the global runtime flags to the container config
523
func WithRuntimeFlags(runtimeFlags []string) RuntimeOption {
524
return func(rt *Runtime) error {
526
return define.ErrRuntimeFinalized
528
rt.runtimeFlags = runtimeFlags
533
// Container Creation Options
535
// WithMaxLogSize sets the maximum size of container logs.
536
// Positive sizes are limits in bytes, -1 is unlimited.
537
func WithMaxLogSize(limit int64) CtrCreateOption {
538
return func(ctr *Container) error {
540
return define.ErrRuntimeFinalized
542
ctr.config.LogSize = limit
548
// WithShmDir sets the directory that should be mounted on /dev/shm.
549
func WithShmDir(dir string) CtrCreateOption {
550
return func(ctr *Container) error {
552
return define.ErrCtrFinalized
555
ctr.config.ShmDir = dir
560
// WithNOShmMount tells libpod whether to mount /dev/shm
561
func WithNoShm(mount bool) CtrCreateOption {
562
return func(ctr *Container) error {
564
return define.ErrCtrFinalized
567
ctr.config.NoShm = mount
572
// WithNoShmShare tells libpod whether to share containers /dev/shm with other containers
573
func WithNoShmShare(share bool) CtrCreateOption {
574
return func(ctr *Container) error {
576
return define.ErrCtrFinalized
579
ctr.config.NoShmShare = share
584
// WithSystemd turns on systemd mode in the container
585
func WithSystemd() CtrCreateOption {
586
return func(ctr *Container) error {
588
return define.ErrCtrFinalized
592
ctr.config.Systemd = &t
597
// WithSdNotifySocket sets the sd-notify of the container
598
func WithSdNotifySocket(socketPath string) CtrCreateOption {
599
return func(ctr *Container) error {
601
return define.ErrCtrFinalized
603
ctr.config.SdNotifySocket = socketPath
608
// WithSdNotifyMode sets the sd-notify method
609
func WithSdNotifyMode(mode string) CtrCreateOption {
610
return func(ctr *Container) error {
612
return define.ErrCtrFinalized
615
if err := define.ValidateSdNotifyMode(mode); err != nil {
619
ctr.config.SdNotifyMode = mode
624
// WithShmSize sets the size of /dev/shm tmpfs mount.
625
func WithShmSize(size int64) CtrCreateOption {
626
return func(ctr *Container) error {
628
return define.ErrCtrFinalized
631
ctr.config.ShmSize = size
636
// WithShmSizeSystemd sets the size of systemd-specific mounts:
642
func WithShmSizeSystemd(size int64) CtrCreateOption {
643
return func(ctr *Container) error {
645
return define.ErrCtrFinalized
648
ctr.config.ShmSizeSystemd = size
653
// WithPrivileged sets the privileged flag in the container runtime.
654
func WithPrivileged(privileged bool) CtrCreateOption {
655
return func(ctr *Container) error {
657
return define.ErrCtrFinalized
660
ctr.config.Privileged = privileged
665
// WithReadWriteTmpfs sets up read-write tmpfs flag in the container runtime.
666
// Only Used if containers are run in ReadOnly mode.
667
func WithReadWriteTmpfs(readWriteTmpfs bool) CtrCreateOption {
668
return func(ctr *Container) error {
670
return define.ErrCtrFinalized
673
ctr.config.ReadWriteTmpfs = readWriteTmpfs
678
// WithSecLabels sets the labels for SELinux.
679
func WithSecLabels(labelOpts []string) CtrCreateOption {
680
return func(ctr *Container) error {
682
return define.ErrCtrFinalized
684
ctr.config.LabelOpts = labelOpts
689
// WithUser sets the user identity field in configuration.
690
// Valid uses [user | user:group | uid | uid:gid | user:gid | uid:group ].
691
func WithUser(user string) CtrCreateOption {
692
return func(ctr *Container) error {
694
return define.ErrCtrFinalized
697
ctr.config.User = user
702
// WithRootFSFromImage sets up a fresh root filesystem using the given image.
703
// If useImageConfig is specified, image volumes, environment variables, and
704
// other configuration from the image will be added to the config.
705
// TODO: Replace image name and ID with a libpod.Image struct when that is
707
func WithRootFSFromImage(imageID, imageName, rawImageName string) CtrCreateOption {
708
return func(ctr *Container) error {
710
return define.ErrCtrFinalized
713
ctr.config.RootfsImageID = imageID
714
ctr.config.RootfsImageName = imageName
715
ctr.config.RawImageName = rawImageName
720
// WithStdin keeps stdin on the container open to allow interaction.
721
func WithStdin() CtrCreateOption {
722
return func(ctr *Container) error {
724
return define.ErrCtrFinalized
727
ctr.config.Stdin = true
733
// WithPod adds the container to a pod.
734
// Containers which join a pod can only join the Linux namespaces of other
735
// containers in the same pod.
736
// Containers can only join pods in the same libpod namespace.
737
func (r *Runtime) WithPod(pod *Pod) CtrCreateOption {
738
return func(ctr *Container) error {
740
return define.ErrCtrFinalized
744
return define.ErrInvalidArg
746
ctr.config.Pod = pod.ID()
752
// WithLabels adds labels to the container.
753
func WithLabels(labels map[string]string) CtrCreateOption {
754
return func(ctr *Container) error {
756
return define.ErrCtrFinalized
759
ctr.config.Labels = make(map[string]string)
760
for key, value := range labels {
761
ctr.config.Labels[key] = value
768
// WithName sets the container's name.
769
func WithName(name string) CtrCreateOption {
770
return func(ctr *Container) error {
772
return define.ErrCtrFinalized
775
name = strings.TrimPrefix(name, "/")
776
// Check the name against a regex
777
if !define.NameRegex.MatchString(name) {
778
return define.RegexError
781
ctr.config.Name = name
787
// WithStopSignal sets the signal that will be sent to stop the container.
788
func WithStopSignal(signal syscall.Signal) CtrCreateOption {
789
return func(ctr *Container) error {
791
return define.ErrCtrFinalized
795
return fmt.Errorf("stop signal cannot be 0: %w", define.ErrInvalidArg)
796
} else if signal > 64 {
797
return fmt.Errorf("stop signal cannot be greater than 64 (SIGRTMAX): %w", define.ErrInvalidArg)
800
ctr.config.StopSignal = uint(signal)
806
// WithStopTimeout sets the time to after initial stop signal is sent to the
807
// container, before sending the kill signal.
808
func WithStopTimeout(timeout uint) CtrCreateOption {
809
return func(ctr *Container) error {
811
return define.ErrCtrFinalized
814
ctr.config.StopTimeout = timeout
820
// WithTimeout sets the maximum time a container is allowed to run"
821
func WithTimeout(timeout uint) CtrCreateOption {
822
return func(ctr *Container) error {
824
return define.ErrCtrFinalized
826
ctr.config.Timeout = timeout
832
// WithIDMappings sets the idmappings for the container
833
func WithIDMappings(idmappings storage.IDMappingOptions) CtrCreateOption {
834
return func(ctr *Container) error {
836
return define.ErrCtrFinalized
839
ctr.config.IDMappings = idmappings
844
// WithUTSNSFromPod indicates that the container should join the UTS namespace of
846
func WithUTSNSFromPod(p *Pod) CtrCreateOption {
847
return func(ctr *Container) error {
849
return define.ErrCtrFinalized
852
if err := validPodNSOption(p, ctr.config.Pod); err != nil {
856
infraContainer, err := p.InfraContainerID()
860
ctr.config.UTSNsCtr = infraContainer
866
// WithIPCNSFrom indicates that the container should join the IPC namespace of
867
// the given container.
868
// If the container has joined a pod, it can only join the namespaces of
869
// containers in the same pod.
870
func WithIPCNSFrom(nsCtr *Container) CtrCreateOption {
871
return func(ctr *Container) error {
873
return define.ErrCtrFinalized
876
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
880
ctr.config.IPCNsCtr = nsCtr.ID()
886
// WithMountNSFrom indicates that the container should join the mount namespace
887
// of the given container.
888
// If the container has joined a pod, it can only join the namespaces of
889
// containers in the same pod.
890
func WithMountNSFrom(nsCtr *Container) CtrCreateOption {
891
return func(ctr *Container) error {
893
return define.ErrCtrFinalized
896
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
899
ctr.config.MountNsCtr = nsCtr.ID()
905
// WithNetNSFrom indicates that the container should join the network namespace
906
// of the given container.
907
// If the container has joined a pod, it can only join the namespaces of
908
// containers in the same pod.
909
func WithNetNSFrom(nsCtr *Container) CtrCreateOption {
910
return func(ctr *Container) error {
912
return define.ErrCtrFinalized
915
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
919
ctr.config.NetNsCtr = nsCtr.ID()
925
// WithPIDNSFrom indicates that the container should join the PID namespace of
926
// the given container.
927
// If the container has joined a pod, it can only join the namespaces of
928
// containers in the same pod.
929
func WithPIDNSFrom(nsCtr *Container) CtrCreateOption {
930
return func(ctr *Container) error {
932
return define.ErrCtrFinalized
935
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
939
ctr.config.PIDNsCtr = nsCtr.ID()
945
// WithAddCurrentUserPasswdEntry indicates that container should add current
946
// user entry to /etc/passwd, since the UID will be mapped into the container,
948
func WithAddCurrentUserPasswdEntry() CtrCreateOption {
949
return func(ctr *Container) error {
951
return define.ErrCtrFinalized
954
ctr.config.AddCurrentUserPasswdEntry = true
959
// WithUserNSFrom indicates that the container should join the user namespace of
960
// the given container.
961
// If the container has joined a pod, it can only join the namespaces of
962
// containers in the same pod.
963
func WithUserNSFrom(nsCtr *Container) CtrCreateOption {
964
return func(ctr *Container) error {
966
return define.ErrCtrFinalized
969
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
973
ctr.config.UserNsCtr = nsCtr.ID()
974
if err := JSONDeepCopy(nsCtr.IDMappings(), &ctr.config.IDMappings); err != nil {
977
// NewFromSpec() is deprecated according to its comment
978
// however the recommended replace just causes a nil map panic
979
g := generate.NewFromSpec(ctr.config.Spec)
981
g.ClearLinuxUIDMappings()
982
for _, uidmap := range nsCtr.config.IDMappings.UIDMap {
983
g.AddLinuxUIDMapping(uint32(uidmap.HostID), uint32(uidmap.ContainerID), uint32(uidmap.Size))
985
g.ClearLinuxGIDMappings()
986
for _, gidmap := range nsCtr.config.IDMappings.GIDMap {
987
g.AddLinuxGIDMapping(uint32(gidmap.HostID), uint32(gidmap.ContainerID), uint32(gidmap.Size))
993
// WithUTSNSFrom indicates that the container should join the UTS namespace of
994
// the given container.
995
// If the container has joined a pod, it can only join the namespaces of
996
// containers in the same pod.
997
func WithUTSNSFrom(nsCtr *Container) CtrCreateOption {
998
return func(ctr *Container) error {
1000
return define.ErrCtrFinalized
1003
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
1007
ctr.config.UTSNsCtr = nsCtr.ID()
1013
// WithCgroupNSFrom indicates that the container should join the Cgroup namespace
1014
// of the given container.
1015
// If the container has joined a pod, it can only join the namespaces of
1016
// containers in the same pod.
1017
func WithCgroupNSFrom(nsCtr *Container) CtrCreateOption {
1018
return func(ctr *Container) error {
1020
return define.ErrCtrFinalized
1023
if err := checkDependencyContainer(nsCtr, ctr); err != nil {
1027
ctr.config.CgroupNsCtr = nsCtr.ID()
1033
// WithDependencyCtrs sets dependency containers of the given container.
1034
// Dependency containers must be running before this container is started.
1035
func WithDependencyCtrs(ctrs []*Container) CtrCreateOption {
1036
return func(ctr *Container) error {
1038
return define.ErrCtrFinalized
1041
deps := make([]string, 0, len(ctrs))
1043
for _, dep := range ctrs {
1044
if err := checkDependencyContainer(dep, ctr); err != nil {
1048
deps = append(deps, dep.ID())
1051
ctr.config.Dependencies = deps
1057
// WithNetNS indicates that the container should be given a new network
1058
// namespace with a minimal configuration.
1059
// An optional array of port mappings can be provided.
1060
// Conflicts with WithNetNSFrom().
1061
func WithNetNS(portMappings []nettypes.PortMapping, exposedPorts map[uint16][]string, postConfigureNetNS bool, netmode string, networks map[string]nettypes.PerNetworkOptions) CtrCreateOption {
1062
return func(ctr *Container) error {
1064
return define.ErrCtrFinalized
1067
ctr.config.PostConfigureNetNS = postConfigureNetNS
1068
ctr.config.NetMode = namespaces.NetworkMode(netmode)
1069
ctr.config.CreateNetNS = true
1070
ctr.config.PortMappings = portMappings
1071
ctr.config.ExposedPorts = exposedPorts
1073
if !ctr.config.NetMode.IsBridge() && len(networks) > 0 {
1074
return errors.New("cannot use networks when network mode is not bridge")
1076
ctr.config.Networks = networks
1082
// WithNetworkOptions sets additional options for the networks.
1083
func WithNetworkOptions(options map[string][]string) CtrCreateOption {
1084
return func(ctr *Container) error {
1086
return define.ErrCtrFinalized
1089
ctr.config.NetworkOptions = options
1095
// WithLogDriver sets the log driver for the container
1096
func WithLogDriver(driver string) CtrCreateOption {
1097
return func(ctr *Container) error {
1099
return define.ErrCtrFinalized
1103
return fmt.Errorf("log driver must be set: %w", define.ErrInvalidArg)
1104
case define.JournaldLogging, define.KubernetesLogging, define.JSONLogging, define.NoLogging, define.PassthroughLogging, define.PassthroughTTYLogging:
1107
return fmt.Errorf("invalid log driver: %w", define.ErrInvalidArg)
1110
ctr.config.LogDriver = driver
1116
// WithLogPath sets the path to the log file.
1117
func WithLogPath(path string) CtrCreateOption {
1118
return func(ctr *Container) error {
1120
return define.ErrCtrFinalized
1123
return fmt.Errorf("log path must be set: %w", define.ErrInvalidArg)
1126
ctr.config.LogPath = path
1132
// WithLogTag sets the tag to the log file.
1133
func WithLogTag(tag string) CtrCreateOption {
1134
return func(ctr *Container) error {
1136
return define.ErrCtrFinalized
1139
return fmt.Errorf("log tag must be set: %w", define.ErrInvalidArg)
1142
ctr.config.LogTag = tag
1148
// WithCgroupsMode disables the creation of Cgroups for the conmon process.
1149
func WithCgroupsMode(mode string) CtrCreateOption {
1150
return func(ctr *Container) error {
1152
return define.ErrCtrFinalized
1157
ctr.config.NoCgroups = true
1158
ctr.config.CgroupsMode = mode
1159
case "enabled", "no-conmon", cgroupSplit:
1160
ctr.config.CgroupsMode = mode
1162
return fmt.Errorf("invalid cgroup mode %q: %w", mode, define.ErrInvalidArg)
1169
// WithCgroupParent sets the Cgroup Parent of the new container.
1170
func WithCgroupParent(parent string) CtrCreateOption {
1171
return func(ctr *Container) error {
1173
return define.ErrCtrFinalized
1177
return fmt.Errorf("cgroup parent cannot be empty: %w", define.ErrInvalidArg)
1180
ctr.config.CgroupParent = parent
1186
// WithDNSSearch sets the additional search domains of a container.
1187
func WithDNSSearch(searchDomains []string) CtrCreateOption {
1188
return func(ctr *Container) error {
1190
return define.ErrCtrFinalized
1192
ctr.config.DNSSearch = searchDomains
1197
// WithDNS sets additional name servers for the container.
1198
func WithDNS(dnsServers []string) CtrCreateOption {
1199
return func(ctr *Container) error {
1201
return define.ErrCtrFinalized
1204
for _, i := range dnsServers {
1205
result := net.ParseIP(i)
1207
return fmt.Errorf("invalid IP address %s: %w", i, define.ErrInvalidArg)
1209
dns = append(dns, result)
1211
ctr.config.DNSServer = append(ctr.config.DNSServer, dns...)
1217
// WithDNSOption sets additional dns options for the container.
1218
func WithDNSOption(dnsOptions []string) CtrCreateOption {
1219
return func(ctr *Container) error {
1221
return define.ErrCtrFinalized
1223
if ctr.config.UseImageResolvConf {
1224
return fmt.Errorf("cannot add DNS options if container will not create /etc/resolv.conf: %w", define.ErrInvalidArg)
1226
ctr.config.DNSOption = append(ctr.config.DNSOption, dnsOptions...)
1231
// WithHosts sets additional host:IP for the hosts file.
1232
func WithHosts(hosts []string) CtrCreateOption {
1233
return func(ctr *Container) error {
1235
return define.ErrCtrFinalized
1238
ctr.config.HostAdd = hosts
1243
// WithConmonPidFile specifies the path to the file that receives the pid of
1245
func WithConmonPidFile(path string) CtrCreateOption {
1246
return func(ctr *Container) error {
1248
return define.ErrCtrFinalized
1250
ctr.config.ConmonPidFile = path
1255
// WithGroups sets additional groups for the container, which are defined by
1257
func WithGroups(groups []string) CtrCreateOption {
1258
return func(ctr *Container) error {
1260
return define.ErrCtrFinalized
1262
ctr.config.Groups = groups
1267
// WithUserVolumes sets the user-added volumes of the container.
1268
// These are not added to the container's spec, but will instead be used during
1269
// commit to populate the volumes of the new image, and to trigger some OCI
1270
// hooks that are only added if volume mounts are present.
1271
// Furthermore, they are used in the output of inspect, to filter volumes -
1272
// only volumes included in this list will be included in the output.
1273
// Unless explicitly set, committed images will have no volumes.
1274
// The given volumes slice must not be nil.
1275
func WithUserVolumes(volumes []string) CtrCreateOption {
1276
return func(ctr *Container) error {
1278
return define.ErrCtrFinalized
1282
return define.ErrInvalidArg
1285
ctr.config.UserVolumes = make([]string, 0, len(volumes))
1286
ctr.config.UserVolumes = append(ctr.config.UserVolumes, volumes...)
1291
// WithEntrypoint sets the entrypoint of the container.
1292
// This is not used to change the container's spec, but will instead be used
1293
// during commit to populate the entrypoint of the new image.
1294
// If not explicitly set it will default to the image's entrypoint.
1295
// A nil entrypoint is allowed, and will clear entrypoint on the created image.
1296
func WithEntrypoint(entrypoint []string) CtrCreateOption {
1297
return func(ctr *Container) error {
1299
return define.ErrCtrFinalized
1302
ctr.config.Entrypoint = make([]string, 0, len(entrypoint))
1303
ctr.config.Entrypoint = append(ctr.config.Entrypoint, entrypoint...)
1308
// WithCommand sets the command of the container.
1309
// This is not used to change the container's spec, but will instead be used
1310
// during commit to populate the command of the new image.
1311
// If not explicitly set it will default to the image's command.
1312
// A nil command is allowed, and will clear command on the created image.
1313
func WithCommand(command []string) CtrCreateOption {
1314
return func(ctr *Container) error {
1316
return define.ErrCtrFinalized
1319
ctr.config.Command = make([]string, 0, len(command))
1320
ctr.config.Command = append(ctr.config.Command, command...)
1325
// WithRootFS sets the rootfs for the container.
1326
// This creates a container from a directory on disk and not an image.
1327
func WithRootFS(rootfs string, overlay bool, mapping *string) CtrCreateOption {
1328
return func(ctr *Container) error {
1330
return define.ErrCtrFinalized
1332
if err := fileutils.Exists(rootfs); err != nil {
1335
ctr.config.Rootfs = rootfs
1336
ctr.config.RootfsOverlay = overlay
1337
ctr.config.RootfsMapping = mapping
1342
// WithCtrNamespace sets the namespace the container will be created in.
1343
// Namespaces are used to create separate views of Podman's state - runtimes can
1344
// join a specific namespace and see only containers and pods in that namespace.
1345
// Empty string namespaces are allowed, and correspond to a lack of namespace.
1346
func WithCtrNamespace(ns string) CtrCreateOption {
1347
return func(ctr *Container) error {
1349
return define.ErrCtrFinalized
1352
ctr.config.Namespace = ns
1358
// WithUseImageResolvConf tells the container not to bind-mount resolv.conf in.
1359
// This conflicts with other DNS-related options.
1360
func WithUseImageResolvConf() CtrCreateOption {
1361
return func(ctr *Container) error {
1363
return define.ErrCtrFinalized
1366
ctr.config.UseImageResolvConf = true
1372
// WithUseImageHosts tells the container not to bind-mount /etc/hosts in.
1373
// This conflicts with WithHosts().
1374
func WithUseImageHosts() CtrCreateOption {
1375
return func(ctr *Container) error {
1377
return define.ErrCtrFinalized
1380
ctr.config.UseImageHosts = true
1386
// WithRestartPolicy sets the container's restart policy. Valid values are
1387
// "no", "on-failure", and "always". The empty string is allowed, and will be
1388
// equivalent to "no".
1389
func WithRestartPolicy(policy string) CtrCreateOption {
1390
return func(ctr *Container) error {
1392
return define.ErrCtrFinalized
1395
if err := define.ValidateRestartPolicy(policy); err != nil {
1399
ctr.config.RestartPolicy = policy
1405
// WithRestartRetries sets the number of retries to use when restarting a
1406
// container with the "on-failure" restart policy.
1407
// 0 is an allowed value, and indicates infinite retries.
1408
func WithRestartRetries(tries uint) CtrCreateOption {
1409
return func(ctr *Container) error {
1411
return define.ErrCtrFinalized
1414
ctr.config.RestartRetries = tries
1420
// WithNamedVolumes adds the given named volumes to the container.
1421
func WithNamedVolumes(volumes []*ContainerNamedVolume) CtrCreateOption {
1422
return func(ctr *Container) error {
1424
return define.ErrCtrFinalized
1427
for _, vol := range volumes {
1428
mountOpts, err := util.ProcessOptions(vol.Options, false, "")
1430
return fmt.Errorf("processing options for named volume %q mounted at %q: %w", vol.Name, vol.Dest, err)
1433
ctr.config.NamedVolumes = append(ctr.config.NamedVolumes, &ContainerNamedVolume{
1437
IsAnonymous: vol.IsAnonymous,
1438
SubPath: vol.SubPath,
1446
// WithOverlayVolumes adds the given overlay volumes to the container.
1447
func WithOverlayVolumes(volumes []*ContainerOverlayVolume) CtrCreateOption {
1448
return func(ctr *Container) error {
1450
return define.ErrCtrFinalized
1453
for _, vol := range volumes {
1454
ctr.config.OverlayVolumes = append(ctr.config.OverlayVolumes, &ContainerOverlayVolume{
1457
Options: vol.Options,
1465
// WithImageVolumes adds the given image volumes to the container.
1466
func WithImageVolumes(volumes []*ContainerImageVolume) CtrCreateOption {
1467
return func(ctr *Container) error {
1469
return define.ErrCtrFinalized
1472
for _, vol := range volumes {
1473
ctr.config.ImageVolumes = append(ctr.config.ImageVolumes, &ContainerImageVolume{
1476
ReadWrite: vol.ReadWrite,
1484
// WithHealthCheck adds the healthcheck to the container config
1485
func WithHealthCheck(healthCheck *manifest.Schema2HealthConfig) CtrCreateOption {
1486
return func(ctr *Container) error {
1488
return define.ErrCtrFinalized
1490
ctr.config.HealthCheckConfig = healthCheck
1495
// WithHealthCheckOnFailureAction adds an on-failure action to health-check config
1496
func WithHealthCheckOnFailureAction(action define.HealthCheckOnFailureAction) CtrCreateOption {
1497
return func(ctr *Container) error {
1499
return define.ErrCtrFinalized
1501
ctr.config.HealthCheckOnFailureAction = action
1506
// WithPreserveFDs forwards from the process running Libpod into the container
1507
// the given number of extra FDs (starting after the standard streams) to the created container
1508
func WithPreserveFDs(fd uint) CtrCreateOption {
1509
return func(ctr *Container) error {
1511
return define.ErrCtrFinalized
1513
ctr.config.PreserveFDs = fd
1518
// WithPreserveFD forwards from the process running Libpod into the container
1519
// the given list of extra FDs to the created container
1520
func WithPreserveFD(fds []uint) CtrCreateOption {
1521
return func(ctr *Container) error {
1523
return define.ErrCtrFinalized
1525
ctr.config.PreserveFD = fds
1530
// WithCreateCommand adds the full command plus arguments of the current
1531
// process to the container config.
1532
func WithCreateCommand(cmd []string) CtrCreateOption {
1533
return func(ctr *Container) error {
1535
return define.ErrCtrFinalized
1537
ctr.config.CreateCommand = cmd
1542
// withIsInfra allows us to differentiate between infra containers and other containers
1543
// within the container config
1544
func withIsInfra() CtrCreateOption {
1545
return func(ctr *Container) error {
1547
return define.ErrCtrFinalized
1550
ctr.config.IsInfra = true
1556
// WithIsService allows us to differentiate between service containers and other container
1557
// within the container config. It also sets the exit-code propagation of the
1558
// service container.
1559
func WithIsService(ecp define.KubeExitCodePropagation) CtrCreateOption {
1560
return func(ctr *Container) error {
1562
return define.ErrCtrFinalized
1565
ctr.config.IsService = true
1566
ctr.config.KubeExitCodePropagation = ecp
1571
// WithCreateWorkingDir tells Podman to create the container's working directory
1572
// if it does not exist.
1573
func WithCreateWorkingDir() CtrCreateOption {
1574
return func(ctr *Container) error {
1576
return define.ErrCtrFinalized
1579
ctr.config.CreateWorkingDir = true
1584
// Volume Creation Options
1586
func WithVolumeIgnoreIfExist() VolumeCreateOption {
1587
return func(volume *Volume) error {
1589
return define.ErrVolumeFinalized
1591
volume.ignoreIfExists = true
1597
// WithVolumeName sets the name of the volume.
1598
func WithVolumeName(name string) VolumeCreateOption {
1599
return func(volume *Volume) error {
1601
return define.ErrVolumeFinalized
1604
// Check the name against a regex
1605
if !define.NameRegex.MatchString(name) {
1606
return define.RegexError
1608
volume.config.Name = name
1614
// WithVolumeDriver sets the volume's driver.
1615
// It is presently not implemented, but will be supported in a future Podman
1617
func WithVolumeDriver(driver string) VolumeCreateOption {
1618
return func(volume *Volume) error {
1620
return define.ErrVolumeFinalized
1623
volume.config.Driver = driver
1628
// WithVolumeLabels sets the labels of the volume.
1629
func WithVolumeLabels(labels map[string]string) VolumeCreateOption {
1630
return func(volume *Volume) error {
1632
return define.ErrVolumeFinalized
1635
volume.config.Labels = make(map[string]string)
1636
for key, value := range labels {
1637
volume.config.Labels[key] = value
1644
// WithVolumeMountLabel sets the MountLabel of the volume.
1645
func WithVolumeMountLabel(mountLabel string) VolumeCreateOption {
1646
return func(volume *Volume) error {
1648
return define.ErrVolumeFinalized
1651
volume.config.MountLabel = mountLabel
1656
// WithVolumeOptions sets the options of the volume.
1657
func WithVolumeOptions(options map[string]string) VolumeCreateOption {
1658
return func(volume *Volume) error {
1660
return define.ErrVolumeFinalized
1663
volume.config.Options = make(map[string]string)
1664
for key, value := range options {
1665
volume.config.Options[key] = value
1672
// WithVolumeUID sets the UID that the volume will be created as.
1673
func WithVolumeUID(uid int) VolumeCreateOption {
1674
return func(volume *Volume) error {
1676
return define.ErrVolumeFinalized
1679
volume.config.UID = uid
1685
// WithVolumeSize sets the maximum size of the volume
1686
func WithVolumeSize(size uint64) VolumeCreateOption {
1687
return func(volume *Volume) error {
1689
return define.ErrVolumeFinalized
1692
volume.config.Size = size
1698
// WithVolumeInodes sets the maximum inodes of the volume
1699
func WithVolumeInodes(inodes uint64) VolumeCreateOption {
1700
return func(volume *Volume) error {
1702
return define.ErrVolumeFinalized
1705
volume.config.Inodes = inodes
1711
// WithVolumeGID sets the GID that the volume will be created as.
1712
func WithVolumeGID(gid int) VolumeCreateOption {
1713
return func(volume *Volume) error {
1715
return define.ErrVolumeFinalized
1718
volume.config.GID = gid
1724
// WithVolumeNoChown prevents the volume from being chowned to the process uid at first use.
1725
func WithVolumeNoChown() VolumeCreateOption {
1726
return func(volume *Volume) error {
1728
return define.ErrVolumeFinalized
1731
volume.state.NeedsChown = false
1737
// WithVolumeDisableQuota prevents the volume from being assigned a quota.
1738
func WithVolumeDisableQuota() VolumeCreateOption {
1739
return func(volume *Volume) error {
1741
return define.ErrVolumeFinalized
1744
volume.config.DisableQuota = true
1750
// withSetAnon sets a bool notifying libpod that this volume is anonymous and
1751
// should be removed when containers using it are removed and volumes are
1752
// specified for removal.
1753
func withSetAnon() VolumeCreateOption {
1754
return func(volume *Volume) error {
1756
return define.ErrVolumeFinalized
1759
volume.config.IsAnon = true
1765
// WithVolumeDriverTimeout sets the volume creation timeout period.
1766
// Only usable if a non-local volume driver is in use.
1767
func WithVolumeDriverTimeout(timeout uint) VolumeCreateOption {
1768
return func(volume *Volume) error {
1770
return define.ErrVolumeFinalized
1773
if volume.config.Driver == "" || volume.config.Driver == define.VolumeDriverLocal {
1774
return fmt.Errorf("Volume driver timeout can only be used with non-local volume drivers: %w", define.ErrInvalidArg)
1779
volume.config.Timeout = &tm
1785
// WithTimezone sets the timezone in the container
1786
func WithTimezone(path string) CtrCreateOption {
1787
return func(ctr *Container) error {
1789
return define.ErrCtrFinalized
1791
if path != "local" {
1792
// validate the format of the timezone specified if it's not "local"
1793
_, err := time.LoadLocation(path)
1795
return fmt.Errorf("finding timezone: %w", err)
1799
ctr.config.Timezone = path
1804
// WithUmask sets the umask in the container
1805
func WithUmask(umask string) CtrCreateOption {
1806
return func(ctr *Container) error {
1808
return define.ErrCtrFinalized
1810
if !umaskRegex.MatchString(umask) {
1811
return fmt.Errorf("invalid umask string %s: %w", umask, define.ErrInvalidArg)
1813
ctr.config.Umask = umask
1818
// WithSecrets adds secrets to the container
1819
func WithSecrets(containerSecrets []*ContainerSecret) CtrCreateOption {
1820
return func(ctr *Container) error {
1822
return define.ErrCtrFinalized
1824
ctr.config.Secrets = containerSecrets
1829
// WithEnvSecrets adds environment variable secrets to the container
1830
func WithEnvSecrets(envSecrets map[string]string) CtrCreateOption {
1831
return func(ctr *Container) error {
1832
ctr.config.EnvSecrets = make(map[string]*secrets.Secret)
1834
return define.ErrCtrFinalized
1836
manager, err := ctr.runtime.SecretsManager()
1840
for target, src := range envSecrets {
1841
secr, err := manager.Lookup(src)
1845
ctr.config.EnvSecrets[target] = secr
1851
// WithPidFile adds pidFile to the container
1852
func WithPidFile(pidFile string) CtrCreateOption {
1853
return func(ctr *Container) error {
1855
return define.ErrCtrFinalized
1857
ctr.config.PidFile = pidFile
1862
// WithHostUsers indicates host users to add to /etc/passwd
1863
func WithHostUsers(hostUsers []string) CtrCreateOption {
1864
return func(ctr *Container) error {
1866
return define.ErrCtrFinalized
1868
ctr.config.HostUsers = hostUsers
1873
// WithInitCtrType indicates the container is an initcontainer
1874
func WithInitCtrType(containerType string) CtrCreateOption {
1875
return func(ctr *Container) error {
1877
return define.ErrCtrFinalized
1879
// Make sure the type is valid
1880
if containerType == define.OneShotInitContainer || containerType == define.AlwaysInitContainer {
1881
ctr.config.InitContainerType = containerType
1884
return fmt.Errorf("%s is invalid init container type", containerType)
1888
// WithHostDevice adds the original host src to the config
1889
func WithHostDevice(dev []specs.LinuxDevice) CtrCreateOption {
1890
return func(ctr *Container) error {
1892
return define.ErrCtrFinalized
1894
ctr.config.DeviceHostSrc = dev
1899
// WithSelectedPasswordManagement makes it so that the container either does or does not set up /etc/passwd or /etc/group
1900
func WithSelectedPasswordManagement(passwd *bool) CtrCreateOption {
1901
return func(c *Container) error {
1903
return define.ErrCtrFinalized
1905
c.config.Passwd = passwd
1910
// WithInfraConfig allows for inheritance of compatible config entities from the infra container
1911
func WithInfraConfig(compatibleOptions InfraInherit) CtrCreateOption {
1912
return func(ctr *Container) error {
1914
return define.ErrCtrFinalized
1916
compatMarshal, err := json.Marshal(compatibleOptions)
1918
return errors.New("could not marshal compatible options")
1921
err = json.Unmarshal(compatMarshal, ctr.config)
1923
return errors.New("could not unmarshal compatible options into container config")
1929
// WithStartupHealthcheck sets a startup healthcheck for the container.
1930
// Requires that a healthcheck must be set.
1931
func WithStartupHealthcheck(startupHC *define.StartupHealthCheck) CtrCreateOption {
1932
return func(ctr *Container) error {
1934
return define.ErrCtrFinalized
1936
ctr.config.StartupHealthCheckConfig = new(define.StartupHealthCheck)
1937
if err := JSONDeepCopy(startupHC, ctr.config.StartupHealthCheckConfig); err != nil {
1938
return fmt.Errorf("error copying startup healthcheck into container: %w", err)
1944
// Pod Creation Options
1946
// WithPodCreateCommand adds the full command plus arguments of the current
1947
// process to the pod config.
1948
func WithPodCreateCommand(createCmd []string) PodCreateOption {
1949
return func(pod *Pod) error {
1951
return define.ErrPodFinalized
1953
pod.config.CreateCommand = createCmd
1958
// WithPodName sets the name of the pod.
1959
func WithPodName(name string) PodCreateOption {
1960
return func(pod *Pod) error {
1962
return define.ErrPodFinalized
1965
// Check the name against a regex
1966
if !define.NameRegex.MatchString(name) {
1967
return define.RegexError
1970
pod.config.Name = name
1976
// WithPodExitPolicy sets the exit policy of the pod.
1977
func WithPodExitPolicy(policy string) PodCreateOption {
1978
return func(pod *Pod) error {
1980
return define.ErrPodFinalized
1983
parsed, err := config.ParsePodExitPolicy(policy)
1988
pod.config.ExitPolicy = parsed
1994
// WithPodRestartPolicy sets the restart policy of the pod.
1995
func WithPodRestartPolicy(policy string) PodCreateOption {
1996
return func(pod *Pod) error {
1998
return define.ErrPodFinalized
2002
//TODO: v5.0 if no restart policy is set, follow k8s convention and default to Always
2003
case define.RestartPolicyNone, define.RestartPolicyNo, define.RestartPolicyOnFailure, define.RestartPolicyAlways, define.RestartPolicyUnlessStopped:
2004
pod.config.RestartPolicy = policy
2006
return fmt.Errorf("%q is not a valid restart policy: %w", policy, define.ErrInvalidArg)
2013
// WithPodRestartRetries sets the number of retries to use when restarting a
2014
// container with the "on-failure" restart policy.
2015
// 0 is an allowed value, and indicates infinite retries.
2016
func WithPodRestartRetries(tries uint) PodCreateOption {
2017
return func(pod *Pod) error {
2019
return define.ErrPodFinalized
2022
pod.config.RestartRetries = &tries
2028
// WithPodHostname sets the hostname of the pod.
2029
func WithPodHostname(hostname string) PodCreateOption {
2030
return func(pod *Pod) error {
2032
return define.ErrPodFinalized
2035
// Check the hostname against a regex
2036
if !define.NameRegex.MatchString(hostname) {
2037
return define.RegexError
2040
pod.config.Hostname = hostname
2046
// WithInfraConmonPidFile sets the path to a custom conmon PID file for the
2048
func WithInfraConmonPidFile(path string, infraSpec *specgen.SpecGenerator) PodCreateOption {
2049
return func(pod *Pod) error {
2051
return define.ErrPodFinalized
2053
infraSpec.ConmonPidFile = path
2058
// WithPodLabels sets the labels of a pod.
2059
func WithPodLabels(labels map[string]string) PodCreateOption {
2060
return func(pod *Pod) error {
2062
return define.ErrPodFinalized
2065
pod.config.Labels = make(map[string]string)
2066
for key, value := range labels {
2067
pod.config.Labels[key] = value
2074
// WithPodCgroupParent sets the Cgroup Parent of the pod.
2075
func WithPodCgroupParent(path string) PodCreateOption {
2076
return func(pod *Pod) error {
2078
return define.ErrPodFinalized
2081
pod.config.CgroupParent = path
2087
// WithPodParent tells containers in this pod to use the cgroup created for
2089
// This can still be overridden at the container level by explicitly specifying
2091
func WithPodParent() PodCreateOption {
2092
return func(pod *Pod) error {
2094
return define.ErrPodFinalized
2097
pod.config.UsePodCgroup = true
2103
// WithPodNamespace sets the namespace for the created pod.
2104
// Namespaces are used to create separate views of Podman's state - runtimes can
2105
// join a specific namespace and see only containers and pods in that namespace.
2106
// Empty string namespaces are allowed, and correspond to a lack of namespace.
2107
// Containers must belong to the same namespace as the pod they join.
2108
func WithPodNamespace(ns string) PodCreateOption {
2109
return func(pod *Pod) error {
2111
return define.ErrPodFinalized
2114
pod.config.Namespace = ns
2120
// WithPodIPC tells containers in this pod to use the ipc namespace
2121
// created for this pod.
2122
// Containers in a pod will inherit the kernel namespaces from the
2123
// first container added.
2124
func WithPodIPC() PodCreateOption {
2125
return func(pod *Pod) error {
2127
return define.ErrPodFinalized
2130
pod.config.UsePodIPC = true
2136
// WithPodNet tells containers in this pod to use the network namespace
2137
// created for this pod.
2138
// Containers in a pod will inherit the kernel namespaces from the
2139
// first container added.
2140
func WithPodNet() PodCreateOption {
2141
return func(pod *Pod) error {
2143
return define.ErrPodFinalized
2146
pod.config.UsePodNet = true
2152
// WithPodMount tells containers in this pod to use the mount namespace
2153
// created for this pod.
2154
// Containers in a pod will inherit the kernel namespaces from the
2155
// first container added.
2156
// TODO implement WithMountNSFrom, so WithMountNsFromPod functions properly
2157
// Then this option can be added on the pod level
2158
func WithPodMount() PodCreateOption {
2159
return func(pod *Pod) error {
2161
return define.ErrPodFinalized
2164
pod.config.UsePodMount = true
2170
// WithPodUser tells containers in this pod to use the user namespace
2171
// created for this pod.
2172
// Containers in a pod will inherit the kernel namespaces from the
2173
// first container added.
2174
// TODO implement WithUserNSFrom, so WithUserNsFromPod functions properly
2175
// Then this option can be added on the pod level
2176
func WithPodUser() PodCreateOption {
2177
return func(pod *Pod) error {
2179
return define.ErrPodFinalized
2182
pod.config.UsePodUser = true
2188
// WithPodPID tells containers in this pod to use the pid namespace
2189
// created for this pod.
2190
// Containers in a pod will inherit the kernel namespaces from the
2191
// first container added.
2192
func WithPodPID() PodCreateOption {
2193
return func(pod *Pod) error {
2195
return define.ErrPodFinalized
2198
pod.config.UsePodPID = true
2204
// WithPodUTS tells containers in this pod to use the uts namespace
2205
// created for this pod.
2206
// Containers in a pod will inherit the kernel namespaces from the
2207
// first container added.
2208
func WithPodUTS() PodCreateOption {
2209
return func(pod *Pod) error {
2211
return define.ErrPodFinalized
2214
pod.config.UsePodUTS = true
2220
// WithPodCgroup tells containers in this pod to use the cgroup namespace
2221
// created for this pod.
2222
// Containers in a pod will inherit the kernel namespaces from the first
2224
func WithPodCgroup() PodCreateOption {
2225
return func(pod *Pod) error {
2227
return define.ErrPodFinalized
2230
pod.config.UsePodCgroupNS = true
2236
// WithInfraContainer tells the pod to create a pause container
2237
func WithInfraContainer() PodCreateOption {
2238
return func(pod *Pod) error {
2240
return define.ErrPodFinalized
2242
pod.config.HasInfra = true
2248
// WithServiceContainer associates the specified service container ID with the pod.
2249
func WithServiceContainer(id string) PodCreateOption {
2250
return func(pod *Pod) error {
2252
return define.ErrPodFinalized
2255
ctr, err := pod.runtime.LookupContainer(id)
2257
return fmt.Errorf("looking up service container: %w", err)
2260
if err := ctr.addServicePodLocked(pod.ID()); err != nil {
2261
return fmt.Errorf("associating service container %s with pod %s: %w", id, pod.ID(), err)
2264
pod.config.ServiceContainerID = id
2269
// WithPodResources sets resource limits to be applied to the pod's cgroup
2270
// these will be inherited by all containers unless overridden.
2271
func WithPodResources(resources specs.LinuxResources) PodCreateOption {
2272
return func(pod *Pod) error {
2274
return define.ErrPodFinalized
2276
pod.config.ResourceLimits = resources
2281
// WithVolatile sets the volatile flag for the container storage.
2282
// The option can potentially cause data loss when used on a container that must survive a machine reboot.
2283
func WithVolatile() CtrCreateOption {
2284
return func(ctr *Container) error {
2286
return define.ErrCtrFinalized
2289
ctr.config.Volatile = true
2295
// WithChrootDirs is an additional set of directories that need to be
2296
// treated as root directories. Standard bind mounts will be mounted
2297
// into paths relative to these directories.
2298
func WithChrootDirs(dirs []string) CtrCreateOption {
2299
return func(ctr *Container) error {
2301
return define.ErrCtrFinalized
2304
ctr.config.ChrootDirs = dirs
2310
// WithPasswdEntry sets the entry to write to the /etc/passwd file.
2311
func WithPasswdEntry(passwdEntry string) CtrCreateOption {
2312
return func(ctr *Container) error {
2314
return define.ErrCtrFinalized
2317
ctr.config.PasswdEntry = passwdEntry
2323
// WithGroupEntry sets the entry to write to the /etc/group file.
2324
func WithGroupEntry(groupEntry string) CtrCreateOption {
2325
return func(ctr *Container) error {
2327
return define.ErrCtrFinalized
2330
ctr.config.GroupEntry = groupEntry
2336
// WithBaseHostsFile sets the option to copy /etc/hosts file.
2337
func WithBaseHostsFile(baseHostsFile string) CtrCreateOption {
2338
return func(ctr *Container) error {
2340
return define.ErrCtrFinalized
2343
ctr.config.BaseHostsFile = baseHostsFile
2349
// WithMountAllDevices sets the option to mount all of a privileged container's
2351
func WithMountAllDevices() CtrCreateOption {
2352
return func(ctr *Container) error {
2354
return define.ErrCtrFinalized
2357
ctr.config.MountAllDevices = true
2363
// WithLabelNested sets the LabelNested flag allowing label separation within container
2364
func WithLabelNested(nested bool) CtrCreateOption {
2365
return func(ctr *Container) error {
2367
return define.ErrCtrFinalized
2370
ctr.config.LabelNested = nested