Skip to content

Commit

Permalink
Merge pull request containers#6000 from mheon/volume_backend_flags
Browse files Browse the repository at this point in the history
Add support for volumes-from, image volumes, init
  • Loading branch information
openshift-merge-robot authored Apr 27, 2020
2 parents 8642e25 + 67ec4e1 commit f6f7172
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 63 deletions.
10 changes: 4 additions & 6 deletions cmd/podman/common/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,11 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
s.StaticMAC = c.Net.StaticMAC
s.UseImageHosts = c.Net.NoHosts

// deferred, must be added on libpod side
//var ImageVolumes map[string]struct{}
//if data != nil && c.String("image-volume") != "ignore" {
// ImageVolumes = data.Config.Volumes
//}

s.ImageVolumeMode = c.ImageVolume
if s.ImageVolumeMode == "bind" {
s.ImageVolumeMode = "anonymous"
}

systemd := c.SystemdD == "always"
if !systemd && command != nil {
x, err := strconv.ParseBool(c.SystemdD)
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/handlers/libpod/containers_create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package libpod

import (
"context"
"encoding/json"
"net/http"

Expand All @@ -26,7 +27,7 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
utils.InternalServerError(w, err)
return
}
ctr, err := generate.MakeContainer(runtime, &sg)
ctr, err := generate.MakeContainer(context.Background(), runtime, &sg)
if err != nil {
utils.InternalServerError(w, err)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (ic *ContainerEngine) ContainerCreate(ctx context.Context, s *specgen.SpecG
if err := generate.CompleteSpec(ctx, ic.Libpod, s); err != nil {
return nil, err
}
ctr, err := generate.MakeContainer(ic.Libpod, s)
ctr, err := generate.MakeContainer(ctx, ic.Libpod, s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -680,7 +680,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
if err := generate.CompleteSpec(ctx, ic.Libpod, opts.Spec); err != nil {
return nil, err
}
ctr, err := generate.MakeContainer(ic.Libpod, opts.Spec)
ctr, err := generate.MakeContainer(ctx, ic.Libpod, opts.Spec)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/specgen/container_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
// SystemDValues describes the only values that SystemD can be
SystemDValues = []string{"true", "false", "always"}
// ImageVolumeModeValues describes the only values that ImageVolumeMode can be
ImageVolumeModeValues = []string{"ignore", "tmpfs", "bind"}
ImageVolumeModeValues = []string{"ignore", "tmpfs", "anonymous"}
)

func exclusiveOptions(opt1, opt2 string) error {
Expand Down
25 changes: 0 additions & 25 deletions pkg/specgen/generate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import (
envLib "github.com/containers/libpod/pkg/env"
"github.com/containers/libpod/pkg/signal"
"github.com/containers/libpod/pkg/specgen"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerator) error {
var appendEntryPoint bool

// If a rootfs is used, then there is no image data
if s.ContainerStorageConfig.Rootfs != "" {
return nil
Expand Down Expand Up @@ -107,28 +104,6 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
}
s.Annotations = annotations

// entrypoint
entrypoint, err := newImage.Entrypoint(ctx)
if err != nil {
return err
}
if len(s.Entrypoint) < 1 && len(entrypoint) > 0 {
appendEntryPoint = true
s.Entrypoint = entrypoint
}
command, err := newImage.Cmd(ctx)
if err != nil {
return err
}
if len(s.Command) < 1 && len(command) > 0 {
if appendEntryPoint {
s.Command = entrypoint
}
s.Command = append(s.Command, command...)
}
if len(s.Command) < 1 && len(s.Entrypoint) < 1 {
return errors.Errorf("No command provided or as CMD or ENTRYPOINT in this image")
}
// workdir
workingDir, err := newImage.WorkingDir(ctx)
if err != nil {
Expand Down
47 changes: 28 additions & 19 deletions pkg/specgen/generate/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// MakeContainer creates a container based on the SpecGenerator
func MakeContainer(rt *libpod.Runtime, s *specgen.SpecGenerator) (*libpod.Container, error) {
func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGenerator) (*libpod.Container, error) {
rtc, err := rt.GetConfig()
if err != nil {
return nil, err
Expand Down Expand Up @@ -75,16 +75,8 @@ func MakeContainer(rt *libpod.Runtime, s *specgen.SpecGenerator) (*libpod.Contai
s.CgroupNS = defaultNS
}

options, err := createContainerOptions(rt, s, pod)
if err != nil {
return nil, err
}
options := []libpod.CtrCreateOption{}

podmanPath, err := os.Executable()
if err != nil {
return nil, err
}
options = append(options, createExitCommandOption(s, rt.StorageConfig(), rtc, podmanPath))
var newImage *image.Image
if s.Rootfs != "" {
options = append(options, libpod.WithRootFS(s.Rootfs))
Expand All @@ -99,14 +91,31 @@ func MakeContainer(rt *libpod.Runtime, s *specgen.SpecGenerator) (*libpod.Contai
return nil, errors.Wrap(err, "invalid config provided")
}

runtimeSpec, err := SpecGenToOCI(s, rt, newImage)
finalMounts, finalVolumes, err := finalizeMounts(ctx, s, rt, rtc, newImage)
if err != nil {
return nil, err
}

opts, err := createContainerOptions(rt, s, pod, finalVolumes)
if err != nil {
return nil, err
}
options = append(options, opts...)

podmanPath, err := os.Executable()
if err != nil {
return nil, err
}
options = append(options, createExitCommandOption(s, rt.StorageConfig(), rtc, podmanPath))

runtimeSpec, err := SpecGenToOCI(ctx, s, rt, rtc, newImage, finalMounts)
if err != nil {
return nil, err
}
return rt.NewContainer(context.Background(), runtimeSpec, options...)
return rt.NewContainer(ctx, runtimeSpec, options...)
}

func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *libpod.Pod) ([]libpod.CtrCreateOption, error) {
func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *libpod.Pod, volumes []*specgen.NamedVolume) ([]libpod.CtrCreateOption, error) {
var options []libpod.CtrCreateOption
var err error

Expand All @@ -133,21 +142,21 @@ func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *l
for _, mount := range s.Mounts {
destinations = append(destinations, mount.Destination)
}
for _, volume := range s.Volumes {
for _, volume := range volumes {
destinations = append(destinations, volume.Dest)
}
options = append(options, libpod.WithUserVolumes(destinations))

if len(s.Volumes) != 0 {
var volumes []*libpod.ContainerNamedVolume
for _, v := range s.Volumes {
volumes = append(volumes, &libpod.ContainerNamedVolume{
if len(volumes) != 0 {
var vols []*libpod.ContainerNamedVolume
for _, v := range volumes {
vols = append(vols, &libpod.ContainerNamedVolume{
Name: v.Name,
Dest: v.Dest,
Options: v.Options,
})
}
options = append(options, libpod.WithNamedVolumes(volumes))
options = append(options, libpod.WithNamedVolumes(vols))
}

if len(s.Command) != 0 {
Expand Down
59 changes: 56 additions & 3 deletions pkg/specgen/generate/oci.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package generate

import (
"context"
"strings"

"github.com/containers/common/pkg/config"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/rootless"
"github.com/containers/libpod/pkg/specgen"
"github.com/opencontainers/runc/libcontainer/user"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/pkg/errors"
)

func addRlimits(s *specgen.SpecGenerator, g *generate.Generator) error {
Expand Down Expand Up @@ -48,7 +51,51 @@ func addRlimits(s *specgen.SpecGenerator, g *generate.Generator) error {
return nil
}

func SpecGenToOCI(s *specgen.SpecGenerator, rt *libpod.Runtime, newImage *image.Image) (*spec.Spec, error) {
// Produce the final command for the container.
func makeCommand(ctx context.Context, s *specgen.SpecGenerator, img *image.Image, rtc *config.Config) ([]string, error) {
finalCommand := []string{}

entrypoint := s.Entrypoint
if len(entrypoint) == 0 && img != nil {
newEntry, err := img.Entrypoint(ctx)
if err != nil {
return nil, err
}
entrypoint = newEntry
}

finalCommand = append(finalCommand, entrypoint...)

command := s.Command
if len(command) == 0 && img != nil {
newCmd, err := img.Cmd(ctx)
if err != nil {
return nil, err
}
command = newCmd
}

finalCommand = append(finalCommand, command...)

if len(finalCommand) == 0 {
return nil, errors.Errorf("no command or entrypoint provided, and no CMD or ENTRYPOINT from image")
}

if s.Init {
initPath := s.InitPath
if initPath == "" && rtc != nil {
initPath = rtc.Engine.InitPath
}
if initPath == "" {
return nil, errors.Errorf("no path to init binary found but container requested an init")
}
finalCommand = append([]string{initPath, "--"}, finalCommand...)
}

return finalCommand, nil
}

func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runtime, rtc *config.Config, newImage *image.Image, mounts []spec.Mount) (*spec.Spec, error) {
var (
inUserNS bool
)
Expand Down Expand Up @@ -173,7 +220,13 @@ func SpecGenToOCI(s *specgen.SpecGenerator, rt *libpod.Runtime, newImage *image.
g.AddMount(cgroupMnt)
}
g.SetProcessCwd(s.WorkDir)
g.SetProcessArgs(s.Command)

finalCmd, err := makeCommand(ctx, s, newImage, rtc)
if err != nil {
return nil, err
}
g.SetProcessArgs(finalCmd)

g.SetProcessTerminal(s.Terminal)

for key, val := range s.Annotations {
Expand Down Expand Up @@ -227,7 +280,7 @@ func SpecGenToOCI(s *specgen.SpecGenerator, rt *libpod.Runtime, newImage *image.
}

// BIND MOUNTS
configSpec.Mounts = SupercedeUserMounts(s.Mounts, configSpec.Mounts)
configSpec.Mounts = SupercedeUserMounts(mounts, configSpec.Mounts)
// Process mounts to ensure correct options
if err := InitFSMounts(configSpec.Mounts); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit f6f7172

Please sign in to comment.