Skip to content

Commit bfdb619

Browse files
committed
avoid use of service.Name when iterating on project.Services
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 33951a4 commit bfdb619

File tree

14 files changed

+54
-68
lines changed

14 files changed

+54
-68
lines changed

cmd/compose/compose.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
230230
return nil, err
231231
}
232232

233-
for i, s := range project.Services {
233+
for name, s := range project.Services {
234234
s.CustomLabels = map[string]string{
235235
api.ProjectLabel: project.Name,
236-
api.ServiceLabel: s.Name,
236+
api.ServiceLabel: name,
237237
api.VersionLabel: api.ComposeVersion,
238238
api.WorkingDirLabel: project.WorkingDir,
239239
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
@@ -242,7 +242,7 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
242242
if len(o.EnvFiles) != 0 {
243243
s.CustomLabels[api.EnvironmentFileLabel] = strings.Join(o.EnvFiles, ",")
244244
}
245-
project.Services[i] = s
245+
project.Services[name] = s
246246
}
247247

248248
project.WithoutUnnecessaryResources()

cmd/compose/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err
210210
if err != nil {
211211
return err
212212
}
213-
fmt.Fprintf(dockerCli.Out(), "%s %s\n", s.Name, hash)
213+
fmt.Fprintf(dockerCli.Out(), "%s %s\n", name, hash)
214214
}
215215
return nil
216216
}

cmd/compose/options.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ import (
2525

2626
func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
2727
defaultPlatform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
28-
for _, service := range project.Services {
28+
for name, service := range project.Services {
2929
if service.Build == nil {
3030
continue
3131
}
3232

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

4141
if service.Platform != "" {
4242
if len(service.Build.Platforms) > 0 {
4343
if !utils.StringContains(service.Build.Platforms, service.Platform) {
44-
return fmt.Errorf("service %q build configuration does not support platform: %s", service.Name, service.Platform)
44+
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
4545
}
4646
}
4747

@@ -68,6 +68,7 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
6868
// empty indicates that the builder gets to decide
6969
service.Build.Platforms = nil
7070
}
71+
project.Services[name] = service
7172
}
7273
return nil
7374
}

cmd/compose/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestApplyPlatforms_UnsupportedPlatform(t *testing.T) {
101101
"DOCKER_DEFAULT_PLATFORM": "commodore/64",
102102
},
103103
Services: types.Services{
104-
"foo": {
104+
"test": {
105105
Name: "test",
106106
Image: "foo",
107107
Build: &types.BuildConfig{

cmd/compose/run.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func (options runOptions) apply(project *types.Project) error {
105105
target.Volumes = append(target.Volumes, volume)
106106
}
107107

108-
for i, s := range project.Services {
109-
if s.Name == options.Service {
110-
project.Services[i] = target
108+
for name := range project.Services {
109+
if name == options.Service {
110+
project.Services[name] = target
111111
break
112112
}
113113
}
@@ -279,10 +279,10 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
279279
QuietPull: options.quietPull,
280280
}
281281

282-
for i, service := range project.Services {
283-
if service.Name == options.Service {
282+
for name, service := range project.Services {
283+
if name == options.Service {
284284
service.StdinOpen = options.interactive
285-
project.Services[i] = service
285+
project.Services[name] = service
286286
}
287287
}
288288

@@ -301,7 +301,7 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
301301
dependencies := types.Services{}
302302
var requestedService types.ServiceConfig
303303
for name, service := range project.Services {
304-
if service.Name != requestedServiceName {
304+
if name != requestedServiceName {
305305
dependencies[name] = service
306306
} else {
307307
requestedService = service

cmd/compose/scale.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,12 @@ func runScale(ctx context.Context, dockerCli command.Cli, backend api.Service, o
7373
}
7474

7575
for key, value := range serviceReplicaTuples {
76-
for i, service := range project.Services {
77-
if service.Name != key {
78-
continue
79-
}
80-
value := value
81-
service.Scale = &value
82-
if service.Deploy != nil {
83-
service.Deploy.Replicas = &value
84-
}
85-
project.Services[i] = service
86-
break
76+
service, err := project.GetService(key)
77+
if err != nil {
78+
return err
8779
}
80+
service.SetScale(value)
81+
project.Services[key] = service
8882
}
8983

9084
return backend.Scale(ctx, project, api.ScaleOptions{Services: services})

cmd/compose/up.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,11 @@ func runUp(
262262
}
263263

264264
func setServiceScale(project *types.Project, name string, replicas int) error {
265-
for i, s := range project.Services {
266-
if s.Name != name {
267-
continue
268-
}
269-
270-
service, err := project.GetService(name)
271-
if err != nil {
272-
return err
273-
}
274-
service.Scale = &replicas
275-
if service.Deploy != nil {
276-
service.Deploy.Replicas = &replicas
277-
}
278-
project.Services[i] = service
279-
return nil
265+
service, err := project.GetService(name)
266+
if err != nil {
267+
return err
280268
}
281-
return fmt.Errorf("unknown service %q", name)
269+
service.SetScale(replicas)
270+
project.Services[name] = service
271+
return nil
282272
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/buger/goterm v1.0.4
9-
github.com/compose-spec/compose-go/v2 v2.0.0-20231123162526-11ef9572f1a4
9+
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.7.7
1212
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
130130
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
131131
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
132132
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
133-
github.com/compose-spec/compose-go/v2 v2.0.0-20231123162526-11ef9572f1a4 h1:Lr78By808iuG+2gTyxIDslRpKQCk/lcRqElKsrhzp+U=
134-
github.com/compose-spec/compose-go/v2 v2.0.0-20231123162526-11ef9572f1a4/go.mod h1:PWCgeD8cxiI/DmdpBM407CuLDrZ2W4xuS6/Z9jAi0YQ=
133+
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1 h1:/A+2QMQVSsAmr9Gn5fm6YwaufjRZmWBnHYjr0oCyGiw=
134+
github.com/compose-spec/compose-go/v2 v2.0.0-beta.1/go.mod h1:PWCgeD8cxiI/DmdpBM407CuLDrZ2W4xuS6/Z9jAi0YQ=
135135
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
136136
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
137137
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=

pkg/api/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,30 @@ type BuildOptions struct {
146146
// Apply mutates project according to build options
147147
func (o BuildOptions) Apply(project *types.Project) error {
148148
platform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
149-
for i, service := range project.Services {
149+
for name, service := range project.Services {
150150
if service.Image == "" && service.Build == nil {
151-
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
151+
return fmt.Errorf("invalid service %q. Must specify either image or build", name)
152152
}
153153

154154
if service.Build == nil {
155155
continue
156156
}
157157
if platform != "" {
158158
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, platform) {
159-
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", service.Name, platform)
159+
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)
160160
}
161161
service.Platform = platform
162162
}
163163
if service.Platform != "" {
164164
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, service.Platform) {
165-
return fmt.Errorf("service %q build configuration does not support platform: %s", service.Name, service.Platform)
165+
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
166166
}
167167
}
168168

169169
service.Build.Pull = service.Build.Pull || o.Pull
170170
service.Build.NoCache = service.Build.NoCache || o.NoCache
171171

172-
project.Services[i] = service
172+
project.Services[name] = service
173173
}
174174
return nil
175175
}

pkg/compose/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
189189
return err
190190
}
191191

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

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

pkg/compose/pull.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
6464
)
6565

6666
i := 0
67-
for _, service := range project.Services {
67+
for name, service := range project.Services {
6868
if service.Image == "" {
6969
w.Event(progress.Event{
70-
ID: service.Name,
70+
ID: name,
7171
Status: progress.Done,
7272
Text: "Skipped - No image to be pulled",
7373
})
@@ -77,15 +77,15 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
7777
switch service.PullPolicy {
7878
case types.PullPolicyNever, types.PullPolicyBuild:
7979
w.Event(progress.Event{
80-
ID: service.Name,
80+
ID: name,
8181
Status: progress.Done,
8282
Text: "Skipped",
8383
})
8484
continue
8585
case types.PullPolicyMissing, types.PullPolicyIfNotPresent:
8686
if imageAlreadyPresent(service.Image, images) {
8787
w.Event(progress.Event{
88-
ID: service.Name,
88+
ID: name,
8989
Status: progress.Done,
9090
Text: "Skipped - Image is already present locally",
9191
})
@@ -95,7 +95,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
9595

9696
if service.Build != nil && opts.IgnoreBuildable {
9797
w.Event(progress.Event{
98-
ID: service.Name,
98+
ID: name,
9999
Status: progress.Done,
100100
Text: "Skipped - Image can be built",
101101
})
@@ -104,7 +104,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
104104

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

114114
imagesBeingPulled[service.Image] = service.Name
115115

116-
idx, service := i, service
116+
idx, name, service := i, name, service
117117
eg.Go(func() error {
118118
_, err := s.pullServiceImage(ctx, service, s.configFile(), w, false, project.Environment["DOCKER_DEFAULT_PLATFORM"])
119119
if err != nil {
@@ -124,7 +124,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
124124
if !opts.IgnoreFailures && service.Build == nil {
125125
if s.dryRun {
126126
w.Event(progress.Event{
127-
ID: service.Name,
127+
ID: name,
128128
Status: progress.Error,
129129
Text: fmt.Sprintf(" - Pull error for image: %s", service.Image),
130130
})

pkg/compose/viz_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ func TestViz(t *testing.T) {
149149

150150
// check edges that SHOULD exist in the generated graph
151151
allowedEdges := make(map[string][]string)
152-
for _, service := range project.Services {
153-
allowedEdges[service.Name] = make([]string, 0, len(service.DependsOn))
152+
for name, service := range project.Services {
153+
allowed := make([]string, 0, len(service.DependsOn))
154154
for depName := range service.DependsOn {
155-
allowedEdges[service.Name] = append(allowedEdges[service.Name], depName)
155+
allowed = append(allowed, depName)
156156
}
157+
allowedEdges[name] = allowed
157158
}
158159
for serviceName, dependencies := range allowedEdges {
159160
for _, dependencyName := range dependencies {
@@ -163,12 +164,12 @@ func TestViz(t *testing.T) {
163164

164165
// check edges that SHOULD NOT exist in the generated graph
165166
forbiddenEdges := make(map[string][]string)
166-
for _, service := range project.Services {
167-
forbiddenEdges[service.Name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
167+
for name, service := range project.Services {
168+
forbiddenEdges[name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
168169
for _, serviceName := range project.ServiceNames() {
169170
_, edgeExists := service.DependsOn[serviceName]
170171
if !edgeExists {
171-
forbiddenEdges[service.Name] = append(forbiddenEdges[service.Name], serviceName)
172+
forbiddenEdges[name] = append(forbiddenEdges[name], serviceName)
172173
}
173174
}
174175
}

pkg/e2e/e2e_config_plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818

1919
package e2e
2020

21-
const composeStandaloneMode = false
21+
const composeStandaloneMode = true

0 commit comments

Comments
 (0)