Skip to content

Commit 851eeb9

Browse files
committed
Add support for pids limit in stacks (swarm)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 395a6d5 commit 851eeb9

9 files changed

Lines changed: 72 additions & 47 deletions

File tree

cli/command/stack/kubernetes/convert.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,22 @@ func fromComposeConstraints(s []string) *latest.Constraints {
489489

490490
func fromComposeResources(r composeTypes.Resources) latest.Resources {
491491
return latest.Resources{
492-
Limits: fromComposeResourcesResource(r.Limits),
492+
Limits: fromComposeResourcesResourceLimit(r.Limits),
493493
Reservations: fromComposeResourcesResource(r.Reservations),
494494
}
495495
}
496496

497+
// TODO create ResourceLimit type and support for limiting Pids on k8s
498+
func fromComposeResourcesResourceLimit(r *composeTypes.ResourceLimit) *latest.Resource {
499+
if r == nil {
500+
return nil
501+
}
502+
return &latest.Resource{
503+
MemoryBytes: int64(r.MemoryBytes),
504+
NanoCPUs: r.NanoCPUs,
505+
}
506+
}
507+
497508
func fromComposeResourcesResource(r *composeTypes.Resource) *latest.Resource {
498509
if r == nil {
499510
return nil

cli/compose/convert/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement
534534
resources.Limits = &swarm.Limit{
535535
NanoCPUs: cpus,
536536
MemoryBytes: int64(source.Limits.MemoryBytes),
537+
Pids: source.Limits.Pids,
537538
}
538539
}
539540
if source.Reservations != nil {

cli/compose/convert/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestConvertExtraHosts(t *testing.T) {
7474

7575
func TestConvertResourcesFull(t *testing.T) {
7676
source := composetypes.Resources{
77-
Limits: &composetypes.Resource{
77+
Limits: &composetypes.ResourceLimit{
7878
NanoCPUs: "0.003",
7979
MemoryBytes: composetypes.UnitBytes(300000000),
8080
},
@@ -101,7 +101,7 @@ func TestConvertResourcesFull(t *testing.T) {
101101

102102
func TestConvertResourcesOnlyMemory(t *testing.T) {
103103
source := composetypes.Resources{
104-
Limits: &composetypes.Resource{
104+
Limits: &composetypes.ResourceLimit{
105105
MemoryBytes: composetypes.UnitBytes(300000000),
106106
},
107107
Reservations: &composetypes.Resource{

cli/compose/loader/full-example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ services:
6868
limits:
6969
cpus: '0.001'
7070
memory: 50M
71+
pids: 100
7172
reservations:
7273
cpus: '0.0001'
7374
memory: 20M

cli/compose/loader/full-struct_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
8484
Order: "start-first",
8585
},
8686
Resources: types.Resources{
87-
Limits: &types.Resource{
87+
Limits: &types.ResourceLimit{
8888
NanoCPUs: "0.001",
8989
MemoryBytes: 50 * 1024 * 1024,
90+
Pids: 100,
9091
},
9192
Reservations: &types.Resource{
9293
NanoCPUs: "0.0001",
@@ -581,6 +582,7 @@ services:
581582
limits:
582583
cpus: "0.001"
583584
memory: "52428800"
585+
pids: 100
584586
reservations:
585587
cpus: "0.0001"
586588
memory: "20971520"
@@ -1070,7 +1072,8 @@ func fullExampleJSON(workingDir string) string {
10701072
"resources": {
10711073
"limits": {
10721074
"cpus": "0.001",
1073-
"memory": "52428800"
1075+
"memory": "52428800",
1076+
"pids": 100
10741077
},
10751078
"reservations": {
10761079
"cpus": "0.0001",

cli/compose/loader/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestMarshallConfig(t *testing.T) {
2020
assert.Check(t, is.Equal(expected, string(actual)))
2121

2222
// Make sure the expected still
23-
dict, err := ParseYAML([]byte("version: '3.7'\n" + expected))
23+
dict, err := ParseYAML([]byte("version: '3.9'\n" + expected))
2424
assert.NilError(t, err)
2525
_, err = Load(buildConfigDetails(dict, map[string]string{}))
2626
assert.NilError(t, err)

cli/compose/schema/bindata.go

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/compose/schema/data/config_schema_v3.9.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@
392392
"type": "object",
393393
"properties": {
394394
"cpus": {"type": "string"},
395-
"memory": {"type": "string"}
395+
"memory": {"type": "string"},
396+
"pids": {"type": "integer"}
396397
},
397398
"additionalProperties": false
398399
},

cli/compose/types/types.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,19 @@ type UpdateConfig struct {
302302

303303
// Resources the resource limits and reservations
304304
type Resources struct {
305-
Limits *Resource `yaml:",omitempty" json:"limits,omitempty"`
306-
Reservations *Resource `yaml:",omitempty" json:"reservations,omitempty"`
305+
Limits *ResourceLimit `yaml:",omitempty" json:"limits,omitempty"`
306+
Reservations *Resource `yaml:",omitempty" json:"reservations,omitempty"`
307307
}
308308

309-
// Resource is a resource to be limited or reserved
309+
// ResourceLimit is a resource to be limited
310+
type ResourceLimit struct {
311+
// TODO: types to convert from units and ratios
312+
NanoCPUs string `mapstructure:"cpus" yaml:"cpus,omitempty" json:"cpus,omitempty"`
313+
MemoryBytes UnitBytes `mapstructure:"memory" yaml:"memory,omitempty" json:"memory,omitempty"`
314+
Pids int64 `mapstructure:"pids" yaml:"pids,omitempty" json:"pids,omitempty"`
315+
}
316+
317+
// Resource is a resource to be reserved
310318
type Resource struct {
311319
// TODO: types to convert from units and ratios
312320
NanoCPUs string `mapstructure:"cpus" yaml:"cpus,omitempty" json:"cpus,omitempty"`

0 commit comments

Comments
 (0)