Skip to content

Commit

Permalink
Enable cgroupsv1 config (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
a10y authored Apr 14, 2020
1 parent f45fe6b commit 366c1a5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-129.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Allow launching processes into cgroupsv1 cgroups via cgexec
links:
- https://github.com/palantir/go-java-launcher/pull/129
1 change: 1 addition & 0 deletions launchlib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type PrimaryCustomLauncherConfig struct {
VersionedConfig `yaml:",inline"`
CustomLauncherConfig `yaml:",inline"`
SubProcesses map[string]CustomLauncherConfig `yaml:"subProcesses"`
CgroupsV1 map[string]string `yaml:"cgroupsV1"`
}

type AllowedLauncherConfigValues struct {
Expand Down
7 changes: 7 additions & 0 deletions launchlib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ env:
jvmOpts:
- jvmOpt1
- jvmOpt2
cgroupsV1:
memory: groupA
cpuset: groupB
subProcesses:
envoy:
configType: executable
Expand All @@ -257,6 +260,10 @@ subProcesses:
VersionedConfig: VersionedConfig{
Version: 1,
},
CgroupsV1: map[string]string{
"memory": "groupA",
"cpuset": "groupB",
},
CustomLauncherConfig: CustomLauncherConfig{
TypedConfig: TypedConfig{
Type: "java",
Expand Down
19 changes: 16 additions & 3 deletions launchlib/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func CompileCmdsFromConfig(
SubProcesses: make(map[string]*exec.Cmd),
}

serviceCmds.Primary, err = compileCmdFromConfig(&staticConfig.StaticLauncherConfig, &customConfig.CustomLauncherConfig, loggers.PrimaryLogger)
serviceCmds.Primary, err = compileCmdFromConfig(&staticConfig.StaticLauncherConfig, &customConfig.CustomLauncherConfig, &customConfig.CgroupsV1, loggers.PrimaryLogger)
if err != nil {
return nil, errors.Wrap(err, "failed to compile command for primary command")
}
Expand All @@ -55,15 +55,16 @@ func CompileCmdsFromConfig(
return nil, errors.Errorf("no custom launcher config exists for subProcess config '%s'", name)
}

serviceCmds.SubProcesses[name], err = compileCmdFromConfig(&subProcStatic, &subProcCustom, loggers.SubProcessLogger(name))
serviceCmds.SubProcesses[name], err = compileCmdFromConfig(&subProcStatic, &subProcCustom, &customConfig.CgroupsV1, loggers.SubProcessLogger(name))
if err != nil {
return nil, errors.Wrapf(err, "failed to compile command for subProcess %s", name)
}
}
return serviceCmds, nil
}

func compileCmdFromConfig(staticConfig *StaticLauncherConfig, customConfig *CustomLauncherConfig, createLogger CreateLogger) (cmd *exec.Cmd, err error) {
func compileCmdFromConfig(
staticConfig *StaticLauncherConfig, customConfig *CustomLauncherConfig, cgroupsV1 *map[string]string, createLogger CreateLogger) (cmd *exec.Cmd, err error) {
logger, err := createLogger()
if err != nil {
return nil, errors.Wrapf(err, "failed to create command compilation logger")
Expand Down Expand Up @@ -115,6 +116,18 @@ func compileCmdFromConfig(staticConfig *StaticLauncherConfig, customConfig *Cust
}

args = append(args, staticConfig.Args...)
if len(*cgroupsV1) > 0 {
var cgexecArgs []string
executable = "/bin/cgexec"

cgexecArgs = append(cgexecArgs, executable)
for controller, cgroup := range *cgroupsV1 {
cgexecArgs = append(cgexecArgs, "-g", fmt.Sprintf("%s:%s", controller, cgroup))
}
cgexecArgs = append(cgexecArgs, args...)
args = cgexecArgs
}

fmt.Fprintf(logger, "Argument list to executable binary: %v\n\n", args)

env := replaceEnvironmentVariables(merge(staticConfig.Env, customConfig.Env))
Expand Down

0 comments on commit 366c1a5

Please sign in to comment.