cmd, podman: handle --pod new:POD

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2020-04-28 11:41:53 +02:00
parent 27f5145445
commit 65d7f22720
No known key found for this signature in database
GPG Key ID: E4730F97F60286ED
3 changed files with 35 additions and 8 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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{}