diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2020-04-28 11:41:53 +0200 |
---|---|---|
committer | Giuseppe Scrivano <gscrivan@redhat.com> | 2020-04-30 11:33:33 +0200 |
commit | 65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d (patch) | |
tree | 95914c05c52f389e51551accda0b9837e851ba7b /cmd/podman | |
parent | 27f514544597f68b2f5229a2b2299e2df446afd5 (diff) | |
download | podman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.tar.gz podman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.tar.bz2 podman-65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d.zip |
cmd, podman: handle --pod new:POD
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Diffstat (limited to 'cmd/podman')
-rw-r--r-- | cmd/podman/containers/create.go | 28 | ||||
-rw-r--r-- | cmd/podman/containers/run.go | 4 |
2 files changed, 32 insertions, 0 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 { |