Skip to content

Commit

Permalink
Merge pull request #537 from dtrudg/cgroups-refactor
Browse files Browse the repository at this point in the history
refactor: tidy cgroups manager code
  • Loading branch information
dtrudg authored Feb 1, 2022
2 parents 5108333 + 00c7c39 commit 10a4c8e
Show file tree
Hide file tree
Showing 18 changed files with 679 additions and 628 deletions.
2 changes: 1 addition & 1 deletion internal/app/singularity/oci_update_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func OciUpdate(containerID string, args *OciArgs) error {
}

resources := &specs.LinuxResources{}
manager, err := cgroups.GetManagerFromPid(state.State.Pid)
manager, err := cgroups.GetManagerForPid(state.State.Pid)
if err != nil {
return fmt.Errorf("failed to get cgroups manager: %v", err)
}
Expand Down
28 changes: 25 additions & 3 deletions internal/pkg/cgroups/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package cgroups

import (
"encoding/json"
"io/ioutil"
"path/filepath"

specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pelletier/go-toml"
)

Expand Down Expand Up @@ -169,7 +171,7 @@ type Config struct {
// Unified map[string]string `toml:"unified" json:"unified,omitempty"`
}

// LoadConfig opens cgroups controls config file and unmarshals it into structures
// LoadConfig loads a cgroups config file into our native cgroups.Config struct
func LoadConfig(confPath string) (config Config, err error) {
path, err := filepath.Abs(confPath)
if err != nil {
Expand All @@ -187,12 +189,32 @@ func LoadConfig(confPath string) (config Config, err error) {
return
}

// PutConfig takes the content of a CgroupsConfig struct and Marshals it to file
func PutConfig(config Config, confPath string) (err error) {
// SaveConfig saves a native cgroups.Config struct into a TOML file at confPath
func SaveConfig(config Config, confPath string) (err error) {
data, err := toml.Marshal(config)
if err != nil {
return
}

return ioutil.WriteFile(confPath, data, 0o600)
}

// LoadResources loads a cgroups config file into a LinuxResources struct
func LoadResources(path string) (spec specs.LinuxResources, err error) {
conf, err := LoadConfig(path)
if err != nil {
return
}

// convert TOML structures to OCI JSON structures
data, err := json.Marshal(conf)
if err != nil {
return
}

if err = json.Unmarshal(data, &spec); err != nil {
return
}

return
}
274 changes: 0 additions & 274 deletions internal/pkg/cgroups/manager_libcontainer_linux.go

This file was deleted.

Loading

0 comments on commit 10a4c8e

Please sign in to comment.