Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ulimit parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
ndeloof committed Jan 19, 2024

Verified

This commit was signed with the committer’s verified signature.
flip1995 Philipp Krones
1 parent 4374750 commit aed034a
Showing 4 changed files with 59 additions and 3 deletions.
25 changes: 25 additions & 0 deletions loader/extends_test.go
Original file line number Diff line number Diff line change
@@ -64,3 +64,28 @@ services:
assert.Equal(t, p.Services["test2"].Hostname, "test2")
assert.Equal(t, p.Services["test3"].Hostname, "test3")
}

func TestExtendsWithInteger(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)
}
8 changes: 8 additions & 0 deletions loader/testdata/extends/base.yaml
Original file line number Diff line number Diff line change
@@ -5,3 +5,11 @@ services:
another:
extends: base

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
@@ -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)
}
25 changes: 25 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -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.Soft = 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 {

0 comments on commit aed034a

Please sign in to comment.