Skip to content

Commit

Permalink
avoid use of service.Name when iterating on project.Services
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 1, 2023
1 parent 33951a4 commit bfdb619
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 68 deletions.
6 changes: 3 additions & 3 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
return nil, err
}

for i, s := range project.Services {
for name, s := range project.Services {
s.CustomLabels = map[string]string{
api.ProjectLabel: project.Name,
api.ServiceLabel: s.Name,
api.ServiceLabel: name,
api.VersionLabel: api.ComposeVersion,
api.WorkingDirLabel: project.WorkingDir,
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
Expand All @@ -242,7 +242,7 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
if len(o.EnvFiles) != 0 {
s.CustomLabels[api.EnvironmentFileLabel] = strings.Join(o.EnvFiles, ",")
}
project.Services[i] = s
project.Services[name] = s
}

project.WithoutUnnecessaryResources()
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err
if err != nil {
return err
}
fmt.Fprintf(dockerCli.Out(), "%s %s\n", s.Name, hash)
fmt.Fprintf(dockerCli.Out(), "%s %s\n", name, hash)

Check warning on line 213 in cmd/compose/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/config.go#L213

Added line #L213 was not covered by tests
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/compose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ import (

func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
defaultPlatform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
for _, service := range project.Services {
for name, service := range project.Services {
if service.Build == nil {
continue
}

// default platform only applies if the service doesn't specify
if defaultPlatform != "" && service.Platform == "" {
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, defaultPlatform) {
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", service.Name, defaultPlatform)
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, defaultPlatform)
}
service.Platform = defaultPlatform
}

if service.Platform != "" {
if len(service.Build.Platforms) > 0 {
if !utils.StringContains(service.Build.Platforms, service.Platform) {
return fmt.Errorf("service %q build configuration does not support platform: %s", service.Name, service.Platform)
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)

Check warning on line 44 in cmd/compose/options.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/options.go#L44

Added line #L44 was not covered by tests
}
}

Expand All @@ -68,6 +68,7 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
// empty indicates that the builder gets to decide
service.Build.Platforms = nil
}
project.Services[name] = service
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/compose/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestApplyPlatforms_UnsupportedPlatform(t *testing.T) {
"DOCKER_DEFAULT_PLATFORM": "commodore/64",
},
Services: types.Services{
"foo": {
"test": {
Name: "test",
Image: "foo",
Build: &types.BuildConfig{
Expand Down
14 changes: 7 additions & 7 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (options runOptions) apply(project *types.Project) error {
target.Volumes = append(target.Volumes, volume)
}

for i, s := range project.Services {
if s.Name == options.Service {
project.Services[i] = target
for name := range project.Services {
if name == options.Service {
project.Services[name] = target
break
}
}
Expand Down Expand Up @@ -279,10 +279,10 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
QuietPull: options.quietPull,
}

for i, service := range project.Services {
if service.Name == options.Service {
for name, service := range project.Services {
if name == options.Service {
service.StdinOpen = options.interactive
project.Services[i] = service
project.Services[name] = service
}
}

Expand All @@ -301,7 +301,7 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
dependencies := types.Services{}
var requestedService types.ServiceConfig
for name, service := range project.Services {
if service.Name != requestedServiceName {
if name != requestedServiceName {
dependencies[name] = service
} else {
requestedService = service
Expand Down
16 changes: 5 additions & 11 deletions cmd/compose/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,12 @@ func runScale(ctx context.Context, dockerCli command.Cli, backend api.Service, o
}

for key, value := range serviceReplicaTuples {
for i, service := range project.Services {
if service.Name != key {
continue
}
value := value
service.Scale = &value
if service.Deploy != nil {
service.Deploy.Replicas = &value
}
project.Services[i] = service
break
service, err := project.GetService(key)
if err != nil {
return err

Check warning on line 78 in cmd/compose/scale.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/scale.go#L78

Added line #L78 was not covered by tests
}
service.SetScale(value)
project.Services[key] = service
}

return backend.Scale(ctx, project, api.ScaleOptions{Services: services})
Expand Down
22 changes: 6 additions & 16 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,11 @@ func runUp(
}

func setServiceScale(project *types.Project, name string, replicas int) error {
for i, s := range project.Services {
if s.Name != name {
continue
}

service, err := project.GetService(name)
if err != nil {
return err
}
service.Scale = &replicas
if service.Deploy != nil {
service.Deploy.Replicas = &replicas
}
project.Services[i] = service
return nil
service, err := project.GetService(name)
if err != nil {
return err

Check warning on line 267 in cmd/compose/up.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/up.go#L267

Added line #L267 was not covered by tests
}
return fmt.Errorf("unknown service %q", name)
service.SetScale(replicas)
project.Services[name] = service
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/Microsoft/go-winio v0.6.1
github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go/v2 v2.0.0-20231123162526-11ef9572f1a4
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.7.7
github.com/davecgh/go-spew v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
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.0.0-20231123162526-11ef9572f1a4 h1:Lr78By808iuG+2gTyxIDslRpKQCk/lcRqElKsrhzp+U=
github.com/compose-spec/compose-go/v2 v2.0.0-20231123162526-11ef9572f1a4/go.mod h1:PWCgeD8cxiI/DmdpBM407CuLDrZ2W4xuS6/Z9jAi0YQ=
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1 h1:/A+2QMQVSsAmr9Gn5fm6YwaufjRZmWBnHYjr0oCyGiw=
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1/go.mod h1:PWCgeD8cxiI/DmdpBM407CuLDrZ2W4xuS6/Z9jAi0YQ=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,30 @@ type BuildOptions struct {
// Apply mutates project according to build options
func (o BuildOptions) Apply(project *types.Project) error {
platform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
for i, service := range project.Services {
for name, service := range project.Services {
if service.Image == "" && service.Build == nil {
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
return fmt.Errorf("invalid service %q. Must specify either image or build", name)

Check warning on line 151 in pkg/api/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/api.go#L151

Added line #L151 was not covered by tests
}

if service.Build == nil {
continue
}
if platform != "" {
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, platform) {
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", service.Name, platform)
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)

Check warning on line 159 in pkg/api/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/api.go#L159

Added line #L159 was not covered by tests
}
service.Platform = platform
}
if service.Platform != "" {
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, service.Platform) {
return fmt.Errorf("service %q build configuration does not support platform: %s", service.Name, service.Platform)
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)

Check warning on line 165 in pkg/api/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/api.go#L165

Added line #L165 was not covered by tests
}
}

service.Build.Pull = service.Build.Pull || o.Pull
service.Build.NoCache = service.Build.NoCache || o.NoCache

project.Services[i] = service
project.Services[name] = service
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
return err
}

digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w, nodes)
digest, err := s.doBuildBuildkit(ctx, name, buildOptions, w, nodes)
if err != nil {
return err
}
Expand Down Expand Up @@ -221,9 +221,9 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
}

func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project, buildOpts *api.BuildOptions, quietPull bool) error {
for _, service := range project.Services {
for name, service := range project.Services {
if service.Image == "" && service.Build == nil {
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
return fmt.Errorf("invalid service %q. Must specify either image or build", name)
}
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/compose/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
)

i := 0
for _, service := range project.Services {
for name, service := range project.Services {
if service.Image == "" {
w.Event(progress.Event{
ID: service.Name,
ID: name,
Status: progress.Done,
Text: "Skipped - No image to be pulled",
})
Expand All @@ -77,15 +77,15 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
switch service.PullPolicy {
case types.PullPolicyNever, types.PullPolicyBuild:
w.Event(progress.Event{
ID: service.Name,
ID: name,

Check warning on line 80 in pkg/compose/pull.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/pull.go#L80

Added line #L80 was not covered by tests
Status: progress.Done,
Text: "Skipped",
})
continue
case types.PullPolicyMissing, types.PullPolicyIfNotPresent:
if imageAlreadyPresent(service.Image, images) {
w.Event(progress.Event{
ID: service.Name,
ID: name,
Status: progress.Done,
Text: "Skipped - Image is already present locally",
})
Expand All @@ -95,7 +95,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts

if service.Build != nil && opts.IgnoreBuildable {
w.Event(progress.Event{
ID: service.Name,
ID: name,

Check warning on line 98 in pkg/compose/pull.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/pull.go#L98

Added line #L98 was not covered by tests
Status: progress.Done,
Text: "Skipped - Image can be built",
})
Expand All @@ -104,7 +104,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts

if s, ok := imagesBeingPulled[service.Image]; ok {
w.Event(progress.Event{
ID: service.Name,
ID: name,
Status: progress.Done,
Text: fmt.Sprintf("Skipped - Image is already being pulled by %v", s),
})
Expand All @@ -113,7 +113,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts

imagesBeingPulled[service.Image] = service.Name

idx, service := i, service
idx, name, service := i, name, service
eg.Go(func() error {
_, err := s.pullServiceImage(ctx, service, s.configFile(), w, false, project.Environment["DOCKER_DEFAULT_PLATFORM"])
if err != nil {
Expand All @@ -124,7 +124,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
if !opts.IgnoreFailures && service.Build == nil {
if s.dryRun {
w.Event(progress.Event{
ID: service.Name,
ID: name,

Check warning on line 127 in pkg/compose/pull.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/pull.go#L127

Added line #L127 was not covered by tests
Status: progress.Error,
Text: fmt.Sprintf(" - Pull error for image: %s", service.Image),
})
Expand Down
13 changes: 7 additions & 6 deletions pkg/compose/viz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ func TestViz(t *testing.T) {

// check edges that SHOULD exist in the generated graph
allowedEdges := make(map[string][]string)
for _, service := range project.Services {
allowedEdges[service.Name] = make([]string, 0, len(service.DependsOn))
for name, service := range project.Services {
allowed := make([]string, 0, len(service.DependsOn))
for depName := range service.DependsOn {
allowedEdges[service.Name] = append(allowedEdges[service.Name], depName)
allowed = append(allowed, depName)
}
allowedEdges[name] = allowed
}
for serviceName, dependencies := range allowedEdges {
for _, dependencyName := range dependencies {
Expand All @@ -163,12 +164,12 @@ func TestViz(t *testing.T) {

// check edges that SHOULD NOT exist in the generated graph
forbiddenEdges := make(map[string][]string)
for _, service := range project.Services {
forbiddenEdges[service.Name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
for name, service := range project.Services {
forbiddenEdges[name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
for _, serviceName := range project.ServiceNames() {
_, edgeExists := service.DependsOn[serviceName]
if !edgeExists {
forbiddenEdges[service.Name] = append(forbiddenEdges[service.Name], serviceName)
forbiddenEdges[name] = append(forbiddenEdges[name], serviceName)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/e2e/e2e_config_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

package e2e

const composeStandaloneMode = false
const composeStandaloneMode = true

0 comments on commit bfdb619

Please sign in to comment.