From 68330e099a2d2c5947704840c3e3cf4770c3778d Mon Sep 17 00:00:00 2001 From: mspiess Date: Wed, 12 Oct 2022 20:16:51 +0200 Subject: [PATCH] add ability to override ints in compose files with 0 Signed-off-by: Marco Spiess --- cli/compose/loader/merge.go | 9 +++++ cli/compose/loader/merge_test.go | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/cli/compose/loader/merge.go b/cli/compose/loader/merge.go index 7a37b0d3cf..57bc2296b7 100644 --- a/cli/compose/loader/merge.go +++ b/cli/compose/loader/merge.go @@ -61,6 +61,7 @@ func mergeServices(base, override []types.ServiceConfig) ([]types.ServiceConfig, reflect.TypeOf([]types.ServiceVolumeConfig{}): mergeSlice(toServiceVolumeConfigsMap, toServiceVolumeConfigsSlice), reflect.TypeOf(types.ShellCommand{}): mergeShellCommand, reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig, + reflect.PointerTo(reflect.TypeOf(uint64(1))): mergeUint64, }, } for name, overrideService := range overrideServices { @@ -259,6 +260,14 @@ func mergeServiceNetworkConfig(dst, src reflect.Value) error { return nil } +//nolint:unparam +func mergeUint64(dst, src reflect.Value) error { + if !src.IsNil() { + dst.Elem().Set(src.Elem()) + } + return nil +} + func getLoggingDriver(v reflect.Value) string { return v.FieldByName("Driver").String() } diff --git a/cli/compose/loader/merge_test.go b/cli/compose/loader/merge_test.go index 4a97900b4c..e5aa3eab13 100644 --- a/cli/compose/loader/merge_test.go +++ b/cli/compose/loader/merge_test.go @@ -1237,3 +1237,60 @@ func TestMergeServiceNetworkConfig(t *testing.T) { }, ) } + +// issue #3293 +func TestMergeServiceOverrideReplicasZero(t *testing.T) { + base := types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{ + Replicas: uint64Ptr(3), + }, + } + override := types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{ + Replicas: uint64Ptr(0), + }, + } + services, err := mergeServices([]types.ServiceConfig{base}, []types.ServiceConfig{override}) + assert.NilError(t, err) + assert.Equal(t, len(services), 1) + actual := services[0] + assert.DeepEqual( + t, + actual, + types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{ + Replicas: uint64Ptr(0), + }, + }, + ) +} + +func TestMergeServiceOverrideReplicasNotNil(t *testing.T) { + base := types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{ + Replicas: uint64Ptr(3), + }, + } + override := types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{}, + } + services, err := mergeServices([]types.ServiceConfig{base}, []types.ServiceConfig{override}) + assert.NilError(t, err) + assert.Equal(t, len(services), 1) + actual := services[0] + assert.DeepEqual( + t, + actual, + types.ServiceConfig{ + Name: "someService", + Deploy: types.DeployConfig{ + Replicas: uint64Ptr(3), + }, + }, + ) +}