From f4d39c4b94220d8ff34fff0c6e15944c6fe98256 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 11 Apr 2024 09:03:58 +0200 Subject: [PATCH] remove constraint for container_name unicity while loading model Signed-off-by: Nicolas De Loof --- loader/validate.go | 8 -------- loader/validate_test.go | 20 -------------------- types/project.go | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/loader/validate.go b/loader/validate.go index 973e5213..cfaf139f 100644 --- a/loader/validate.go +++ b/loader/validate.go @@ -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) @@ -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 { diff --git a/loader/validate_test.go b/loader/validate_test.go index d4a2de68..c70788f6 100644 --- a/loader/validate_test.go +++ b/loader/validate_test.go @@ -17,7 +17,6 @@ package loader import ( - "strings" "testing" "gotest.tools/v3/assert" @@ -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{ diff --git a/types/project.go b/types/project.go index 780dd91c..b86a563f 100644 --- a/types/project.go +++ b/types/project.go @@ -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 +}