Skip to content

Commit 60aa640

Browse files
committed
remove constraint for container_name unicity while loading model
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 5422e49 commit 60aa640

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

loader/validate.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

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

148-
if s.ContainerName != "" {
149-
if existing, ok := containerNames[s.ContainerName]; ok {
150-
return fmt.Errorf(`"services.%s": container name "%s" is already in use by "services.%s": %w`, s.Name, s.ContainerName, existing, errdefs.ErrInvalid)
151-
}
152-
containerNames[s.ContainerName] = s.Name
153-
}
154-
155147
if s.GetScale() > 1 && s.ContainerName != "" {
156148
attr := "scale"
157149
if s.Scale == nil {

loader/validate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func TestValidateDependsOn(t *testing.T) {
262262
}
263263

264264
func TestValidateContainerName(t *testing.T) {
265-
project := types.Project{
265+
project := &types.Project{
266266
Services: types.Services{
267267
"myservice": {
268268
Name: "myservice",
@@ -276,7 +276,7 @@ func TestValidateContainerName(t *testing.T) {
276276
},
277277
},
278278
}
279-
err := checkConsistency(&project)
279+
err := project.CheckContainerNameUnicity()
280280
assert.Assert(t, strings.Contains(err.Error(), `container name "mycontainer" is already in use by`))
281281
}
282282

types/project.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,17 @@ func (p *Project) WithServicesTransform(fn func(name string, s ServiceConfig) (S
696696
}
697697
return newProject, eg.Wait()
698698
}
699+
700+
// CheckContainerNameUnicity validate project doesn't have services declaring the same container_name
701+
func (p *Project) CheckContainerNameUnicity() error {
702+
names := utils.Set[string]{}
703+
for name, s := range p.Services {
704+
if s.ContainerName != "" {
705+
if existing, ok := names[s.ContainerName]; ok {
706+
return fmt.Errorf(`services.%s: container name %q is already in use by service %s"`, name, s.ContainerName, existing)
707+
}
708+
names.Add(s.ContainerName)
709+
}
710+
}
711+
return nil
712+
}

0 commit comments

Comments
 (0)