summaryrefslogtreecommitdiff
path: root/libpod/options.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@pm.me>2020-02-03 11:17:32 -0500
committerMatthew Heon <mheon@redhat.com>2020-02-04 10:07:14 -0500
commit07a8ab09e05052e6776395ba3b0275d7f9921ef2 (patch)
tree24bc3a17d337aae463b74421532c78132921fc50 /libpod/options.go
parent28eb296653f78f462c07e3fe6b156bc093ff44c6 (diff)
downloadpodman-07a8ab09e05052e6776395ba3b0275d7f9921ef2.tar.gz
podman-07a8ab09e05052e6776395ba3b0275d7f9921ef2.tar.bz2
podman-07a8ab09e05052e6776395ba3b0275d7f9921ef2.zip
Add backend code for pod network options
This adds network-related options to the pod in the database. We are going to add the CLI frontend in further patches. In short, this should greatly improve the ability of pods to configure networking, once the CLI parsing is added. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'libpod/options.go')
-rw-r--r--libpod/options.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/libpod/options.go b/libpod/options.go
index 923e7292c..4957f822d 100644
--- a/libpod/options.go
+++ b/libpod/options.go
@@ -1784,3 +1784,156 @@ func WithInfraContainerPorts(bindings []ocicni.PortMapping) PodCreateOption {
return nil
}
}
+
+// WithPodStaticIP sets a static IP for the pod.
+func WithPodStaticIP(ip net.IP) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if len(pod.config.InfraContainer.Networks) > 1 {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot set a static IP if joining more than 1 CNI network")
+ }
+
+ pod.config.InfraContainer.StaticIP = ip
+
+ return nil
+ }
+}
+
+// WithPodStaticMAC sets a static MAC address for the pod.
+func WithPodStaticMAC(mac net.HardwareAddr) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if len(pod.config.InfraContainer.Networks) > 1 {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot set a static MAC if joining more than 1 CNI network")
+ }
+
+ pod.config.InfraContainer.StaticMAC = mac
+
+ return nil
+ }
+}
+
+// WithPodUseImageResolvConf sets a pod to use an image's resolv.conf and not
+// create its own.
+func WithPodUseImageResolvConf() PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if len(pod.config.InfraContainer.DNSServer) != 0 ||
+ len(pod.config.InfraContainer.DNSSearch) != 0 ||
+ len(pod.config.InfraContainer.DNSOption) != 0 {
+ return errors.Wrapf(define.ErrInvalidArg, "requested use of image resolv.conf conflicts with already-configured DNS settings")
+ }
+
+ pod.config.InfraContainer.UseImageResolvConf = true
+
+ return nil
+ }
+}
+
+// WithPodDNS sets the DNS Servers for a pod.
+func WithPodDNS(dnsServer []string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if pod.config.InfraContainer.UseImageResolvConf {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot add DNS servers if pod will not create /etc/resolv.conf")
+ }
+
+ pod.config.InfraContainer.DNSServer = dnsServer
+
+ return nil
+ }
+}
+
+// WithPodDNSSearch sets the DNS Search domains for a pod.
+func WithPodDNSSearch(dnsSearch []string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if pod.config.InfraContainer.UseImageResolvConf {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot add DNS search domains if pod will not create /etc/resolv.conf")
+ }
+
+ pod.config.InfraContainer.DNSSearch = dnsSearch
+
+ return nil
+ }
+}
+
+// WithPodDNSOption sets DNS Options for a pod.
+func WithPodDNSOption(dnsOption []string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if pod.config.InfraContainer.UseImageResolvConf {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot add DNS options if pod will not create /etc/resolv.conf")
+ }
+
+ pod.config.InfraContainer.DNSOption = dnsOption
+
+ return nil
+ }
+}
+
+// WithPodUseImageHosts tells the pod not to create /etc/hosts and instead to
+// use the one provided by the image.
+func WithPodUseImageHosts() PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if len(pod.config.InfraContainer.HostAdd) != 0 {
+ return errors.Wrapf(define.ErrInvalidArg, "not creating /etc/hosts conflicts with adding to the hosts file")
+ }
+
+ pod.config.InfraContainer.UseImageHosts = true
+
+ return nil
+ }
+}
+
+// WithPodHosts adds additional entries to the pod's /etc/hosts
+func WithPodHosts(hosts []string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ if pod.config.InfraContainer.UseImageHosts {
+ return errors.Wrapf(define.ErrInvalidArg, "cannot add to /etc/hosts if container is using image hosts")
+ }
+
+ pod.config.InfraContainer.HostAdd = hosts
+
+ return nil
+ }
+}
+
+// WithPodNetworks sets additional CNI networks for the pod to join.
+func WithPodNetworks(networks []string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ pod.config.InfraContainer.Networks = networks
+
+ return nil
+ }
+}