diff --git a/changelog/@unreleased/pr-129.v2.yml b/changelog/@unreleased/pr-129.v2.yml new file mode 100644 index 00000000..296158bd --- /dev/null +++ b/changelog/@unreleased/pr-129.v2.yml @@ -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 diff --git a/launchlib/config.go b/launchlib/config.go index 34bae06d..bcf9c8fe 100644 --- a/launchlib/config.go +++ b/launchlib/config.go @@ -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 { diff --git a/launchlib/config_test.go b/launchlib/config_test.go index debbb19a..e7d1cf7f 100644 --- a/launchlib/config_test.go +++ b/launchlib/config_test.go @@ -247,6 +247,9 @@ env: jvmOpts: - jvmOpt1 - jvmOpt2 +cgroupsV1: + memory: groupA + cpuset: groupB subProcesses: envoy: configType: executable @@ -257,6 +260,10 @@ subProcesses: VersionedConfig: VersionedConfig{ Version: 1, }, + CgroupsV1: map[string]string{ + "memory": "groupA", + "cpuset": "groupB", + }, CustomLauncherConfig: CustomLauncherConfig{ TypedConfig: TypedConfig{ Type: "java", diff --git a/launchlib/launcher.go b/launchlib/launcher.go index 4508ae57..cd1e3047 100644 --- a/launchlib/launcher.go +++ b/launchlib/launcher.go @@ -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") } @@ -55,7 +55,7 @@ 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) } @@ -63,7 +63,8 @@ func CompileCmdsFromConfig( 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") @@ -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))