diff options
Diffstat (limited to 'libpod/options.go')
-rw-r--r-- | libpod/options.go | 260 |
1 files changed, 226 insertions, 34 deletions
diff --git a/libpod/options.go b/libpod/options.go index 8a9cf94b6..f82cb20c4 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -1,7 +1,7 @@ package libpod import ( - "fmt" + "net" "path/filepath" "regexp" "syscall" @@ -13,27 +13,9 @@ import ( ) var ( - ctrNotImplemented = func(c *Container) error { - return fmt.Errorf("NOT IMPLEMENTED") - } nameRegex = regexp.MustCompile("[a-zA-Z0-9_-]+") ) -const ( - // IPCNamespace represents the IPC namespace - IPCNamespace = "ipc" - // MountNamespace represents the mount namespace - MountNamespace = "mount" - // NetNamespace represents the network namespace - NetNamespace = "network" - // PIDNamespace represents the PID namespace - PIDNamespace = "pid" - // UserNamespace represents the user namespace - UserNamespace = "user" - // UTSNamespace represents the UTS namespace - UTSNamespace = "uts" -) - // Runtime Creation Options // WithStorageConfig uses the given configuration to set up container storage @@ -100,15 +82,21 @@ func WithSignaturePolicy(path string) RuntimeOption { } } -// WithInMemoryState specifies that the runtime will be backed by an in-memory -// state only, and state will not persist after the runtime is shut down -func WithInMemoryState() RuntimeOption { +// WithStateType sets the backing state implementation for libpod +// Please note that information is not portable between backing states +// As such, if this differs between two libpods running on the same system, +// they will not share containers, and unspecified behavior may occur +func WithStateType(storeType RuntimeStateStore) RuntimeOption { return func(rt *Runtime) error { if rt.valid { return ErrRuntimeFinalized } - rt.config.InMemoryState = true + if storeType == InvalidStateStore { + return errors.Wrapf(ErrInvalidArg, "must provide a valid state store type") + } + + rt.config.StateType = storeType return nil } @@ -341,15 +329,6 @@ func WithStdin() CtrCreateOption { } } -// WithSharedNamespaces sets a container to share namespaces with another -// container. If the from container belongs to a pod, the new container will -// be added to the pod. -// By default no namespaces are shared. To share a namespace, add the Namespace -// string constant to the map as a key -func WithSharedNamespaces(from *Container, namespaces map[string]string) CtrCreateOption { - return ctrNotImplemented -} - // WithPod adds the container to a pod func (r *Runtime) WithPod(pod *Pod) CtrCreateOption { return func(ctr *Container) error { @@ -362,7 +341,6 @@ func (r *Runtime) WithPod(pod *Pod) CtrCreateOption { } ctr.config.Pod = pod.ID() - ctr.pod = pod return nil } @@ -434,6 +412,164 @@ func WithStopTimeout(timeout uint) CtrCreateOption { } } +// WithIPCNSFrom indicates the the container should join the IPC namespace of +// the given container +func WithIPCNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.IPCNsCtr = nsCtr.ID() + + return nil + } +} + +// WithMountNSFrom indicates the the container should join the mount namespace +// of the given container +func WithMountNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.MountNsCtr = nsCtr.ID() + + return nil + } +} + +// WithNetNSFrom indicates the the container should join the network namespace +// of the given container +func WithNetNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + if ctr.config.CreateNetNS { + return errors.Wrapf(ErrInvalidArg, "cannot join another container's net ns as we are making a new net ns") + } + + ctr.config.NetNsCtr = nsCtr.ID() + + return nil + } +} + +// WithPIDNSFrom indicates the the container should join the PID namespace of +// the given container +func WithPIDNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.PIDNsCtr = nsCtr.ID() + + return nil + } +} + +// WithUserNSFrom indicates the the container should join the user namespace of +// the given container +func WithUserNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.UserNsCtr = nsCtr.ID() + + return nil + } +} + +// WithUTSNSFrom indicates the the container should join the UTS namespace of +// the given container +func WithUTSNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.UTSNsCtr = nsCtr.ID() + + return nil + } +} + +// WithCgroupNSFrom indicates the the container should join the CGroup namespace +// of the given container +func WithCgroupNSFrom(nsCtr *Container) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if !nsCtr.valid { + return ErrCtrRemoved + } + + if nsCtr.ID() == ctr.ID() { + return errors.Wrapf(ErrInvalidArg, "must specify another container") + } + + ctr.config.CgroupNsCtr = nsCtr.ID() + + return nil + } +} + // WithNetNS indicates that the container should be given a new network // namespace with a minimal configuration // An optional array of port mappings can be provided @@ -443,8 +579,12 @@ func WithNetNS(portMappings []ocicni.PortMapping) CtrCreateOption { return ErrCtrFinalized } + if ctr.config.NetNsCtr != "" { + return errors.Wrapf(ErrInvalidArg, "container is already set to join another container's net ns, cannot create a new net ns") + } + ctr.config.CreateNetNS = true - copy(ctr.config.PortMappings, portMappings) + ctr.config.PortMappings = portMappings return nil } @@ -502,3 +642,55 @@ func WithPodLabels(labels map[string]string) PodCreateOption { return nil } } + +// WithDNSSearch sets the additional search domains of a container +func WithDNSSearch(searchDomains []string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + ctr.config.DNSSearch = searchDomains + return nil + } +} + +// WithDNS sets additional name servers for the container +func WithDNS(dnsServers []string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + var dns []net.IP + for _, i := range dnsServers { + result := net.ParseIP(i) + if result == nil { + return errors.Wrapf(ErrInvalidArg, "invalid IP address %s", i) + } + dns = append(dns, result) + } + ctr.config.DNSServer = dns + return nil + } +} + +// WithDNSOption sets addition dns options for the container +func WithDNSOption(dnsOptions []string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + ctr.config.DNSOption = dnsOptions + return nil + } +} + +// WithHosts sets additional host:IP for the hosts file +func WithHosts(hosts []string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + ctr.config.HostAdd = hosts + return nil + } +} |