|
3 | 3 | package systemd |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "fmt" |
| 7 | + "math" |
6 | 8 | "os" |
7 | 9 | "path/filepath" |
8 | 10 | "strconv" |
@@ -34,6 +36,47 @@ func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) cgrou |
34 | 36 | } |
35 | 37 | } |
36 | 38 |
|
| 39 | +// unifiedResToSystemdProps tries to convert from Cgroup.Resources.Unified |
| 40 | +// key/value map (where key is cgroupfs file name) to systemd unit properties. |
| 41 | +// This is on a best-effort basis, so the properties that are not known |
| 42 | +// (to this function and/or systemd) are ignored (but logged with "debug" |
| 43 | +// log level). |
| 44 | +// |
| 45 | +// For the list of keys, see https://www.kernel.org/doc/Documentation/cgroup-v2.txt |
| 46 | +// |
| 47 | +// For the list of systemd unit properties, see systemd.resource-control(5). |
| 48 | +func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Property, _ error) { |
| 49 | + for k, v := range res { |
| 50 | + if strings.Contains(k, "/") { |
| 51 | + return nil, fmt.Errorf("unified resource %q must be a file name (no slashes)", k) |
| 52 | + } |
| 53 | + sk := strings.SplitN(k, ".", 2) |
| 54 | + if len(sk) != 2 { |
| 55 | + return nil, fmt.Errorf("unified resource %q must be in the form CONTROLLER.PARAMETER", k) |
| 56 | + } |
| 57 | + switch k { |
| 58 | + case "pids.max": |
| 59 | + num := uint64(math.MaxUint64) |
| 60 | + if v != "max" { |
| 61 | + var err error |
| 62 | + num, err = strconv.ParseUint(v, 10, 64) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("unified resource %q value conversion error: %w", k, err) |
| 65 | + } |
| 66 | + } |
| 67 | + props = append(props, |
| 68 | + newProp("TasksAccounting", true), |
| 69 | + newProp("TasksMax", uint64(num))) |
| 70 | + default: |
| 71 | + // Ignore the unknown resource here -- will still be |
| 72 | + // applied in Set which calls fs2.Set. |
| 73 | + logrus.Debugf("don't know how to convert unified resource %q=%q to systemd unit property; skipping (will still be applied to cgroupfs)", k, v) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return props, nil |
| 78 | +} |
| 79 | + |
37 | 80 | func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]systemdDbus.Property, error) { |
38 | 81 | var properties []systemdDbus.Property |
39 | 82 | r := c.Resources |
@@ -82,6 +125,15 @@ func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst |
82 | 125 |
|
83 | 126 | // ignore r.KernelMemory |
84 | 127 |
|
| 128 | + // convert Resources.Unified map to systemd properties |
| 129 | + if r.Unified != nil { |
| 130 | + unifiedProps, err := unifiedResToSystemdProps(r.Unified) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + properties = append(properties, unifiedProps...) |
| 135 | + } |
| 136 | + |
85 | 137 | return properties, nil |
86 | 138 | } |
87 | 139 |
|
|
0 commit comments