Skip to content

Commit 7210cee

Browse files
authored
Merge pull request #481 from ndeloof/scale_default
make scale a *int as default is 1 (not 0)
2 parents cd128ba + 7fe622d commit 7210cee

File tree

9 files changed

+23
-61
lines changed

9 files changed

+23
-61
lines changed

loader/full-struct_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
100100
},
101101
Deploy: &types.DeployConfig{
102102
Mode: "replicated",
103-
Replicas: uint64Ptr(6),
103+
Replicas: intPtr(6),
104104
Labels: map[string]string{"FOO": "BAR"},
105105
RollbackConfig: &types.UpdateConfig{
106106
Parallelism: uint64Ptr(3),
@@ -387,7 +387,6 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
387387
Privileged: true,
388388
ReadOnly: true,
389389
Restart: types.RestartPolicyAlways,
390-
Scale: 1,
391390
Secrets: []types.ServiceSecretConfig{
392391
{
393392
Source: "secret1",
@@ -444,7 +443,6 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
444443
DockerfileInline: "FROM alpine\nRUN echo \"hello\" > /world.txt\n",
445444
},
446445
Environment: types.MappingWithEquals{},
447-
Scale: 1,
448446
},
449447
}
450448
}

loader/loader_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ var sampleConfig = types.Config{
222222
Networks: map[string]*types.ServiceNetworkConfig{
223223
"with_me": nil,
224224
},
225-
Scale: 1,
226225
},
227226
{
228227
Name: "bar",
@@ -231,7 +230,6 @@ var sampleConfig = types.Config{
231230
Networks: map[string]*types.ServiceNetworkConfig{
232231
"with_ipam": nil,
233232
},
234-
Scale: 1,
235233
},
236234
},
237235
Networks: map[string]types.NetworkConfig{
@@ -817,7 +815,7 @@ networks:
817815
Disable: true,
818816
},
819817
Deploy: &types.DeployConfig{
820-
Replicas: uint64Ptr(555),
818+
Replicas: intPtr(555),
821819
UpdateConfig: &types.UpdateConfig{
822820
Parallelism: uint64Ptr(555),
823821
MaxFailureRatio: 3.14,
@@ -844,7 +842,6 @@ networks:
844842
},
845843
Privileged: true,
846844
ReadOnly: true,
847-
Scale: 1,
848845
ShmSize: types.UnitBytes(2 * 1024 * 1024 * 1024),
849846
StopGracePeriod: &typesDuration,
850847
StdinOpen: true,
@@ -1054,14 +1051,18 @@ services:
10541051
`, map[string]string{"FOO_SCALE": "2"})
10551052

10561053
assert.NilError(t, err)
1057-
assert.Equal(t, project.Services[0].Scale, 2)
1054+
assert.Equal(t, *project.Services[0].Scale, 2)
10581055
}
10591056

10601057
func durationPtr(value time.Duration) *types.Duration {
10611058
result := types.Duration(value)
10621059
return &result
10631060
}
10641061

1062+
func intPtr(value int) *int {
1063+
return &value
1064+
}
1065+
10651066
func uint64Ptr(value uint64) *uint64 {
10661067
return &value
10671068
}
@@ -1583,7 +1584,6 @@ networks:
15831584
{
15841585
Name: "hello-world",
15851586
Image: "redis:alpine",
1586-
Scale: 1,
15871587
Networks: map[string]*types.ServiceNetworkConfig{
15881588
"network1": nil,
15891589
"network3": nil,
@@ -1634,7 +1634,6 @@ networks:
16341634
{
16351635
Name: "foo",
16361636
Image: "alpine",
1637-
Scale: 1,
16381637
Networks: map[string]*types.ServiceNetworkConfig{
16391638
"network1": {
16401639
Ipv4Address: "10.1.0.100",
@@ -1796,7 +1795,6 @@ secrets:
17961795
Source: "config",
17971796
},
17981797
},
1799-
Scale: 1,
18001798
Secrets: []types.ServiceSecretConfig{
18011799
{
18021800
Source: "secret",
@@ -1866,7 +1864,6 @@ secrets:
18661864
Source: "config",
18671865
},
18681866
},
1869-
Scale: 1,
18701867
Secrets: []types.ServiceSecretConfig{
18711868
{
18721869
Source: "secret",
@@ -1952,7 +1949,6 @@ func TestLoadWithExtends(t *testing.T) {
19521949
Target: "/var/lib/mysql",
19531950
Bind: &types.ServiceVolumeBind{CreateHostPath: true},
19541951
}},
1955-
Scale: 1,
19561952
},
19571953
}
19581954
assert.Check(t, is.DeepEqual(expServices, actual.Services))
@@ -1982,7 +1978,6 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
19821978
},
19831979
Environment: types.MappingWithEquals{},
19841980
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
1985-
Scale: 1,
19861981
},
19871982
}
19881983
assert.Check(t, is.DeepEqual(expServices, actual.Services))
@@ -2348,7 +2343,6 @@ func TestDeviceWriteBps(t *testing.T) {
23482343
Name: "foo",
23492344
Image: "busybox",
23502345
Environment: types.MappingWithEquals{},
2351-
Scale: 1,
23522346
BlkioConfig: &types.BlkioConfig{
23532347
DeviceReadBps: []types.ThrottleDevice{
23542348
{
@@ -2392,7 +2386,6 @@ volumes:
23922386
Name: "foo",
23932387
Image: "busybox",
23942388
Environment: types.MappingWithEquals{},
2395-
Scale: 1,
23962389
Volumes: []types.ServiceVolumeConfig{
23972390
{
23982391
Type: types.VolumeTypeVolume,
@@ -2418,7 +2411,6 @@ services:
24182411
Name: "x-foo",
24192412
Image: "busybox",
24202413
Environment: types.MappingWithEquals{},
2421-
Scale: 1,
24222414
},
24232415
})
24242416
}
@@ -2448,7 +2440,6 @@ services:
24482440
Name: "foo",
24492441
Image: "busybox",
24502442
Environment: types.MappingWithEquals{},
2451-
Scale: 1,
24522443
DependsOn: types.DependsOnConfig{"imported": {Condition: "service_started", Required: true}},
24532444
},
24542445
{
@@ -2459,7 +2450,6 @@ services:
24592450
filepath.Join(workingDir, "testdata", "subdir", "extra.env"),
24602451
},
24612452
Image: "nginx",
2462-
Scale: 1,
24632453
Volumes: []types.ServiceVolumeConfig{
24642454
{
24652455
Type: "bind",
@@ -2586,7 +2576,6 @@ services:
25862576
Name: "foo",
25872577
Image: "nginx",
25882578
Environment: types.MappingWithEquals{},
2589-
Scale: 1,
25902579
DependsOn: types.DependsOnConfig{
25912580
"bar": {Condition: types.ServiceConditionStarted, Required: true},
25922581
"baz": {Condition: types.ServiceConditionHealthy, Required: false},
@@ -2640,7 +2629,6 @@ services:
26402629
EnvFile: types.StringList{
26412630
filepath.Join(config.WorkingDir, "testdata", "remote", "env"),
26422631
},
2643-
Scale: 1,
26442632
Volumes: []types.ServiceVolumeConfig{
26452633
{
26462634
Type: types.VolumeTypeBind,
@@ -2833,8 +2821,12 @@ services:
28332821
memswap_limit: 640kb
28342822
`)
28352823
assert.NilError(t, err)
2836-
assert.Equal(t, project.Services[0].MemSwapLimit, types.UnitBytes(-1))
2837-
assert.Equal(t, project.Services[1].MemSwapLimit, types.UnitBytes(640*1024))
2824+
test1, err := project.GetService("test1")
2825+
assert.NilError(t, err)
2826+
assert.Equal(t, test1.MemSwapLimit, types.UnitBytes(-1))
2827+
test2, err := project.GetService("test2")
2828+
assert.NilError(t, err)
2829+
assert.Equal(t, test2.MemSwapLimit, types.UnitBytes(640*1024))
28382830
}
28392831

28402832
func TestBuildUlimits(t *testing.T) {

loader/loader_yaml_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ services:
5151
"image": "bar",
5252
"command": "echo world",
5353
"init": false,
54-
"scale": 1,
5554
},
5655
},
5756
})

loader/normalize.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ func Normalize(project *types.Project) error {
116116
return err
117117
}
118118

119-
err = relocateScale(&s)
120-
if err != nil {
121-
return err
122-
}
123-
124119
inferImplicitDependencies(&s)
125120

126121
project.Services[i] = s
@@ -198,21 +193,6 @@ func setIfMissing(d types.DependsOnConfig, service string, dep types.ServiceDepe
198193
return d
199194
}
200195

201-
func relocateScale(s *types.ServiceConfig) error {
202-
scale := uint64(s.Scale)
203-
if scale > 1 {
204-
logrus.Warn("`scale` is deprecated. Use the `deploy.replicas` element")
205-
if s.Deploy == nil {
206-
s.Deploy = &types.DeployConfig{}
207-
}
208-
if s.Deploy.Replicas != nil && *s.Deploy.Replicas != scale {
209-
return errors.Wrap(errdefs.ErrInvalid, "can't use both 'scale' (deprecated) and 'deploy.replicas'")
210-
}
211-
s.Deploy.Replicas = &scale
212-
}
213-
return nil
214-
}
215-
216196
// Resources with no explicit name are actually named by their key in map
217197
func setNameFromKey(project *types.Project) {
218198
for i, n := range project.Networks {

loader/normalize_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func TestNormalizeNetworkNames(t *testing.T) {
5252
"ZOT": nil,
5353
},
5454
},
55-
Scale: 1,
5655
},
5756
},
5857
}

loader/validate.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ func checkConsistency(project *types.Project) error {
106106
return errors.Wrap(errdefs.ErrInvalid, fmt.Sprintf("service %q refers to undefined secret %s", s.Name, secret.Source))
107107
}
108108
}
109+
110+
if s.Scale != nil && s.Deploy != nil {
111+
if s.Deploy.Replicas != nil && *s.Scale != *s.Deploy.Replicas {
112+
return errors.Wrap(errdefs.ErrInvalid, "can't set distinct values on 'scale' and 'deploy.replicas'")
113+
}
114+
s.Deploy.Replicas = s.Scale
115+
}
116+
109117
}
110118

111119
for name, secret := range project.Secrets {

loader/with-version-struct_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func withVersionServices() []types.ServiceConfig {
4545
"default": nil,
4646
},
4747
VolumesFrom: []string{"other"},
48-
Scale: 1,
4948
},
5049
{
5150
Name: "other",
@@ -56,7 +55,6 @@ func withVersionServices() []types.ServiceConfig {
5655
Volumes: []types.ServiceVolumeConfig{
5756
{Target: "/data", Type: "volume", Volume: &types.ServiceVolumeVolume{}},
5857
},
59-
Scale: 1,
6058
},
6159
}
6260
}

transform/services.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import (
2222

2323
func transformService(data any, p tree.Path) (any, error) {
2424
value := data.(map[string]any)
25-
if _, ok := value["scale"]; !ok {
26-
value["scale"] = 1 // TODO(ndeloof) we should make scale a *int
27-
}
2825
return transformMapping(value, p)
2926
}
3027

types/types.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type ServiceConfig struct {
112112
ReadOnly bool `yaml:"read_only,omitempty" json:"read_only,omitempty"`
113113
Restart string `yaml:"restart,omitempty" json:"restart,omitempty"`
114114
Runtime string `yaml:"runtime,omitempty" json:"runtime,omitempty"`
115-
Scale int `yaml:"scale,omitempty" json:"scale,omitempty"`
115+
Scale *int `yaml:"scale,omitempty" json:"scale,omitempty"`
116116
Secrets []ServiceSecretConfig `yaml:"secrets,omitempty" json:"secrets,omitempty"`
117117
SecurityOpt []string `yaml:"security_opt,omitempty" json:"security_opt,omitempty"`
118118
ShmSize UnitBytes `yaml:"shm_size,omitempty" json:"shm_size,omitempty"`
@@ -138,19 +138,10 @@ type ServiceConfig struct {
138138
func (s ServiceConfig) MarshalYAML() (interface{}, error) {
139139
type t ServiceConfig
140140
value := t(s)
141-
value.Scale = 0 // deprecated, but default value "1" doesn't match omitempty
142141
value.Name = "" // set during map to slice conversion, not part of the yaml representation
143142
return value, nil
144143
}
145144

146-
// MarshalJSON makes ServiceConfig implement json.Marshaller
147-
func (s ServiceConfig) MarshalJSON() ([]byte, error) {
148-
type t ServiceConfig
149-
value := t(s)
150-
value.Scale = 0 // deprecated, but default value "1" doesn't match omitempty
151-
return json.Marshal(value)
152-
}
153-
154145
// NetworksByPriority return the service networks IDs sorted according to Priority
155146
func (s *ServiceConfig) NetworksByPriority() []string {
156147
type key struct {
@@ -323,7 +314,7 @@ type LoggingConfig struct {
323314
// DeployConfig the deployment configuration for a service
324315
type DeployConfig struct {
325316
Mode string `yaml:"mode,omitempty" json:"mode,omitempty"`
326-
Replicas *uint64 `yaml:"replicas,omitempty" json:"replicas,omitempty"`
317+
Replicas *int `yaml:"replicas,omitempty" json:"replicas,omitempty"`
327318
Labels Labels `yaml:"labels,omitempty" json:"labels,omitempty"`
328319
UpdateConfig *UpdateConfig `yaml:"update_config,omitempty" json:"update_config,omitempty"`
329320
RollbackConfig *UpdateConfig `yaml:"rollback_config,omitempty" json:"rollback_config,omitempty"`

0 commit comments

Comments
 (0)