Skip to content

Commit

Permalink
Fix ulimit parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof authored and glours committed Jan 19, 2024
1 parent cef6453 commit 3c8d69f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
25 changes: 25 additions & 0 deletions loader/extends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,28 @@ services:
assert.NilError(t, err)
assert.Equal(t, p.Services["test"].Ports[0].Target, uint32(8000))
}

func TestExtendsUlimits(t *testing.T) {
yaml := `
name: test-extends
services:
test:
extends:
file: testdata/extends/base.yaml
service: withUlimits
`
abs, err := filepath.Abs(".")
assert.NilError(t, err)

p, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Content: []byte(yaml),
Filename: "(inline)",
},
},
WorkingDir: abs,
})
assert.NilError(t, err)
assert.Equal(t, p.Services["test"].Ulimits["nproc"].Single, 65535)
}
11 changes: 10 additions & 1 deletion loader/testdata/extends/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ services:

with-port:
ports:
- 8000:8000
- 8000:8000

withUlimits:
image: test
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000

4 changes: 1 addition & 3 deletions transform/ulimits.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func transformUlimits(data any, p tree.Path) (any, error) {
case map[string]any:
return v, nil
case int:
return map[string]any{
"single": v,
}, nil
return v, nil
default:
return data, errors.Errorf("%s: invalid type %T for external", p, v)
}
Expand Down
25 changes: 25 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,31 @@ type UlimitsConfig struct {
Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`
}

func (u *UlimitsConfig) DecodeMapstructure(value interface{}) error {
switch v := value.(type) {
case *UlimitsConfig:
// this call to DecodeMapstructure is triggered after initial value conversion as we use a map[string]*UlimitsConfig
return nil
case int:
u.Single = v
u.Soft = 0
u.Hard = 0
case map[string]any:
u.Single = 0
soft, ok := v["soft"]
if ok {
u.Soft = soft.(int)
}
hard, ok := v["hard"]
if ok {
u.Hard = hard.(int)
}
default:
return fmt.Errorf("unexpected value type %T for ulimit", value)
}
return nil
}

// MarshalYAML makes UlimitsConfig implement yaml.Marshaller
func (u *UlimitsConfig) MarshalYAML() (interface{}, error) {
if u.Single != 0 {
Expand Down

0 comments on commit 3c8d69f

Please sign in to comment.