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 f4d39c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 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
20 changes: 0 additions & 20 deletions loader/validate_test.go
Expand Up @@ -17,7 +17,6 @@
package loader

import (
"strings"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -261,25 +260,6 @@ func TestValidateDependsOn(t *testing.T) {
assert.Error(t, err, `service "myservice" depends on undefined service "missingservice": invalid compose project`)
}

func TestValidateContainerName(t *testing.T) {
project := types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
ContainerName: "mycontainer",
},
"myservice2": {
Name: "myservice2",
Image: "scratch",
ContainerName: "mycontainer",
},
},
}
err := checkConsistency(&project)
assert.Assert(t, strings.Contains(err.Error(), `container name "mycontainer" is already in use by`))
}

func TestValidateWatch(t *testing.T) {
t.Run("watch valid configuration", func(t *testing.T) {
project := types.Project{
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 f4d39c4

Please sign in to comment.