Skip to content

Commit

Permalink
remove constraint for container_name unicity while loading model
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Apr 16, 2024
1 parent 5422e49 commit 60aa640
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 0 additions & 8 deletions loader/validate.go
Expand Up @@ -28,7 +28,6 @@ import (

// checkConsistency validate a compose model is consistent
func checkConsistency(project *types.Project) error {
containerNames := map[string]string{}
for _, s := range project.Services {
if s.Build == nil && s.Image == "" {
return fmt.Errorf("service %q has neither an image nor a build context specified: %w", s.Name, errdefs.ErrInvalid)
Expand Down Expand Up @@ -145,13 +144,6 @@ func checkConsistency(project *types.Project) error {
}
}

if s.ContainerName != "" {
if existing, ok := containerNames[s.ContainerName]; ok {
return fmt.Errorf(`"services.%s": container name "%s" is already in use by "services.%s": %w`, s.Name, s.ContainerName, existing, errdefs.ErrInvalid)
}
containerNames[s.ContainerName] = s.Name
}

if s.GetScale() > 1 && s.ContainerName != "" {
attr := "scale"
if s.Scale == nil {
Expand Down
4 changes: 2 additions & 2 deletions loader/validate_test.go
Expand Up @@ -262,7 +262,7 @@ func TestValidateDependsOn(t *testing.T) {
}

func TestValidateContainerName(t *testing.T) {
project := types.Project{
project := &types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Expand All @@ -276,7 +276,7 @@ func TestValidateContainerName(t *testing.T) {
},
},
}
err := checkConsistency(&project)
err := project.CheckContainerNameUnicity()
assert.Assert(t, strings.Contains(err.Error(), `container name "mycontainer" is already in use by`))
}

Expand Down
14 changes: 14 additions & 0 deletions types/project.go
Expand Up @@ -696,3 +696,17 @@ func (p *Project) WithServicesTransform(fn func(name string, s ServiceConfig) (S
}
return newProject, eg.Wait()
}

// CheckContainerNameUnicity validate project doesn't have services declaring the same container_name
func (p *Project) CheckContainerNameUnicity() error {
names := utils.Set[string]{}
for name, s := range p.Services {
if s.ContainerName != "" {
if existing, ok := names[s.ContainerName]; ok {
return fmt.Errorf(`services.%s: container name %q is already in use by service %s"`, name, s.ContainerName, existing)
}
names.Add(s.ContainerName)
}
}
return nil
}

0 comments on commit 60aa640

Please sign in to comment.