Move cgroup and oom score setting to cmd.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2018-01-18 08:14:28 +00:00
parent 74d8880032
commit 7d18d61674
3 changed files with 36 additions and 36 deletions

View File

@ -27,6 +27,9 @@ import (
"runtime" "runtime"
"syscall" "syscall"
"github.com/containerd/cgroups"
"github.com/containerd/containerd/sys"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux" "github.com/opencontainers/selinux/go-selinux"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -95,6 +98,19 @@ func main() {
logrus.Infof("Run cri-containerd %+v", o) logrus.Infof("Run cri-containerd %+v", o)
if o.CgroupPath != "" {
_, err := loadCgroup(o.CgroupPath)
if err != nil {
return fmt.Errorf("failed to load cgroup for cgroup path %v: %v", o.CgroupPath, err)
}
}
if o.OOMScore != 0 {
if err := sys.SetOOMScore(os.Getpid(), o.OOMScore); err != nil {
return fmt.Errorf("failed to set OOMScore to %v: %v", o.OOMScore, err)
}
}
// Start profiling server if enable. // Start profiling server if enable.
if o.EnableProfiling { if o.EnableProfiling {
logrus.Info("Start profiling server") logrus.Info("Start profiling server")
@ -200,3 +216,23 @@ func setGLogLevel(l logrus.Level) error {
} }
return nil return nil
} }
// loadCgroup loads the cgroup associated with path if it exists and moves the current process into the cgroup. If the cgroup
// is not created it is created and returned.
func loadCgroup(cgroupPath string) (cgroups.Cgroup, error) {
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath))
if err != nil {
if err != cgroups.ErrCgroupDeleted {
return nil, err
}
if cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(cgroupPath), &runtimespec.LinuxResources{}); err != nil {
return nil, err
}
}
if err := cg.Add(cgroups.Process{
Pid: os.Getpid(),
}); err != nil {
return nil, err
}
return cg, nil
}

View File

@ -19,13 +19,11 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/containerd/cgroups"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
@ -33,7 +31,6 @@ import (
"github.com/opencontainers/image-spec/identity" "github.com/opencontainers/image-spec/identity"
imagespec "github.com/opencontainers/image-spec/specs-go/v1" imagespec "github.com/opencontainers/image-spec/specs-go/v1"
runtimespec "github.com/opencontainers/runtime-spec/specs-go" runtimespec "github.com/opencontainers/runtime-spec/specs-go"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux" "github.com/opencontainers/selinux/go-selinux"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
@ -288,26 +285,6 @@ func (c *criContainerdService) ensureImageExists(ctx context.Context, ref string
return &newImage, nil return &newImage, nil
} }
// loadCgroup loads the cgroup associated with path if it exists and moves the current process into the cgroup. If the cgroup
// is not created it is created and returned.
func loadCgroup(cgroupPath string) (cgroups.Cgroup, error) {
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath))
if err != nil {
if err != cgroups.ErrCgroupDeleted {
return nil, err
}
if cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(cgroupPath), &specs.LinuxResources{}); err != nil {
return nil, err
}
}
if err := cg.Add(cgroups.Process{
Pid: os.Getpid(),
}); err != nil {
return nil, err
}
return cg, nil
}
// imageInfo is the information about the image got from containerd. // imageInfo is the information about the image got from containerd.
type imageInfo struct { type imageInfo struct {
id string id string

View File

@ -26,7 +26,6 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/sys"
"github.com/cri-o/ocicni/pkg/ocicni" "github.com/cri-o/ocicni/pkg/ocicni"
runcapparmor "github.com/opencontainers/runc/libcontainer/apparmor" runcapparmor "github.com/opencontainers/runc/libcontainer/apparmor"
runcseccomp "github.com/opencontainers/runc/libcontainer/seccomp" runcseccomp "github.com/opencontainers/runc/libcontainer/seccomp"
@ -114,18 +113,6 @@ type criContainerdService struct {
// NewCRIContainerdService returns a new instance of CRIContainerdService // NewCRIContainerdService returns a new instance of CRIContainerdService
func NewCRIContainerdService(config options.Config) (CRIContainerdService, error) { func NewCRIContainerdService(config options.Config) (CRIContainerdService, error) {
var err error var err error
if config.CgroupPath != "" {
_, err := loadCgroup(config.CgroupPath)
if err != nil {
return nil, fmt.Errorf("failed to load cgroup for cgroup path %v: %v", config.CgroupPath, err)
}
}
if config.OOMScore != 0 {
if err := sys.SetOOMScore(os.Getpid(), config.OOMScore); err != nil {
return nil, fmt.Errorf("failed to set OOMScore to %v: %v", config.OOMScore, err)
}
}
c := &criContainerdService{ c := &criContainerdService{
config: config, config: config,
apparmorEnabled: runcapparmor.IsEnabled(), apparmorEnabled: runcapparmor.IsEnabled(),