summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivan@redhat.com>2020-04-28 11:41:53 +0200
committerGiuseppe Scrivano <gscrivan@redhat.com>2020-04-30 11:33:33 +0200
commit65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d (patch)
tree95914c05c52f389e51551accda0b9837e851ba7b
parent27f514544597f68b2f5229a2b2299e2df446afd5 (diff)
downloadpodman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.tar.gz
podman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.tar.bz2
podman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.zip
cmd, podman: handle --pod new:POD
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
-rw-r--r--cmd/podman/containers/create.go28
-rw-r--r--cmd/podman/containers/run.go4
-rw-r--r--pkg/specgen/generate/container_create.go11
3 files changed, 35 insertions, 8 deletions
diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go
index 7eed84f29..9e2d47d21 100644
--- a/cmd/podman/containers/create.go
+++ b/cmd/podman/containers/create.go
@@ -1,8 +1,10 @@
package containers
import (
+ "context"
"fmt"
"os"
+ "strings"
"github.com/containers/common/pkg/config"
"github.com/containers/libpod/cmd/podman/common"
@@ -105,6 +107,10 @@ func create(cmd *cobra.Command, args []string) error {
return err
}
+ if _, err := createPodIfNecessary(s); err != nil {
+ return err
+ }
+
report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s)
if err != nil {
return err
@@ -205,3 +211,25 @@ func openCidFile(cidfile string) (*os.File, error) {
}
return cidFile, nil
}
+
+// createPodIfNecessary automatically creates a pod when requested. if the pod name
+// has the form new:ID, the pod ID is created and the name in the spec generator is replaced
+// with ID.
+func createPodIfNecessary(s *specgen.SpecGenerator) (*entities.PodCreateReport, error) {
+ if !strings.HasPrefix(s.Pod, "new:") {
+ return nil, nil
+ }
+ podName := strings.Replace(s.Pod, "new:", "", 1)
+ if len(podName) < 1 {
+ return nil, errors.Errorf("new pod name must be at least one character")
+ }
+ createOptions := entities.PodCreateOptions{
+ Name: podName,
+ Infra: true,
+ Net: &entities.NetOptions{
+ PublishPorts: s.PortMappings,
+ },
+ }
+ s.Pod = podName
+ return registry.ContainerEngine().PodCreate(context.Background(), createOptions)
+}
diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go
index e3fe4cd0b..d0180840b 100644
--- a/cmd/podman/containers/run.go
+++ b/cmd/podman/containers/run.go
@@ -144,6 +144,10 @@ func run(cmd *cobra.Command, args []string) error {
}
runOpts.Spec = s
+ if _, err := createPodIfNecessary(s); err != nil {
+ return err
+ }
+
report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts)
// report.ExitCode is set by ContainerRun even it it returns an error
if report != nil {
diff --git a/pkg/specgen/generate/container_create.go b/pkg/specgen/generate/container_create.go
index 01ddcf9c8..f04e1548f 100644
--- a/pkg/specgen/generate/container_create.go
+++ b/pkg/specgen/generate/container_create.go
@@ -24,11 +24,10 @@ func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGener
// If joining a pod, retrieve the pod for use.
var pod *libpod.Pod
if s.Pod != "" {
- foundPod, err := rt.LookupPod(s.Pod)
+ pod, err = rt.LookupPod(s.Pod)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving pod %s", s.Pod)
}
- pod = foundPod
}
// Set defaults for unset namespaces
@@ -130,12 +129,8 @@ func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *l
logrus.Debugf("setting container name %s", s.Name)
options = append(options, libpod.WithName(s.Name))
}
- if s.Pod != "" {
- pod, err := rt.LookupPod(s.Pod)
- if err != nil {
- return nil, err
- }
- logrus.Debugf("adding container to pod %s", s.Pod)
+ if pod != nil {
+ logrus.Debugf("adding container to pod %s", pod.Name)
options = append(options, rt.WithPod(pod))
}
destinations := []string{}