-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/int: add a "update cpu period with pod limit set" test
Add a test case for an issue fixed by the previous commit. Unfortunately, this is somewhat complicated as there's no easy way to create a transient unit, so a binary, sd-helper, had to be added. On top of that, an ability to create a parent/pod cgroup is added to helpers.bash, which might be useful for future integration tests. Signed-off-by: Kir Kolyshkin <[email protected]>
- Loading branch information
Showing
5 changed files
with
234 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
"github.com/opencontainers/runc/libcontainer/cgroups/systemd" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
) | ||
|
||
// version will be populated by the Makefile, read from | ||
// VERSION file of the source code. | ||
var version = "" | ||
|
||
// gitCommit will be the hash that the binary was built from | ||
// and will be populated by the Makefile. | ||
var gitCommit = "" | ||
|
||
const ( | ||
usage = `Open Container Initiative contrib/cmd/sd-helper | ||
sd-helper is a tool that uses runc/libcontainer/cgroups/systemd package | ||
functionality to communicate to systemd in order to perform various operations. | ||
Currently this is limited to starting and stopping systemd transient slice | ||
units. | ||
Example: | ||
sd-helper start system-pod123.slice | ||
` | ||
) | ||
|
||
func main() { | ||
if !systemd.IsRunningSystemd() { | ||
logrus.Fatal("systemd is required") | ||
} | ||
|
||
app := cli.NewApp() | ||
app.Name = "sd-helper" | ||
app.Usage = usage | ||
|
||
// Set version to be the same as runc. | ||
var v []string | ||
if version != "" { | ||
v = append(v, version) | ||
} | ||
if gitCommit != "" { | ||
v = append(v, "commit: "+gitCommit) | ||
} | ||
app.Version = strings.Join(v, "\n") | ||
|
||
// Set the flags. | ||
app.Flags = []cli.Flag{ | ||
cli.BoolFlag{ | ||
Name: "debug", | ||
Usage: "Enable debug output", | ||
}, | ||
cli.StringFlag{ | ||
Name: "parent, p", | ||
Usage: "parent unit name", | ||
}, | ||
} | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "start", | ||
Usage: "start a transient unit", | ||
Action: func(c *cli.Context) error { | ||
return unitCommand("start", c) | ||
}, | ||
}, | ||
{ | ||
Name: "stop", | ||
Usage: "stop a transient unit", | ||
Action: func(c *cli.Context) error { | ||
return unitCommand("stop", c) | ||
}, | ||
}, | ||
} | ||
err := app.Run(os.Args) | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
} | ||
|
||
func newManager(config *configs.Cgroup) cgroups.Manager { | ||
if cgroups.IsCgroup2UnifiedMode() { | ||
return systemd.NewUnifiedManager(config, "", false) | ||
} | ||
return systemd.NewLegacyManager(config, nil) | ||
} | ||
|
||
func unitCommand(cmd string, c *cli.Context) error { | ||
if c.Bool("debug") { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
} | ||
name := c.Args().First() | ||
if name == "" { | ||
return errors.New("unit name is required") | ||
} | ||
|
||
podConfig := &configs.Cgroup{ | ||
Name: name, | ||
Parent: c.String("parent"), | ||
Resources: &configs.Resources{}, | ||
} | ||
pm := newManager(podConfig) | ||
|
||
switch cmd { | ||
case "start": | ||
return pm.Apply(-1) | ||
case "stop": | ||
return pm.Destroy() | ||
} | ||
|
||
// Should not happen. | ||
return errors.New("invalid command") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters