Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Support human readable strings for memory related vaules
Browse files Browse the repository at this point in the history
e.g. 40m for 40 Megabyte for mem_limit, …

Signed-off-by: d041705 <ma.kohler@sap.com>
Signed-off-by: kohler <markus.kohler@gmail.com>
  • Loading branch information
kohlermsap authored and vdemeester committed Dec 31, 2016
1 parent 15bf99a commit ba184a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
16 changes: 8 additions & 8 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type ServiceConfigV1 struct {
Links yaml.MaporColonSlice `yaml:"links,omitempty"`
LogDriver string `yaml:"log_driver,omitempty"`
MacAddress string `yaml:"mac_address,omitempty"`
MemLimit yaml.StringorInt `yaml:"mem_limit,omitempty"`
MemSwapLimit yaml.StringorInt `yaml:"memswap_limit,omitempty"`
MemSwappiness yaml.StringorInt `yaml:"mem_swappiness,omitempty"`
MemLimit yaml.MemStringorInt `yaml:"mem_limit,omitempty"`
MemSwapLimit yaml.MemStringorInt `yaml:"memswap_limit,omitempty"`
MemSwappiness yaml.MemStringorInt `yaml:"mem_swappiness,omitempty"`
Name string `yaml:"name,omitempty"`
Net string `yaml:"net,omitempty"`
OomScoreAdj yaml.StringorInt `yaml:"oom_score_adj,omitempty"`
Expand All @@ -58,7 +58,7 @@ type ServiceConfigV1 struct {
Privileged bool `yaml:"privileged,omitempty"`
Restart string `yaml:"restart,omitempty"`
ReadOnly bool `yaml:"read_only,omitempty"`
ShmSize yaml.StringorInt `yaml:"shm_size,omitempty"`
ShmSize yaml.MemStringorInt `yaml:"shm_size,omitempty"`
StdinOpen bool `yaml:"stdin_open,omitempty"`
SecurityOpt []string `yaml:"security_opt,omitempty"`
StopSignal string `yaml:"stop_signal,omitempty"`
Expand Down Expand Up @@ -115,17 +115,17 @@ type ServiceConfig struct {
Links yaml.MaporColonSlice `yaml:"links,omitempty"`
Logging Log `yaml:"logging,omitempty"`
MacAddress string `yaml:"mac_address,omitempty"`
MemLimit yaml.StringorInt `yaml:"mem_limit,omitempty"`
MemSwapLimit yaml.StringorInt `yaml:"memswap_limit,omitempty"`
MemSwappiness yaml.StringorInt `yaml:"mem_swappiness,omitempty"`
MemLimit yaml.MemStringorInt `yaml:"mem_limit,omitempty"`
MemSwapLimit yaml.MemStringorInt `yaml:"memswap_limit,omitempty"`
MemSwappiness yaml.MemStringorInt `yaml:"mem_swappiness,omitempty"`
NetworkMode string `yaml:"network_mode,omitempty"`
Networks *yaml.Networks `yaml:"networks,omitempty"`
OomScoreAdj yaml.StringorInt `yaml:"oom_score_adj,omitempty"`
Pid string `yaml:"pid,omitempty"`
Ports []string `yaml:"ports,omitempty"`
Privileged bool `yaml:"privileged,omitempty"`
SecurityOpt []string `yaml:"security_opt,omitempty"`
ShmSize yaml.StringorInt `yaml:"shm_size,omitempty"`
ShmSize yaml.MemStringorInt `yaml:"shm_size,omitempty"`
StopSignal string `yaml:"stop_signal,omitempty"`
Tmpfs yaml.Stringorslice `yaml:"tmpfs,omitempty"`
VolumeDriver string `yaml:"volume_driver,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion docker/service/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestIsolation(t *testing.T) {
func TestMemSwappiness(t *testing.T) {
ctx := &ctx.Context{}
sc := &config.ServiceConfig{
MemSwappiness: yaml.StringorInt(10),
MemSwappiness: yaml.MemStringorInt(10),
}
_, hostCfg, err := Convert(sc, ctx.Context, nil)
assert.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestParseWithMultipleComposeFiles(t *testing.T) {
configThree := []byte(`
multiple:
image: busybox
mem_limit: 40000000
mem_limit: "40m"
ports:
- 10000`)

Expand Down Expand Up @@ -243,5 +243,5 @@ func TestParseWithMultipleComposeFiles(t *testing.T) {
assert.Equal(t, "busybox", multipleConfig.Image)
assert.Equal(t, "multi", multipleConfig.ContainerName)
assert.Equal(t, []string{"8000", "9000", "10000"}, multipleConfig.Ports)
assert.Equal(t, yaml.StringorInt(40000000), multipleConfig.MemLimit)
assert.Equal(t, yaml.MemStringorInt(41943040), multipleConfig.MemLimit)
}
28 changes: 28 additions & 0 deletions yaml/types_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-units"
)

// StringorInt represents a string or an integer.
Expand All @@ -23,6 +24,7 @@ func (s *StringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
var stringType string
if err := unmarshal(&stringType); err == nil {
intType, err := strconv.ParseInt(stringType, 10, 64)

if err != nil {
return err
}
Expand All @@ -33,6 +35,32 @@ func (s *StringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
return errors.New("Failed to unmarshal StringorInt")
}

// MemStringorInt represents a string or an integer
// the String supports notations like 10m for then Megabyte of memory
type MemStringorInt int64

// UnmarshalYAML implements the Unmarshaller interface.
func (s *MemStringorInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
var intType int64
if err := unmarshal(&intType); err == nil {
*s = MemStringorInt(intType)
return nil
}

var stringType string
if err := unmarshal(&stringType); err == nil {
intType, err := units.RAMInBytes(stringType)

if err != nil {
return err
}
*s = MemStringorInt(intType)
return nil
}

return errors.New("Failed to unmarshal MemStringorInt")
}

// Stringorslice represents
// Using engine-api Strslice and augment it with YAML marshalling stuff. a string or an array of strings.
type Stringorslice strslice.StrSlice
Expand Down

0 comments on commit ba184a1

Please sign in to comment.