Add support for pids limit in stacks (swarm)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-05-09 23:08:42 +02:00
parent 395a6d560d
commit 851eeb9639
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
9 changed files with 72 additions and 47 deletions

View File

@ -489,11 +489,22 @@ func fromComposeConstraints(s []string) *latest.Constraints {
func fromComposeResources(r composeTypes.Resources) latest.Resources { func fromComposeResources(r composeTypes.Resources) latest.Resources {
return latest.Resources{ return latest.Resources{
Limits: fromComposeResourcesResource(r.Limits), Limits: fromComposeResourcesResourceLimit(r.Limits),
Reservations: fromComposeResourcesResource(r.Reservations), Reservations: fromComposeResourcesResource(r.Reservations),
} }
} }
// TODO create ResourceLimit type and support for limiting Pids on k8s
func fromComposeResourcesResourceLimit(r *composeTypes.ResourceLimit) *latest.Resource {
if r == nil {
return nil
}
return &latest.Resource{
MemoryBytes: int64(r.MemoryBytes),
NanoCPUs: r.NanoCPUs,
}
}
func fromComposeResourcesResource(r *composeTypes.Resource) *latest.Resource { func fromComposeResourcesResource(r *composeTypes.Resource) *latest.Resource {
if r == nil { if r == nil {
return nil return nil

View File

@ -534,6 +534,7 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement
resources.Limits = &swarm.Limit{ resources.Limits = &swarm.Limit{
NanoCPUs: cpus, NanoCPUs: cpus,
MemoryBytes: int64(source.Limits.MemoryBytes), MemoryBytes: int64(source.Limits.MemoryBytes),
Pids: source.Limits.Pids,
} }
} }
if source.Reservations != nil { if source.Reservations != nil {

View File

@ -74,7 +74,7 @@ func TestConvertExtraHosts(t *testing.T) {
func TestConvertResourcesFull(t *testing.T) { func TestConvertResourcesFull(t *testing.T) {
source := composetypes.Resources{ source := composetypes.Resources{
Limits: &composetypes.Resource{ Limits: &composetypes.ResourceLimit{
NanoCPUs: "0.003", NanoCPUs: "0.003",
MemoryBytes: composetypes.UnitBytes(300000000), MemoryBytes: composetypes.UnitBytes(300000000),
}, },
@ -101,7 +101,7 @@ func TestConvertResourcesFull(t *testing.T) {
func TestConvertResourcesOnlyMemory(t *testing.T) { func TestConvertResourcesOnlyMemory(t *testing.T) {
source := composetypes.Resources{ source := composetypes.Resources{
Limits: &composetypes.Resource{ Limits: &composetypes.ResourceLimit{
MemoryBytes: composetypes.UnitBytes(300000000), MemoryBytes: composetypes.UnitBytes(300000000),
}, },
Reservations: &composetypes.Resource{ Reservations: &composetypes.Resource{

View File

@ -68,6 +68,7 @@ services:
limits: limits:
cpus: '0.001' cpus: '0.001'
memory: 50M memory: 50M
pids: 100
reservations: reservations:
cpus: '0.0001' cpus: '0.0001'
memory: 20M memory: 20M

View File

@ -84,9 +84,10 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Order: "start-first", Order: "start-first",
}, },
Resources: types.Resources{ Resources: types.Resources{
Limits: &types.Resource{ Limits: &types.ResourceLimit{
NanoCPUs: "0.001", NanoCPUs: "0.001",
MemoryBytes: 50 * 1024 * 1024, MemoryBytes: 50 * 1024 * 1024,
Pids: 100,
}, },
Reservations: &types.Resource{ Reservations: &types.Resource{
NanoCPUs: "0.0001", NanoCPUs: "0.0001",
@ -581,6 +582,7 @@ services:
limits: limits:
cpus: "0.001" cpus: "0.001"
memory: "52428800" memory: "52428800"
pids: 100
reservations: reservations:
cpus: "0.0001" cpus: "0.0001"
memory: "20971520" memory: "20971520"
@ -1070,7 +1072,8 @@ func fullExampleJSON(workingDir string) string {
"resources": { "resources": {
"limits": { "limits": {
"cpus": "0.001", "cpus": "0.001",
"memory": "52428800" "memory": "52428800",
"pids": 100
}, },
"reservations": { "reservations": {
"cpus": "0.0001", "cpus": "0.0001",

View File

@ -20,7 +20,7 @@ func TestMarshallConfig(t *testing.T) {
assert.Check(t, is.Equal(expected, string(actual))) assert.Check(t, is.Equal(expected, string(actual)))
// Make sure the expected still // Make sure the expected still
dict, err := ParseYAML([]byte("version: '3.7'\n" + expected)) dict, err := ParseYAML([]byte("version: '3.9'\n" + expected))
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(buildConfigDetails(dict, map[string]string{})) _, err = Load(buildConfigDetails(dict, map[string]string{}))
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -583,45 +583,45 @@ ean7MQBPP+U4w19V/z+t/hsAAP//Fd/bF0ZHAAA=
"/data/config_schema_v3.9.json": { "/data/config_schema_v3.9.json": {
name: "config_schema_v3.9.json", name: "config_schema_v3.9.json",
local: "data/config_schema_v3.9.json", local: "data/config_schema_v3.9.json",
size: 18362, size: 18407,
modtime: 1518458244, modtime: 1518458244,
compressed: ` compressed: `
H4sIAAAAAAAC/+xcS4/jNhK++1cISm7pxwAbLJC57XFPu+dteASaKttMUyRTpDztDPq/L/RsiSJFylY/ H4sIAAAAAAAC/+xcSY/jNha++1cISm6ppYEJBkjf5jinmfMU3AJNPdtMUSTzSLnLadR/H2gtiSJFylYt
stsBgmlbxUc9+VWx5B+bJEl/1vQIBUm/JunRGPX1/v53LcVt8+2dxMN9jmRvbr/8et9891N6U41jeTWE makAQZetx+XxLfzeIv/YJEn6s6ZHKEj6NUmPxqiv9/e/aylum2/vJB7ucyR7c/vl1/vmu5/Sm2ocy6sh
SrFnh6x5kp3+dvfbXTW8ITFnBRWR3P0O1DTfIfxRMoRq8EN6AtRMinR7s6meKZQK0DDQ6dek2lyS9CTd VIo9O2TNk+z0t7vf7qrhDYk5K6iI5O53oKb5DuGPkiFUgx/SE6BmUqTbm031TKFUgIaBTr8m1eaSpCfp
F4NptUEmDmn99XM9Q5KkGvDE6GCGfqs/3b/Mf9+T3dizDjZbf6+IMYDi39O91Y+/PZDbP/9x+58vt7/d vhhMqw0ycUjrr5/rGZIk1YAnRgcz9Fv96f5l/vue7MaedbDZ+ntFjAEU/57urX787YHc/vmP2/98uf3t
ZbfbX34ePa7ki7Bvls9hzwQzTIp+/bSnfG7/eu4XJnleExM+WntPuIYxzwLMd4mPIZ57snfiuV3fwfOY Lrvd/vLz6HF1vgj7Zvkc9kwww6To1097yuf2r+d+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K3vd9a7crpHTI9WHPu9105/VNlrEXsNYb/91Cs6ucbpydcQYQo9R
g8LnpNDvDGkt1WdEWxDR/ur2//FstX1XNvg+Zk0Vfr31CguNeEfkA+h/DbX+z7llla9yYiCbYecNbHmC ab2F2ZU3yJJOqgJOl9ZSfXq0BR7tr67/H09X2xdrgy9v1lThd2Gv0NCIF0o+gPzXEOv/nFlW8SonBrIZ
PJy23FJ92vLatvxBrMBqaRpYw/RqbU5B0X3Xm+FNWr8Nm8zxqyC+LNS7Kd9FsLVoq5t5zlcMIne/zKD9 dt5AlyfIw6nLLdWnLq+tyx9EC6z+p4E2TOtwcwKKbtLeDMtu/TZsMsdPiPiiUO+mfFVja9FWNvOcr+hE
ufcjXgkmr9BM6tapVaDa9K2j9o8a+ENPN37yEwcVn+I8ufr9MW4fan6eYPxeuUXSvLs0iNrbqOKF64cP 7n6ZQftzL1O8EkxeofPULVMrQbXp+0ztX0Dwu55u/OT3ECo+xXlSJ/4x7jVqfstg/BK6RdK86DTw2tuo
7Oal7gcIPP2U4wx/U/3/vPlvAAAA//8VI4RoukcAAA== 5IXrVxLsTqfu1wo8zZfjCH9T/f+8+W8AAAD//6unbt7nRwAA
`, `,
}, },

View File

@ -392,7 +392,8 @@
"type": "object", "type": "object",
"properties": { "properties": {
"cpus": {"type": "string"}, "cpus": {"type": "string"},
"memory": {"type": "string"} "memory": {"type": "string"},
"pids": {"type": "integer"}
}, },
"additionalProperties": false "additionalProperties": false
}, },

View File

@ -302,11 +302,19 @@ type UpdateConfig struct {
// Resources the resource limits and reservations // Resources the resource limits and reservations
type Resources struct { type Resources struct {
Limits *Resource `yaml:",omitempty" json:"limits,omitempty"` Limits *ResourceLimit `yaml:",omitempty" json:"limits,omitempty"`
Reservations *Resource `yaml:",omitempty" json:"reservations,omitempty"` Reservations *Resource `yaml:",omitempty" json:"reservations,omitempty"`
} }
// Resource is a resource to be limited or reserved // ResourceLimit is a resource to be limited
type ResourceLimit struct {
// TODO: types to convert from units and ratios
NanoCPUs string `mapstructure:"cpus" yaml:"cpus,omitempty" json:"cpus,omitempty"`
MemoryBytes UnitBytes `mapstructure:"memory" yaml:"memory,omitempty" json:"memory,omitempty"`
Pids int64 `mapstructure:"pids" yaml:"pids,omitempty" json:"pids,omitempty"`
}
// Resource is a resource to be reserved
type Resource struct { type Resource struct {
// TODO: types to convert from units and ratios // TODO: types to convert from units and ratios
NanoCPUs string `mapstructure:"cpus" yaml:"cpus,omitempty" json:"cpus,omitempty"` NanoCPUs string `mapstructure:"cpus" yaml:"cpus,omitempty" json:"cpus,omitempty"`