libpod: refactor platformMakePod signature

accept only the resources to be used by the pod, so that the function
can more easily be used by a successive patch.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2023-09-06 15:41:20 +02:00
parent 627ac1c96b
commit 38209ef49d
3 changed files with 36 additions and 35 deletions

View File

@ -58,9 +58,13 @@ func (r *Runtime) NewPod(ctx context.Context, p specgen.PodSpecGenerator, option
pod.valid = true pod.valid = true
if err := r.platformMakePod(pod, p); err != nil { parentCgroup, err := r.platformMakePod(pod, p.ResourceLimits)
if err != nil {
return nil, err return nil, err
} }
if p.InfraContainerSpec != nil {
p.InfraContainerSpec.CgroupParent = parentCgroup
}
if !pod.HasInfraContainer() && pod.SharesNamespaces() { if !pod.HasInfraContainer() && pod.SharesNamespaces() {
return nil, errors.New("Pods must have an infra container to share namespaces") return nil, errors.New("Pods must have an infra container to share namespaces")

View File

@ -1,9 +1,9 @@
package libpod package libpod
import ( import (
"github.com/containers/podman/v4/pkg/specgen" spec "github.com/opencontainers/runtime-spec/specs-go"
) )
func (r *Runtime) platformMakePod(pod *Pod, p specgen.PodSpecGenerator) error { func (r *Runtime) platformMakePod(pod *Pod, resourceLimits *spec.LinuxResources) (string, error) {
return nil return "", nil
} }

View File

@ -10,11 +10,12 @@ import (
"github.com/containers/common/pkg/config" "github.com/containers/common/pkg/config"
"github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/specgen" spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func (r *Runtime) platformMakePod(pod *Pod, p specgen.PodSpecGenerator) error { func (r *Runtime) platformMakePod(pod *Pod, resourceLimits *spec.LinuxResources) (string, error) {
cgroupParent := ""
// Check Cgroup parent sanity, and set it if it was not set // Check Cgroup parent sanity, and set it if it was not set
if r.config.Cgroups() != "disabled" { if r.config.Cgroups() != "disabled" {
switch r.config.Engine.CgroupManager { switch r.config.Engine.CgroupManager {
@ -25,32 +26,30 @@ func (r *Runtime) platformMakePod(pod *Pod, p specgen.PodSpecGenerator) error {
if pod.config.CgroupParent == "" { if pod.config.CgroupParent == "" {
pod.config.CgroupParent = CgroupfsDefaultCgroupParent pod.config.CgroupParent = CgroupfsDefaultCgroupParent
} else if strings.HasSuffix(path.Base(pod.config.CgroupParent), ".slice") { } else if strings.HasSuffix(path.Base(pod.config.CgroupParent), ".slice") {
return fmt.Errorf("systemd slice received as cgroup parent when using cgroupfs: %w", define.ErrInvalidArg) return "", fmt.Errorf("systemd slice received as cgroup parent when using cgroupfs: %w", define.ErrInvalidArg)
} }
// If we are set to use pod cgroups, set the cgroup parent that // If we are set to use pod cgroups, set the cgroup parent that
// all containers in the pod will share // all containers in the pod will share
if pod.config.UsePodCgroup { if pod.config.UsePodCgroup {
pod.state.CgroupPath = filepath.Join(pod.config.CgroupParent, pod.ID()) pod.state.CgroupPath = filepath.Join(pod.config.CgroupParent, pod.ID())
if p.InfraContainerSpec != nil { cgroupParent = pod.state.CgroupPath
p.InfraContainerSpec.CgroupParent = pod.state.CgroupPath // cgroupfs + rootless = permission denied when creating the cgroup.
// cgroupfs + rootless = permission denied when creating the cgroup. if !rootless.IsRootless() {
if !rootless.IsRootless() { res, err := GetLimits(resourceLimits)
res, err := GetLimits(p.ResourceLimits) if err != nil {
if err != nil { return "", err
return err }
} // Need to both create and update the cgroup
// Need to both create and update the cgroup // rather than create a new path in c/common for pod cgroup creation
// rather than create a new path in c/common for pod cgroup creation // just create as if it is a ctr and then update figures out that we need to
// just create as if it is a ctr and then update figures out that we need to // populate the resource limits on the pod level
// populate the resource limits on the pod level cgc, err := cgroups.New(pod.state.CgroupPath, &res)
cgc, err := cgroups.New(pod.state.CgroupPath, &res) if err != nil {
if err != nil { return "", err
return err }
} err = cgc.Update(&res)
err = cgc.Update(&res) if err != nil {
if err != nil { return "", err
return err
}
} }
} }
} }
@ -63,22 +62,20 @@ func (r *Runtime) platformMakePod(pod *Pod, p specgen.PodSpecGenerator) error {
pod.config.CgroupParent = SystemdDefaultCgroupParent pod.config.CgroupParent = SystemdDefaultCgroupParent
} }
} else if len(pod.config.CgroupParent) < 6 || !strings.HasSuffix(path.Base(pod.config.CgroupParent), ".slice") { } else if len(pod.config.CgroupParent) < 6 || !strings.HasSuffix(path.Base(pod.config.CgroupParent), ".slice") {
return fmt.Errorf("did not receive systemd slice as cgroup parent when using systemd to manage cgroups: %w", define.ErrInvalidArg) return "", fmt.Errorf("did not receive systemd slice as cgroup parent when using systemd to manage cgroups: %w", define.ErrInvalidArg)
} }
// If we are set to use pod cgroups, set the cgroup parent that // If we are set to use pod cgroups, set the cgroup parent that
// all containers in the pod will share // all containers in the pod will share
if pod.config.UsePodCgroup { if pod.config.UsePodCgroup {
cgroupPath, err := systemdSliceFromPath(pod.config.CgroupParent, fmt.Sprintf("libpod_pod_%s", pod.ID()), p.ResourceLimits) cgroupPath, err := systemdSliceFromPath(pod.config.CgroupParent, fmt.Sprintf("libpod_pod_%s", pod.ID()), resourceLimits)
if err != nil { if err != nil {
return fmt.Errorf("unable to create pod cgroup for pod %s: %w", pod.ID(), err) return "", fmt.Errorf("unable to create pod cgroup for pod %s: %w", pod.ID(), err)
} }
pod.state.CgroupPath = cgroupPath pod.state.CgroupPath = cgroupPath
if p.InfraContainerSpec != nil { cgroupParent = pod.state.CgroupPath
p.InfraContainerSpec.CgroupParent = pod.state.CgroupPath
}
} }
default: default:
return fmt.Errorf("unsupported Cgroup manager: %s - cannot validate cgroup parent: %w", r.config.Engine.CgroupManager, define.ErrInvalidArg) return "", fmt.Errorf("unsupported Cgroup manager: %s - cannot validate cgroup parent: %w", r.config.Engine.CgroupManager, define.ErrInvalidArg)
} }
} }
@ -86,5 +83,5 @@ func (r *Runtime) platformMakePod(pod *Pod, p specgen.PodSpecGenerator) error {
logrus.Debugf("Got pod cgroup as %s", pod.state.CgroupPath) logrus.Debugf("Got pod cgroup as %s", pod.state.CgroupPath)
} }
return nil return cgroupParent, nil
} }