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 5, 2023
1 parent 138face commit 26aca86
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 92 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)
}
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)
}
}

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
}
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 @@ -260,21 +260,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
}
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 @@ -132,8 +132,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
5 changes: 1 addition & 4 deletions internal/sync/docker_cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func (d *DockerCopy) Sync(ctx context.Context, service types.ServiceConfig, path
}

func (d *DockerCopy) sync(ctx context.Context, service types.ServiceConfig, pathMapping PathMapping) error {
scale := 1
if service.Scale != nil {
scale = *service.Scale
}
scale := service.GetScale()

if fi, statErr := os.Stat(pathMapping.HostPath); statErr == nil {
if fi.IsDir() {
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)
}

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)
}
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)
}
}

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
9 changes: 5 additions & 4 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 Expand Up @@ -261,7 +261,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
}

// set digest as com.docker.compose.image label so we can detect outdated containers
for _, service := range project.Services {
for name, service := range project.Services {
image := api.GetImageNameOrDefault(service, project.Name)
digest, ok := images[image]
if ok {
Expand All @@ -270,6 +270,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
}
service.CustomLabels.Add(api.ImageDigestLabel, digest)
}
project.Services[name] = service
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
Image: c.Image,
Labels: c.Labels,
}
set[serviceLabel] = service

}
service.Scale = increment(service.Scale)
set[serviceLabel] = service
}
for name, service := range set {
dependencies := service.Labels[api.DependenciesLabel]
Expand Down
30 changes: 12 additions & 18 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
return err
}

func getScale(config types.ServiceConfig) (int, error) {
scale := config.GetScale()
if scale > 1 && config.ContainerName != "" {
return 0, fmt.Errorf(doubledContainerNameWarning,
config.Name,
config.ContainerName)
}
return scale, nil
}

// resolveServiceReferences replaces reference to another service with reference to an actual container
func (c *convergence) resolveServiceReferences(service *types.ServiceConfig) error {
err := c.resolveVolumeFrom(service)
Expand Down Expand Up @@ -428,7 +438,7 @@ func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceD
}
}
return false, err
} else if service.Scale != nil && *service.Scale == 0 {
} else if service.GetScale() == 0 {
// don't wait for the dependency which configured to have 0 containers running
return false, nil
}
Expand All @@ -455,22 +465,6 @@ func nextContainerNumber(containers []moby.Container) int {

}

func getScale(config types.ServiceConfig) (int, error) {
scale := 1
if config.Scale != nil {
scale = *config.Scale
} else if config.Deploy != nil && config.Deploy.Replicas != nil {
// this should not be required as compose-go enforce consistency between scale anr replicas
scale = *config.Deploy.Replicas
}
if scale > 1 && config.ContainerName != "" {
return 0, fmt.Errorf(doubledContainerNameWarning,
config.Name,
config.ContainerName)
}
return scale, nil
}

func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
name string, number int, opts createOptions) (container moby.Container, err error) {
w := progress.ContextWriter(ctx)
Expand Down Expand Up @@ -754,7 +748,7 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
}

if len(containers) == 0 {
if scale, err := getScale(service); err != nil && scale == 0 {
if service.GetScale() == 0 {
return nil
}
return fmt.Errorf("service %q has no container to start", service.Name)
Expand Down
Loading

0 comments on commit 26aca86

Please sign in to comment.