Skip to content

Commit

Permalink
fix pod volume passing and alter infra inheritance
Browse files Browse the repository at this point in the history
the infra Inherit function was not properly passing pod volume information to new containers
alter the inherit function and struct to use the new `ConfigToSpec` function used in clone
pick and choose the proper entities from a temp spec and validate them on the spegen side rather
than passing directly to a config

resolves containers#13548

Signed-off-by: cdoern <cbdoer23@g.holycross.edu>
  • Loading branch information
cdoern committed Mar 22, 2022
1 parent b4b8b8b commit 7106bcb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
12 changes: 4 additions & 8 deletions libpod/container_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/containers/common/pkg/secrets"
"github.com/containers/image/v5/manifest"
"github.com/containers/podman/v4/pkg/namespaces"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/containers/storage"
spec "github.com/opencontainers/runtime-spec/specs-go"
)
Expand Down Expand Up @@ -406,12 +407,7 @@ type ContainerMiscConfig struct {
}

type InfraInherit struct {
InfraSecurity ContainerSecurityConfig
InfraLabels []string `json:"labelopts,omitempty"`
InfraVolumes []*ContainerNamedVolume `json:"namedVolumes,omitempty"`
InfraOverlay []*ContainerOverlayVolume `json:"overlayVolumes,omitempty"`
InfraImageVolumes []*ContainerImageVolume `json:"ctrImageVolumes,omitempty"`
InfraUserVolumes []string `json:"userVolumes,omitempty"`
InfraResources *spec.LinuxResources `json:"resources,omitempty"`
InfraDevices []spec.LinuxDevice `json:"device_host_src,omitempty"`
InfraSecurity specgen.ContainerSecurityConfig `json:"container_security_config,omitempty"`
InfraResources *spec.LinuxResources `json:"resource_limits,omitempty"`
InfraStorage specgen.ContainerStorageConfig `json:"container_storage_config,omitempty"`
}
16 changes: 16 additions & 0 deletions pkg/specgen/generate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s
conf.Systemd = nil
conf.Mounts = []string{}

if specg == nil {
specg = &specgen.SpecGenerator{}
}

specg.Pod = conf.Pod

matching, err := json.Marshal(conf)
Expand Down Expand Up @@ -483,5 +487,17 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s
specg.OverlayVolumes = overlay
specg.Mounts = conf.Spec.Mounts
specg.HostDeviceList = conf.DeviceHostSrc
mapSecurityConfig(conf, specg)
return c, nil
}

// mapSecurityConfig takes a libpod.ContainerSecurityConfig and converts it to a specgen.ContinerSecurityConfig
func mapSecurityConfig(c *libpod.ContainerConfig, s *specgen.SpecGenerator) {
s.Privileged = c.Privileged
if len(c.LabelOpts) > 0 {
s.SelinuxOpts = append(s.SelinuxOpts, c.LabelOpts...)
}
s.User = c.User
s.Groups = c.Groups
s.HostUsers = c.HostUsers
}
42 changes: 24 additions & 18 deletions pkg/specgen/generate/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGener
compatibleOptions := &libpod.InfraInherit{}
var infraSpec *spec.Spec
if infra != nil {
options, infraSpec, compatibleOptions, err = Inherit(*infra)
options, infraSpec, compatibleOptions, err = Inherit(*infra, s, rt)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -152,8 +152,8 @@ func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGener
return nil, nil, nil, err
}

infraVolumes := (len(compatibleOptions.InfraVolumes) > 0 || len(compatibleOptions.InfraUserVolumes) > 0 || len(compatibleOptions.InfraImageVolumes) > 0)
opts, err := createContainerOptions(ctx, rt, s, pod, finalVolumes, finalOverlays, imageData, command, infraVolumes, *compatibleOptions)
infraVol := (len(compatibleOptions.InfraStorage.Mounts) > 0 || len(compatibleOptions.InfraStorage.Volumes) > 0 || len(compatibleOptions.InfraStorage.ImageVolumes) > 0 || len(compatibleOptions.InfraStorage.OverlayVolumes) > 0)
opts, err := createContainerOptions(ctx, rt, s, pod, finalVolumes, finalOverlays, imageData, command, infraVol, *compatibleOptions)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func createContainerOptions(ctx context.Context, rt *libpod.Runtime, s *specgen.
if len(s.SelinuxOpts) > 0 {
options = append(options, libpod.WithSecLabels(s.SelinuxOpts))
} else {
if pod != nil && len(compatibleOptions.InfraLabels) == 0 {
if pod != nil && len(compatibleOptions.InfraSecurity.SelinuxOpts) == 0 {
// duplicate the security options from the pod
processLabel, err := pod.ProcessLabel()
if err != nil {
Expand Down Expand Up @@ -535,32 +535,38 @@ func createContainerOptions(ctx context.Context, rt *libpod.Runtime, s *specgen.
return options, nil
}

func Inherit(infra libpod.Container) (opts []libpod.CtrCreateOption, infraS *spec.Spec, compat *libpod.InfraInherit, err error) {
func Inherit(infra libpod.Container, s *specgen.SpecGenerator, rt *libpod.Runtime) (opts []libpod.CtrCreateOption, infraS *spec.Spec, compat *libpod.InfraInherit, err error) {
inheritSpec := &specgen.SpecGenerator{}
_, err = ConfigToSpec(rt, inheritSpec, infra.ID())
if err != nil {
return nil, nil, nil, err
}
options := []libpod.CtrCreateOption{}
compatibleOptions := &libpod.InfraInherit{}
infraConf := infra.Config()
infraSpec := infraConf.Spec

config, err := json.Marshal(infraConf)
// double unmarshal, once into compat options and then again into the spec itself
// this is to avoid taking everything from infra's spec
specG, err := json.Marshal(inheritSpec)
if err != nil {
return nil, nil, nil, err
}
err = json.Unmarshal(config, compatibleOptions)

err = json.Unmarshal(specG, compatibleOptions)
if err != nil {
return nil, nil, nil, err
}
if infraSpec.Linux != nil && infraSpec.Linux.Resources != nil {
resources, err := json.Marshal(infraSpec.Linux.Resources)
if err != nil {
return nil, nil, nil, err
}
err = json.Unmarshal(resources, &compatibleOptions.InfraResources)
if err != nil {
return nil, nil, nil, err
}

var compatByte []byte
compatByte, err = json.Marshal(compatibleOptions)
if err != nil {
return nil, nil, nil, err
}
if compatibleOptions != nil {
options = append(options, libpod.WithInfraConfig(*compatibleOptions))

err = json.Unmarshal(compatByte, s)
if err != nil {
return nil, nil, nil, err
}
return options, infraSpec, compatibleOptions, nil
}
4 changes: 2 additions & 2 deletions pkg/specgen/generate/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt
return nil, err
}
}
if len(compatibleOptions.InfraDevices) > 0 && len(s.Devices) == 0 {
userDevices = compatibleOptions.InfraDevices
if len(compatibleOptions.InfraStorage.HostDeviceList) > 0 && len(s.Devices) == 0 {
userDevices = compatibleOptions.InfraStorage.HostDeviceList
} else {
userDevices = s.Devices
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ type ContainerHealthCheckConfig struct {
// swagger:model SpecGenerator
type SpecGenerator struct {
ContainerBasicConfig
ContainerStorageConfig
ContainerSecurityConfig
ContainerStorageConfig `json:"container_storage_config,omitempty"`
ContainerSecurityConfig `json:"container_security_config,omitempty"`
ContainerCgroupConfig
ContainerNetworkConfig
ContainerResourceConfig
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/pod_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ ENTRYPOINT ["sleep","99999"]
ctr3 := podmanTest.Podman([]string{"run", "--pod", podName, ALPINE, "cat", "/tmp1/test"})
ctr3.WaitWithDefaultTimeout()
Expect(ctr3.OutputToString()).To(ContainSubstring("hello"))

ctr4 := podmanTest.Podman([]string{"run", "--pod", podName, ALPINE, "touch", "/tmp1/testing.txt"})
ctr4.WaitWithDefaultTimeout()
Expect(ctr4).Should(Exit(0))
})

It("podman pod create --device", func() {
Expand Down

0 comments on commit 7106bcb

Please sign in to comment.