Skip to content

Commit 6e38069

Browse files
committed
check conflict between scale and container_name
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 668f1f2 commit 6e38069

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

loader/validate.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,23 @@ func checkConsistency(project *types.Project) error {
109109

110110
if s.Scale != nil && s.Deploy != nil {
111111
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'")
112+
return errors.Wrapf(errdefs.ErrInvalid,
113+
"services.%s: can't set distinct values on 'scale' and 'deploy.replicas'",
114+
s.Name)
113115
}
114116
s.Deploy.Replicas = s.Scale
115117
}
116118

119+
if s.GetScale() > 1 && s.ContainerName != "" {
120+
attr := "scale"
121+
if s.Scale == nil {
122+
attr = "deploy.replicas"
123+
}
124+
return errors.Wrapf(errdefs.ErrInvalid,
125+
"services.%s: can't set container_name and %s as container name must be unique",
126+
attr,
127+
s.Name)
128+
}
117129
}
118130

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

types/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ func (s *ServiceConfig) NetworksByPriority() []string {
169169
return sorted
170170
}
171171

172+
func (s *ServiceConfig) GetScale() int {
173+
if s.Scale != nil {
174+
return *s.Scale
175+
}
176+
if s.Deploy != nil && s.Deploy.Replicas != nil {
177+
// this should not be required as compose-go enforce consistency between scale anr replicas
178+
return *s.Deploy.Replicas
179+
}
180+
return 1
181+
}
182+
183+
func (s *ServiceConfig) SetScale(scale int) {
184+
s.Scale = &scale
185+
if s.Deploy != nil {
186+
s.Deploy.Replicas = &scale
187+
}
188+
}
189+
172190
const (
173191
// PullPolicyAlways always pull images
174192
PullPolicyAlways = "always"

0 commit comments

Comments
 (0)