/
cgroup.go
64 lines (54 loc) · 1.42 KB
/
cgroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// Parser for the image builder's configuration schemas.
package configuration
import (
"encoding/json"
"fmt"
)
// CGroup sets the CGroup version
type CGroup string
const (
// CGroupDefault enables cgroupv1
CGroupDefault CGroup = ""
// CGroupV1 enables cgroupv1
CGroupV1 CGroup = "version_one"
// CGroupV2 enables cgroupv2
CGroupV2 CGroup = "version_two"
)
func (c CGroup) String() string {
return fmt.Sprint(string(c))
}
// GetValidCGroup returns a list of all the supported
// cgroup version options
func (c *CGroup) GetValidCGroup() (types []CGroup) {
return []CGroup{
CGroupDefault,
CGroupV1,
CGroupV2,
}
}
// IsValid returns an error if the CGroup is not valid
func (c *CGroup) IsValid() (err error) {
for _, valid := range c.GetValidCGroup() {
if *c == valid {
return
}
}
return fmt.Errorf("invalid value for CGroup (%s)", c)
}
// UnmarshalJSON Unmarshals a CGroup entry
func (c *CGroup) UnmarshalJSON(b []byte) (err error) {
// Use an intermediate type which will use the default JSON unmarshal implementation
type IntermediateTypeCGroup CGroup
err = json.Unmarshal(b, (*IntermediateTypeCGroup)(c))
if err != nil {
return fmt.Errorf("failed to parse [CGroup]: %w", err)
}
// Now validate the resulting unmarshaled object
err = c.IsValid()
if err != nil {
return fmt.Errorf("failed to parse [CGroup]: %w", err)
}
return
}