Merge pull request #3812 from mspiess/3293-fix-replicas-override-0

add ability to override ints in compose files with 0
This commit is contained in:
Sebastiaan van Stijn 2022-12-05 16:26:44 +01:00 committed by GitHub
commit 37dcee37b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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),
},
},
)
}