add ability to override ints in compose files with 0

Signed-off-by: Marco Spiess <marco.spiess@hotmail.de>
This commit is contained in:
mspiess 2022-10-12 20:16:51 +02:00 committed by Marco Spiess
parent ed525aa322
commit 68330e099a
2 changed files with 66 additions and 0 deletions

View File

@ -61,6 +61,7 @@ func mergeServices(base, override []types.ServiceConfig) ([]types.ServiceConfig,
reflect.TypeOf([]types.ServiceVolumeConfig{}): mergeSlice(toServiceVolumeConfigsMap, toServiceVolumeConfigsSlice), reflect.TypeOf([]types.ServiceVolumeConfig{}): mergeSlice(toServiceVolumeConfigsMap, toServiceVolumeConfigsSlice),
reflect.TypeOf(types.ShellCommand{}): mergeShellCommand, reflect.TypeOf(types.ShellCommand{}): mergeShellCommand,
reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig, reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig,
reflect.PointerTo(reflect.TypeOf(uint64(1))): mergeUint64,
}, },
} }
for name, overrideService := range overrideServices { for name, overrideService := range overrideServices {
@ -259,6 +260,14 @@ func mergeServiceNetworkConfig(dst, src reflect.Value) error {
return nil 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 { func getLoggingDriver(v reflect.Value) string {
return v.FieldByName("Driver").String() return v.FieldByName("Driver").String()
} }

View File

@ -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),
},
},
)
}