Skip to content

Commit 3c8d69f

Browse files
ndeloofglours
authored andcommitted
Fix ulimit parsing
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent cef6453 commit 3c8d69f

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

loader/extends_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,28 @@ services:
9090
assert.NilError(t, err)
9191
assert.Equal(t, p.Services["test"].Ports[0].Target, uint32(8000))
9292
}
93+
94+
func TestExtendsUlimits(t *testing.T) {
95+
yaml := `
96+
name: test-extends
97+
services:
98+
test:
99+
extends:
100+
file: testdata/extends/base.yaml
101+
service: withUlimits
102+
`
103+
abs, err := filepath.Abs(".")
104+
assert.NilError(t, err)
105+
106+
p, err := LoadWithContext(context.Background(), types.ConfigDetails{
107+
ConfigFiles: []types.ConfigFile{
108+
{
109+
Content: []byte(yaml),
110+
Filename: "(inline)",
111+
},
112+
},
113+
WorkingDir: abs,
114+
})
115+
assert.NilError(t, err)
116+
assert.Equal(t, p.Services["test"].Ulimits["nproc"].Single, 65535)
117+
}

loader/testdata/extends/base.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ services:
77

88
with-port:
99
ports:
10-
- 8000:8000
10+
- 8000:8000
11+
12+
withUlimits:
13+
image: test
14+
ulimits:
15+
nproc: 65535
16+
nofile:
17+
soft: 20000
18+
hard: 40000
19+

transform/ulimits.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ func transformUlimits(data any, p tree.Path) (any, error) {
2626
case map[string]any:
2727
return v, nil
2828
case int:
29-
return map[string]any{
30-
"single": v,
31-
}, nil
29+
return v, nil
3230
default:
3331
return data, errors.Errorf("%s: invalid type %T for external", p, v)
3432
}

types/types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,31 @@ type UlimitsConfig struct {
603603
Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`
604604
}
605605

606+
func (u *UlimitsConfig) DecodeMapstructure(value interface{}) error {
607+
switch v := value.(type) {
608+
case *UlimitsConfig:
609+
// this call to DecodeMapstructure is triggered after initial value conversion as we use a map[string]*UlimitsConfig
610+
return nil
611+
case int:
612+
u.Single = v
613+
u.Soft = 0
614+
u.Hard = 0
615+
case map[string]any:
616+
u.Single = 0
617+
soft, ok := v["soft"]
618+
if ok {
619+
u.Soft = soft.(int)
620+
}
621+
hard, ok := v["hard"]
622+
if ok {
623+
u.Hard = hard.(int)
624+
}
625+
default:
626+
return fmt.Errorf("unexpected value type %T for ulimit", value)
627+
}
628+
return nil
629+
}
630+
606631
// MarshalYAML makes UlimitsConfig implement yaml.Marshaller
607632
func (u *UlimitsConfig) MarshalYAML() (interface{}, error) {
608633
if u.Single != 0 {

0 commit comments

Comments
 (0)