From 62ded22a53202541b69382768d676c9492318afb Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 22 Sep 2021 09:57:31 +0200 Subject: [PATCH] define compose container names with a valid hostname Signed-off-by: Nicolas De Loof --- cmd/compose/compose.go | 19 +++++++++++-------- pkg/compose/compose.go | 2 ++ pkg/compose/convergence.go | 2 +- pkg/e2e/cascade_stop_test.go | 4 ++-- pkg/e2e/compose_exec_test.go | 8 ++++---- pkg/e2e/compose_run_test.go | 2 +- pkg/e2e/compose_test.go | 14 +++++++------- pkg/e2e/ipc_test.go | 6 +++--- pkg/e2e/networks_test.go | 2 +- pkg/e2e/restart_test.go | 4 ++-- pkg/e2e/start_stop_test.go | 4 ++-- pkg/e2e/volumes_test.go | 2 +- 12 files changed, 37 insertions(+), 32 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 7f1834e51d1..4acabd85643 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -91,12 +91,13 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error { var Warning string type projectOptions struct { - ProjectName string - Profiles []string - ConfigPaths []string - WorkDir string - ProjectDir string - EnvFile string + ProjectName string + Profiles []string + ConfigPaths []string + WorkDir string + ProjectDir string + EnvFile string + Compatibility bool } // ProjectFunc does stuff within a types.Project @@ -149,9 +150,8 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) { f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.") f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)") f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the Compose file)") - f.Bool("compatibility", false, "DEPRECATED") + f.BoolVar(&o.Compatibility, "compatibility", false, "Run compose in backward compatibility mode") _ = f.MarkHidden("workdir") - _ = f.MarkHidden("compatibility") } func (o *projectOptions) toProjectName() (string, error) { @@ -259,6 +259,9 @@ func RootCommand(backend api.Service) *cobra.Command { opts.ProjectDir = opts.WorkDir fmt.Fprint(os.Stderr, aec.Apply("option '--workdir' is DEPRECATED at root level! Please use '--project-directory' instead.\n", aec.RedF)) } + if opts.Compatibility || os.Getenv("COMPOSE_COMPATIBILITY") == "true" { + compose.Separator = "_" + } return nil }, } diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 0cab4f3636c..16bb53ae331 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -32,6 +32,8 @@ import ( "github.com/sanathkr/go-yaml" ) +var Separator = "-" + // NewComposeService create a local implementation of the compose.Service API func NewComposeService(apiClient client.APIClient, configFile *configfile.ConfigFile) api.Service { return &composeService{ diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index 632726229af..50bdf86a603 100644 --- a/pkg/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -245,7 +245,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project, } func getContainerName(projectName string, service types.ServiceConfig, number int) string { - name := fmt.Sprintf("%s_%s_%d", projectName, service.Name, number) + name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator) if service.ContainerName != "" { name = service.ContainerName } diff --git a/pkg/e2e/cascade_stop_test.go b/pkg/e2e/cascade_stop_test.go index 7b0cb80a106..d6470ff2b8d 100644 --- a/pkg/e2e/cascade_stop_test.go +++ b/pkg/e2e/cascade_stop_test.go @@ -29,13 +29,13 @@ func TestCascadeStop(t *testing.T) { t.Run("abort-on-container-exit", func(t *testing.T) { res := c.RunDockerOrExitError("compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--abort-on-container-exit") - res.Assert(t, icmd.Expected{ExitCode: 1, Out: `should_fail_1 exited with code 1`}) + res.Assert(t, icmd.Expected{ExitCode: 1, Out: `should_fail-1 exited with code 1`}) res.Assert(t, icmd.Expected{ExitCode: 1, Out: `Aborting on container exit...`}) }) t.Run("exit-code-from", func(t *testing.T) { res := c.RunDockerOrExitError("compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=sleep") - res.Assert(t, icmd.Expected{ExitCode: 137, Out: `should_fail_1 exited with code 1`}) + res.Assert(t, icmd.Expected{ExitCode: 137, Out: `should_fail-1 exited with code 1`}) res.Assert(t, icmd.Expected{ExitCode: 137, Out: `Aborting on container exit...`}) }) diff --git a/pkg/e2e/compose_exec_test.go b/pkg/e2e/compose_exec_test.go index 36e6b970e4e..e3f58548c8c 100644 --- a/pkg/e2e/compose_exec_test.go +++ b/pkg/e2e/compose_exec_test.go @@ -32,17 +32,17 @@ func TestLocalComposeExec(t *testing.T) { c.RunDockerCmd("compose", "--project-directory", "fixtures/simple-composefile", "--project-name", projectName, "up", "-d") t.Run("exec true", func(t *testing.T) { - res := c.RunDockerOrExitError("exec", "compose-e2e-exec_simple_1", "/bin/true") + res := c.RunDockerOrExitError("exec", "compose-e2e-exec-simple-1", "/bin/true") res.Assert(t, icmd.Expected{ExitCode: 0}) }) t.Run("exec false", func(t *testing.T) { - res := c.RunDockerOrExitError("exec", "compose-e2e-exec_simple_1", "/bin/false") + res := c.RunDockerOrExitError("exec", "compose-e2e-exec-simple-1", "/bin/false") res.Assert(t, icmd.Expected{ExitCode: 1}) }) t.Run("exec with env set", func(t *testing.T) { - res := icmd.RunCmd(c.NewDockerCmd("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env"), + res := icmd.RunCmd(c.NewDockerCmd("exec", "-e", "FOO", "compose-e2e-exec-simple-1", "/usr/bin/env"), func(cmd *icmd.Cmd) { cmd.Env = append(cmd.Env, "FOO=BAR") }) @@ -50,7 +50,7 @@ func TestLocalComposeExec(t *testing.T) { }) t.Run("exec without env set", func(t *testing.T) { - res := c.RunDockerOrExitError("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env") + res := c.RunDockerOrExitError("exec", "-e", "FOO", "compose-e2e-exec-simple-1", "/usr/bin/env") res.Assert(t, icmd.Expected{ExitCode: 0}) assert.Check(t, !strings.Contains(res.Stdout(), "FOO=")) }) diff --git a/pkg/e2e/compose_run_test.go b/pkg/e2e/compose_run_test.go index ce24fc6530e..c361db51604 100644 --- a/pkg/e2e/compose_run_test.go +++ b/pkg/e2e/compose_run_test.go @@ -54,7 +54,7 @@ func TestLocalComposeRun(t *testing.T) { truncatedSlug = strings.Replace(containerID, "run-test_back_run_", "", 1) runContainerID = containerID } - if strings.HasPrefix(containerID, "run-test_db_1") { + if strings.HasPrefix(containerID, "run-test-db-1") { assert.Assert(t, strings.Contains(line, "Up"), line) } } diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index 11a3b947db9..3612fdf1e6b 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -71,7 +71,7 @@ func TestLocalComposeUp(t *testing.T) { }) t.Run("check compose labels", func(t *testing.T) { - res := c.RunDockerCmd("inspect", projectName+"_web_1") + res := c.RunDockerCmd("inspect", projectName+"-web-1") res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`}) @@ -88,26 +88,26 @@ func TestLocalComposeUp(t *testing.T) { }) t.Run("check user labels", func(t *testing.T) { - res := c.RunDockerCmd("inspect", projectName+"_web_1") + res := c.RunDockerCmd("inspect", projectName+"-web-1") res.Assert(t, icmd.Expected{Out: `"my-label": "test"`}) }) t.Run("check healthcheck output", func(t *testing.T) { c.WaitForCmdResult(c.NewDockerCmd("compose", "-p", projectName, "ps", "--format", "json"), - StdoutContains(`"Name":"compose-e2e-demo_web_1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`), + StdoutContains(`"Name":"compose-e2e-demo-web-1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`), 5*time.Second, 1*time.Second) res := c.RunDockerCmd("compose", "-p", projectName, "ps") res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`}) - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_web_1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp`}) - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_db_1 "docker-entrypoint.s…" db running 5432/tcp`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 "docker-entrypoint.s…" db running 5432/tcp`}) }) t.Run("images", func(t *testing.T) { res := c.RunDockerCmd("compose", "-p", projectName, "images") - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_db_1 gtardif/sentences-db latest`}) - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_web_1 gtardif/sentences-web latest`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`}) res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_words_1 gtardif/sentences-api latest`}) }) diff --git a/pkg/e2e/ipc_test.go b/pkg/e2e/ipc_test.go index 4bf2d709ede..52aea745443 100644 --- a/pkg/e2e/ipc_test.go +++ b/pkg/e2e/ipc_test.go @@ -44,13 +44,13 @@ func TestIPC(t *testing.T) { }) t.Run("check ipcmode in container inspect", func(t *testing.T) { - res := c.RunDockerCmd("inspect", projectName+"_shareable_1") + res := c.RunDockerCmd("inspect", projectName+"-shareable-1") res.Assert(t, icmd.Expected{Out: `"IpcMode": "shareable",`}) - res = c.RunDockerCmd("inspect", projectName+"_service_1") + res = c.RunDockerCmd("inspect", projectName+"-service-1") res.Assert(t, icmd.Expected{Out: `"IpcMode": "container:`}) - res = c.RunDockerCmd("inspect", projectName+"_container_1") + res = c.RunDockerCmd("inspect", projectName+"-container-1") res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`"IpcMode": "container:%s",`, cid)}) }) diff --git a/pkg/e2e/networks_test.go b/pkg/e2e/networks_test.go index 20d1f15e7f6..3ebaedea132 100644 --- a/pkg/e2e/networks_test.go +++ b/pkg/e2e/networks_test.go @@ -107,7 +107,7 @@ func TestIPAMConfig(t *testing.T) { }) t.Run("ensure service get fixed IP assigned", func(t *testing.T) { - res := c.RunDockerCmd("inspect", projectName+"_foo_1", "-f", "{{ .NetworkSettings.Networks."+projectName+"_default.IPAddress }}") + res := c.RunDockerCmd("inspect", projectName+"-foo-1", "-f", "{{ .NetworkSettings.Networks."+projectName+"_default.IPAddress }}") res.Assert(t, icmd.Expected{Out: "10.1.0.100"}) }) diff --git a/pkg/e2e/restart_test.go b/pkg/e2e/restart_test.go index dee349ff9b5..16bc80492a7 100644 --- a/pkg/e2e/restart_test.go +++ b/pkg/e2e/restart_test.go @@ -32,7 +32,7 @@ func TestRestart(t *testing.T) { getServiceRegx := func(service string, status string) string { // match output with random spaces like: - // e2e-start-stop_db_1 "echo hello" db running + // e2e-start-stop-db-1 "echo hello" db running return fmt.Sprintf("%s_%s_1.+%s\\s+%s", projectName, service, service, status) } @@ -41,7 +41,7 @@ func TestRestart(t *testing.T) { c.RunDockerOrExitError("compose", "--project-name", projectName, "down") res := c.RunDockerOrExitError("compose", "-f", "./fixtures/restart-test/compose.yaml", "--project-name", projectName, "up", "-d") - assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart_restart_1 Started"), res.Combined()) + assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-restart-1 Started"), res.Combined()) c.WaitForCmdResult(c.NewDockerCmd("compose", "--project-name", projectName, "ps", "-a", "--format", "json"), StdoutContains(`"State":"exited"`), diff --git a/pkg/e2e/start_stop_test.go b/pkg/e2e/start_stop_test.go index 5d897d08f4f..d10a60dae88 100644 --- a/pkg/e2e/start_stop_test.go +++ b/pkg/e2e/start_stop_test.go @@ -37,13 +37,13 @@ func TestStartStop(t *testing.T) { getServiceRegx := func(service string, status string) string { // match output with random spaces like: - // e2e-start-stop_db_1 "echo hello" db running + // e2e-start-stop-db-1 "echo hello" db running return fmt.Sprintf("%s_%s_1.+%s\\s+%s", projectName, service, service, status) } t.Run("Up a project", func(t *testing.T) { res := c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "up", "-d") - assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop_simple_1 Started"), res.Combined()) + assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-simple-1 Started"), res.Combined()) res = c.RunDockerCmd("compose", "ls", "--all") testify.Regexp(t, getProjectRegx("running"), res.Stdout()) diff --git a/pkg/e2e/volumes_test.go b/pkg/e2e/volumes_test.go index 051cf73a0b7..aad16a46fa5 100644 --- a/pkg/e2e/volumes_test.go +++ b/pkg/e2e/volumes_test.go @@ -62,7 +62,7 @@ func TestLocalComposeVolume(t *testing.T) { }) t.Run("check container bind-mounts specs", func(t *testing.T) { - res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx_1", "--format", "{{ json .Mounts }}") + res := c.RunDockerCmd("inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}") output := res.Stdout() // nolint assert.Assert(t, strings.Contains(output, `"Type":"bind"`))