Skip to content

Commit

Permalink
check conflict between scale and container_name
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 21, 2023
1 parent 668f1f2 commit 6e38069
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion loader/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,23 @@ func checkConsistency(project *types.Project) error {

if s.Scale != nil && s.Deploy != nil {
if s.Deploy.Replicas != nil && *s.Scale != *s.Deploy.Replicas {
return errors.Wrap(errdefs.ErrInvalid, "can't set distinct values on 'scale' and 'deploy.replicas'")
return errors.Wrapf(errdefs.ErrInvalid,
"services.%s: can't set distinct values on 'scale' and 'deploy.replicas'",
s.Name)
}
s.Deploy.Replicas = s.Scale
}

if s.GetScale() > 1 && s.ContainerName != "" {
attr := "scale"
if s.Scale == nil {
attr = "deploy.replicas"
}
return errors.Wrapf(errdefs.ErrInvalid,
"services.%s: can't set container_name and %s as container name must be unique",
attr,
s.Name)
}
}

for name, secret := range project.Secrets {
Expand Down
18 changes: 18 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ func (s *ServiceConfig) NetworksByPriority() []string {
return sorted
}

func (s *ServiceConfig) GetScale() int {
if s.Scale != nil {
return *s.Scale
}
if s.Deploy != nil && s.Deploy.Replicas != nil {
// this should not be required as compose-go enforce consistency between scale anr replicas
return *s.Deploy.Replicas
}
return 1
}

func (s *ServiceConfig) SetScale(scale int) {
s.Scale = &scale
if s.Deploy != nil {
s.Deploy.Replicas = &scale
}
}

const (
// PullPolicyAlways always pull images
PullPolicyAlways = "always"
Expand Down

0 comments on commit 6e38069

Please sign in to comment.