Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 2e320f6

Browse files
authored
Merge pull request #427 from vdemeester/pr-411
Carry #411 & #425 — Supporting human readable for memory related values
2 parents 6c477ba + a5aaaf2 commit 2e320f6

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

config/types.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ type ServiceConfigV1 struct {
4545
Links yaml.MaporColonSlice `yaml:"links,omitempty"`
4646
LogDriver string `yaml:"log_driver,omitempty"`
4747
MacAddress string `yaml:"mac_address,omitempty"`
48-
MemLimit yaml.StringorInt `yaml:"mem_limit,omitempty"`
49-
MemSwapLimit yaml.StringorInt `yaml:"memswap_limit,omitempty"`
50-
MemSwappiness yaml.StringorInt `yaml:"mem_swappiness,omitempty"`
48+
MemLimit yaml.MemStringorInt `yaml:"mem_limit,omitempty"`
49+
MemSwapLimit yaml.MemStringorInt `yaml:"memswap_limit,omitempty"`
50+
MemSwappiness yaml.MemStringorInt `yaml:"mem_swappiness,omitempty"`
5151
Name string `yaml:"name,omitempty"`
5252
Net string `yaml:"net,omitempty"`
5353
OomScoreAdj yaml.StringorInt `yaml:"oom_score_adj,omitempty"`
@@ -58,7 +58,7 @@ type ServiceConfigV1 struct {
5858
Privileged bool `yaml:"privileged,omitempty"`
5959
Restart string `yaml:"restart,omitempty"`
6060
ReadOnly bool `yaml:"read_only,omitempty"`
61-
ShmSize yaml.StringorInt `yaml:"shm_size,omitempty"`
61+
ShmSize yaml.MemStringorInt `yaml:"shm_size,omitempty"`
6262
StdinOpen bool `yaml:"stdin_open,omitempty"`
6363
SecurityOpt []string `yaml:"security_opt,omitempty"`
6464
StopSignal string `yaml:"stop_signal,omitempty"`
@@ -115,17 +115,17 @@ type ServiceConfig struct {
115115
Links yaml.MaporColonSlice `yaml:"links,omitempty"`
116116
Logging Log `yaml:"logging,omitempty"`
117117
MacAddress string `yaml:"mac_address,omitempty"`
118-
MemLimit yaml.StringorInt `yaml:"mem_limit,omitempty"`
119-
MemSwapLimit yaml.StringorInt `yaml:"memswap_limit,omitempty"`
120-
MemSwappiness yaml.StringorInt `yaml:"mem_swappiness,omitempty"`
118+
MemLimit yaml.MemStringorInt `yaml:"mem_limit,omitempty"`
119+
MemSwapLimit yaml.MemStringorInt `yaml:"memswap_limit,omitempty"`
120+
MemSwappiness yaml.MemStringorInt `yaml:"mem_swappiness,omitempty"`
121121
NetworkMode string `yaml:"network_mode,omitempty"`
122122
Networks *yaml.Networks `yaml:"networks,omitempty"`
123123
OomScoreAdj yaml.StringorInt `yaml:"oom_score_adj,omitempty"`
124124
Pid string `yaml:"pid,omitempty"`
125125
Ports []string `yaml:"ports,omitempty"`
126126
Privileged bool `yaml:"privileged,omitempty"`
127127
SecurityOpt []string `yaml:"security_opt,omitempty"`
128-
ShmSize yaml.StringorInt `yaml:"shm_size,omitempty"`
128+
ShmSize yaml.MemStringorInt `yaml:"shm_size,omitempty"`
129129
StopSignal string `yaml:"stop_signal,omitempty"`
130130
Tmpfs yaml.Stringorslice `yaml:"tmpfs,omitempty"`
131131
VolumeDriver string `yaml:"volume_driver,omitempty"`

docker/service/convert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestIsolation(t *testing.T) {
130130
func TestMemSwappiness(t *testing.T) {
131131
ctx := &ctx.Context{}
132132
sc := &config.ServiceConfig{
133-
MemSwappiness: yaml.StringorInt(10),
133+
MemSwappiness: yaml.MemStringorInt(10),
134134
}
135135
_, hostCfg, err := Convert(sc, ctx.Context, nil)
136136
assert.Nil(t, err)

project/project_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ func TestParseWithMultipleComposeFiles(t *testing.T) {
201201
configThree := []byte(`
202202
multiple:
203203
image: busybox
204-
mem_limit: 40000000
204+
mem_limit: "40m"
205+
memswap_limit: 40000000
205206
ports:
206207
- 10000`)
207208

@@ -243,5 +244,6 @@ func TestParseWithMultipleComposeFiles(t *testing.T) {
243244
assert.Equal(t, "busybox", multipleConfig.Image)
244245
assert.Equal(t, "multi", multipleConfig.ContainerName)
245246
assert.Equal(t, []string{"8000", "9000", "10000"}, multipleConfig.Ports)
246-
assert.Equal(t, yaml.StringorInt(40000000), multipleConfig.MemLimit)
247+
assert.Equal(t, yaml.MemStringorInt(41943040), multipleConfig.MemLimit)
248+
assert.Equal(t, yaml.MemStringorInt(40000000), multipleConfig.MemSwapLimit)
247249
}

yaml/types_yaml.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/docker/docker/api/types/strslice"
10+
"github.com/docker/go-units"
1011
)
1112

1213
// StringorInt represents a string or an integer.
@@ -23,6 +24,7 @@ func (s *StringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
2324
var stringType string
2425
if err := unmarshal(&stringType); err == nil {
2526
intType, err := strconv.ParseInt(stringType, 10, 64)
27+
2628
if err != nil {
2729
return err
2830
}
@@ -33,6 +35,32 @@ func (s *StringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
3335
return errors.New("Failed to unmarshal StringorInt")
3436
}
3537

38+
// MemStringorInt represents a string or an integer
39+
// the String supports notations like 10m for then Megabyte of memory
40+
type MemStringorInt int64
41+
42+
// UnmarshalYAML implements the Unmarshaller interface.
43+
func (s *MemStringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
44+
var intType int64
45+
if err := unmarshal(&intType); err == nil {
46+
*s = MemStringorInt(intType)
47+
return nil
48+
}
49+
50+
var stringType string
51+
if err := unmarshal(&stringType); err == nil {
52+
intType, err := units.RAMInBytes(stringType)
53+
54+
if err != nil {
55+
return err
56+
}
57+
*s = MemStringorInt(intType)
58+
return nil
59+
}
60+
61+
return errors.New("Failed to unmarshal MemStringorInt")
62+
}
63+
3664
// Stringorslice represents
3765
// Using engine-api Strslice and augment it with YAML marshalling stuff. a string or an array of strings.
3866
type Stringorslice strslice.StrSlice

0 commit comments

Comments
 (0)