summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhaircommander <pehunt@redhat.com>2018-08-23 13:33:10 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-08-23 18:16:28 +0000
commit4c00dc66dfacc214d6fff845b73a4fc4226b6747 (patch)
tree38bb31acffd27a56a68f54af4968a082ffdb1f73
parent0e6266858a913ac36de0726ede10d5d03af533e3 (diff)
downloadpodman-4c00dc66dfacc214d6fff845b73a4fc4226b6747.tar.gz
podman-4c00dc66dfacc214d6fff845b73a4fc4226b6747.tar.bz2
podman-4c00dc66dfacc214d6fff845b73a4fc4226b6747.zip
Refactor error checking in With*NSFromPod options
Signed-off-by: haircommander <pehunt@redhat.com> Closes: #1187 Approved by: mheon
-rw-r--r--libpod/options.go84
-rw-r--r--libpod/util.go15
2 files changed, 29 insertions, 70 deletions
diff --git a/libpod/options.go b/libpod/options.go
index f9ef2468e..ae6b19055 100644
--- a/libpod/options.go
+++ b/libpod/options.go
@@ -557,16 +557,8 @@ func WithIPCNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -587,16 +579,8 @@ func WithMountNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -617,16 +601,8 @@ func WithNetNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -647,16 +623,8 @@ func WithPIDNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -677,16 +645,8 @@ func WithUTSNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -707,16 +667,8 @@ func WithUserNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
@@ -737,16 +689,8 @@ func WithCgroupNSFromPod(p *Pod) CtrCreateOption {
return ErrCtrFinalized
}
- if p == nil {
- return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
- }
-
- if ctr.config.Pod == "" {
- return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
- }
-
- if ctr.config.Pod != p.ID() {
- return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ if err := validPodNSOption(p, ctr.config.Pod); err != nil {
+ return err
}
infraContainer, err := p.InfraContainerID()
diff --git a/libpod/util.go b/libpod/util.go
index 13235059f..17325f6e4 100644
--- a/libpod/util.go
+++ b/libpod/util.go
@@ -145,3 +145,18 @@ func sortMounts(m []spec.Mount) []spec.Mount {
sort.Sort(byDestination(m))
return m
}
+
+func validPodNSOption(p *Pod, ctrPod string) error {
+ if p == nil {
+ return errors.Wrapf(ErrInvalidArg, "pod passed in was nil. Container may not be associated with a pod")
+ }
+
+ if ctrPod == "" {
+ return errors.Wrapf(ErrInvalidArg, "container is not a member of any pod")
+ }
+
+ if ctrPod != p.ID() {
+ return errors.Wrapf(ErrInvalidArg, "pod passed in is not the pod the container is associated with")
+ }
+ return nil
+}