diff --git a/go.mod b/go.mod index 227100470..32c770209 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Microsoft/go-winio v0.6.3 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1 + github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af github.com/containerd/console v1.0.5 github.com/containerd/containerd/v2 v2.4.2 github.com/containerd/errdefs v1.0.0 diff --git a/go.sum b/go.sum index d6aa640ec..9acda1765 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,8 @@ github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= -github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1 h1:zlQ2D89Te4kZ0CPSywYwptC3h/fxjvz1rPSff/Eo2aM= -github.com/compose-spec/compose-go/v2 v2.16.2-0.20261012152039-c53b35077dd1/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ= +github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af h1:TOZLcS+qr4t2Xv8Wh0i9ebHLF7nlRt2hZ59pI6s49z8= +github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ= github.com/containerd/cgroups/v3 v3.1.3 h1:eUNflyMddm18+yrDmZPn3jI7C5hJ9ahABE5q6dyLYXQ= github.com/containerd/cgroups/v3 v3.1.3/go.mod h1:PKZ2AcWmSBsY/tJUVhtS/rluX0b1uq1GmPO1ElCmbOw= github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc= diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 601783714..11a82c3f3 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -514,7 +514,9 @@ func (s *composeService) prepareContainerMACAddress(service types.ServiceConfig, func getAliases(project *types.Project, service types.ServiceConfig, serviceIndex int, cfg *types.ServiceNetworkConfig, useNetworkAliases bool) []string { aliases := []string{getContainerName(project.Name, service, serviceIndex)} if useNetworkAliases { - aliases = append(aliases, service.Name) + if cfg.ServiceNameAliasEnabled() { + aliases = append(aliases, service.Name) + } if cfg != nil { aliases = append(aliases, cfg.Aliases...) } diff --git a/pkg/compose/create_test.go b/pkg/compose/create_test.go index 0600cf4ed..41090d940 100644 --- a/pkg/compose/create_test.go +++ b/pkg/compose/create_test.go @@ -360,6 +360,53 @@ func TestCreateEndpointSettings(t *testing.T) { }, cmpopts.EquateComparable(netip.Addr{}))) } +func TestGetAliases(t *testing.T) { + enabled, disabled := true, false + tests := []struct { + name string + cfg *composetypes.ServiceNetworkConfig + useNetworkAliases bool + expected []string + }{ + { + name: "no network config", + useNetworkAliases: true, + expected: []string{"containerName", "serviceName"}, + }, + { + name: "use_service_name_alias unset", + cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}}, + useNetworkAliases: true, + expected: []string{"containerName", "serviceName", "alias1"}, + }, + { + name: "use_service_name_alias true", + cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}, UseServiceNameAlias: &enabled}, + useNetworkAliases: true, + expected: []string{"containerName", "serviceName", "alias1"}, + }, + { + name: "use_service_name_alias false keeps the container name and the aliases", + cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}, UseServiceNameAlias: &disabled}, + useNetworkAliases: true, + expected: []string{"containerName", "alias1"}, + }, + { + name: "network aliases disabled for the run", + cfg: &composetypes.ServiceNetworkConfig{Aliases: []string{"alias1"}}, + useNetworkAliases: false, + expected: []string{"containerName"}, + }, + } + service := composetypes.ServiceConfig{Name: "serviceName", ContainerName: "containerName"} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + aliases := getAliases(&composetypes.Project{Name: "projName"}, service, 0, tt.cfg, tt.useNetworkAliases) + assert.DeepEqual(t, aliases, tt.expected) + }) + } +} + func Test_buildContainerVolumes(t *testing.T) { pwd, err := os.Getwd() assert.NilError(t, err)