Skip to content

Commit

Permalink
Address comments from codeclimate
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Jan 29, 2019
1 parent 2354ad1 commit ab9ab88
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Expand Up @@ -2,7 +2,7 @@ version: "2"
checks:
argument-count:
config:
threshold: 9
threshold: 10
complex-logic:
config:
threshold: 8
Expand Down
4 changes: 2 additions & 2 deletions cmd/vmwrapper/vmwrapper.go
Expand Up @@ -73,7 +73,7 @@ func main() {

// FIXME: move the pid of qemu instance out of /kubepods/podxxxxxxx
// for some cases it will be killed by kubelet after the virtlet pod is deleted/recreated
cm := cgroups.NewCgroupsManager(os.Getpid(), nil)
cm := cgroups.NewManager(os.Getpid(), nil)
if _, err := cm.GetProcessController("hugetlb"); err == nil {
err = cm.MoveProcess("hugetlb", "/")
if err != nil {
Expand Down Expand Up @@ -164,7 +164,7 @@ func main() {
}
}

func setupCPUSets(cm cgroups.CgroupsManager) error {
func setupCPUSets(cm cgroups.Manager) error {
cpusets := os.Getenv(config.CpusetsEnvVarName)
if cpusets == "" {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/libvirttools/cpusets.go
Expand Up @@ -95,7 +95,7 @@ func (v *VirtualizationTool) UpdateCpusetsForEmulatorProcess(containerID, cpuset
}
}

cm := cgroups.NewCgroupsManager(pid, v.filesManipulator)
cm := cgroups.NewManager(pid, v.filesManipulator)
controller, err := cm.GetProcessController("cpuset")
if err != nil {
return false, err
Expand Down
28 changes: 14 additions & 14 deletions pkg/utils/cgroups/controllers.go
Expand Up @@ -36,8 +36,8 @@ type Controller struct {
path string
}

// CgroupsManager provides an interface to operate on linux cgroups
type CgroupsManager interface {
// Manager provides an interface to operate on linux cgroups
type Manager interface {
// GetProcessControllers returns the mapping between controller types and
// their paths inside cgroup fs for the specified PID.
GetProcessControllers() (map[string]string, error)
Expand All @@ -47,26 +47,26 @@ type CgroupsManager interface {
MoveProcess(controller, path string) error
}

// RealCgroupsManager provides an implementation of CgroupsManager which is
// RealManager provides an implementation of Manager which is
// using default linux system paths to access info about cgroups for processes.
type RealCgroupsManager struct {
type RealManager struct {
fm utils.FilesManipulator
pid string
}

var _ CgroupsManager = &RealCgroupsManager{}
var _ Manager = &RealManager{}

// NewRealCgroupsManager returns instance of RealCgroupsManager
func NewCgroupsManager(pid interface{}, fm utils.FilesManipulator) CgroupsManager {
// NewManager returns an instance of RealManager
func NewManager(pid interface{}, fm utils.FilesManipulator) Manager {
if fm == nil {
fm = utils.DefaultFilesManipulator
}
return &RealCgroupsManager{fm: fm, pid: utils.Stringify(pid)}
return &RealManager{fm: fm, pid: utils.Stringify(pid)}
}

// GetProcessControllers is an implementation of GetProcessControllers method
// of CgroupsManager interface.
func (c *RealCgroupsManager) GetProcessControllers() (map[string]string, error) {
// of Manager interface.
func (c *RealManager) GetProcessControllers() (map[string]string, error) {
fr, err := c.fm.FileReader(filepath.Join("/proc", c.pid, "cgroup"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -105,8 +105,8 @@ func (c *RealCgroupsManager) GetProcessControllers() (map[string]string, error)
}

// GetProcessController is an implementation of GetProcessController method
// of CgroupsManager interface.
func (c *RealCgroupsManager) GetProcessController(controllerName string) (*Controller, error) {
// of Manager interface.
func (c *RealManager) GetProcessController(controllerName string) (*Controller, error) {
controllers, err := c.GetProcessControllers()
if err != nil {
return nil, err
Expand All @@ -124,8 +124,8 @@ func (c *RealCgroupsManager) GetProcessController(controllerName string) (*Contr
}, nil
}

// MoveProcess implements MoveProcess method of CgroupsManager
func (c *RealCgroupsManager) MoveProcess(controller, path string) error {
// MoveProcess implements MoveProcess method of Manager
func (c *RealManager) MoveProcess(controller, path string) error {
return c.fm.WriteFile(
filepath.Join(cgroupfs, controller, path, "cgroup.procs"),
[]byte(utils.Stringify(c.pid)),
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/files_manipulator.go
Expand Up @@ -36,6 +36,8 @@ type FilesManipulator interface {

type realFilesManipulator struct{}

// DefaultFilesManipulator is an implementation of FilesManager which is
// accessing real filesystem to provide requested data.
var DefaultFilesManipulator FilesManipulator = &realFilesManipulator{}

// FileReader provides an interface to file reading
Expand Down

0 comments on commit ab9ab88

Please sign in to comment.