Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hash): recreate container on project config content change #11931

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/api/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
ServiceLabel = "com.docker.compose.service"
// ConfigHashLabel stores configuration hash for a compose service
ConfigHashLabel = "com.docker.compose.config-hash"
// ServiceConfigsHash stores configuration hash for a compose service configs
ServiceConfigsHash = "com.docker.compose.service.%s.configs.hash"
// ServiceSecretsHash stores configuration hash for a compose service secrets
ServiceSecretsHash = "com.docker.compose.service.%s.secrets.hash"
// ContainerNumberLabel stores the container index of a replicated service
ContainerNumberLabel = "com.docker.compose.container-number"
// VolumeLabel allow to track resource related to a compose volume
Expand Down
42 changes: 34 additions & 8 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@

sort.Slice(containers, func(i, j int) bool {
// select obsolete containers first, so they get removed as we scale down
if obsolete, _ := c.mustRecreate(service, containers[i], recreate); obsolete {
if obsolete, _ := c.mustRecreate(project, service, containers[i], recreate); obsolete {
// i is obsolete, so must be first in the list
return true
}
if obsolete, _ := c.mustRecreate(service, containers[j], recreate); obsolete {
if obsolete, _ := c.mustRecreate(project, service, containers[j], recreate); obsolete {
// j is obsolete, so must be first in the list
return false
}
Expand Down Expand Up @@ -162,7 +162,7 @@
continue
}

mustRecreate, err := c.mustRecreate(service, container, recreate)
mustRecreate, err := c.mustRecreate(project, service, container, recreate)
if err != nil {
return err
}
Expand Down Expand Up @@ -326,23 +326,49 @@
return nil
}

func (c *convergence) mustRecreate(expected types.ServiceConfig, actual moby.Container, policy string) (bool, error) {
//nolint:gocyclo
func (c *convergence) mustRecreate(project *types.Project, expected types.ServiceConfig, actual moby.Container, policy string) (bool, error) {
if policy == api.RecreateNever {
return false, nil
}
if policy == api.RecreateForce {
return true, nil
}
configHash, err := ServiceHash(expected)
serviceHash, err := ServiceHash(expected)
if err != nil {
return false, err
}
configChanged := actual.Labels[api.ConfigHashLabel] != configHash
imageUpdated := actual.Labels[api.ImageDigestLabel] != expected.CustomLabels[api.ImageDigestLabel]
if configChanged || imageUpdated {

if actual.Labels[api.ConfigHashLabel] != serviceHash {
return true, nil
}

if actual.Labels[api.ImageDigestLabel] != expected.CustomLabels[api.ImageDigestLabel] {
return true, nil
}

serviceNameToConfigHash, err := ServiceConfigsHash(project, expected)
if err != nil {
return false, err
}

Check warning on line 353 in pkg/compose/convergence.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/convergence.go#L352-L353

Added lines #L352 - L353 were not covered by tests

for serviceName, hash := range serviceNameToConfigHash {
if actual.Labels[fmt.Sprintf(api.ServiceConfigsHash, serviceName)] != hash {
return true, nil
}

Check warning on line 358 in pkg/compose/convergence.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/convergence.go#L356-L358

Added lines #L356 - L358 were not covered by tests
}

serviceSecretsHash, err := ServiceSecretsHash(project, expected)
if err != nil {
return false, err
}

Check warning on line 364 in pkg/compose/convergence.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/convergence.go#L363-L364

Added lines #L363 - L364 were not covered by tests

for serviceName, hash := range serviceSecretsHash {
if actual.Labels[fmt.Sprintf(api.ServiceSecretsHash, serviceName)] != hash {
return true, nil
}

Check warning on line 369 in pkg/compose/convergence.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/convergence.go#L367-L369

Added lines #L367 - L369 were not covered by tests
}

if c.networks != nil && actual.State == "running" {
if checkExpectedNetworks(expected, actual, c.networks) {
return true, nil
Expand Down
27 changes: 23 additions & 4 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
inherit *moby.Container,
opts createOptions,
) (createConfigs, error) {
labels, err := s.prepareLabels(opts.Labels, service, number)
labels, err := s.prepareLabels(opts.Labels, p, service, number)
if err != nil {
return createConfigs{}, err
}
Expand Down Expand Up @@ -505,17 +505,36 @@
return parsed, unconfined, nil
}

func (s *composeService) prepareLabels(labels types.Labels, service types.ServiceConfig, number int) (map[string]string, error) {
hash, err := ServiceHash(service)
func (s *composeService) prepareLabels(labels types.Labels, project *types.Project, service types.ServiceConfig, number int) (map[string]string, error) {
serviceHash, err := ServiceHash(service)
if err != nil {
return nil, err
}
labels[api.ConfigHashLabel] = hash

serviceNameToConfigHash, err := ServiceConfigsHash(project, service)
if err != nil {
return nil, err
}

Check warning on line 517 in pkg/compose/create.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/create.go#L516-L517

Added lines #L516 - L517 were not covered by tests

for serviceName, hash := range serviceNameToConfigHash {
labels[fmt.Sprintf(api.ServiceConfigsHash, serviceName)] = hash
}

serviceNameToSecretHash, err := ServiceSecretsHash(project, service)
if err != nil {
return nil, err
}

Check warning on line 526 in pkg/compose/create.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/create.go#L525-L526

Added lines #L525 - L526 were not covered by tests

for serviceName, hash := range serviceNameToSecretHash {
labels[fmt.Sprintf(api.ServiceSecretsHash, serviceName)] = hash
}

if number > 0 {
// One-off containers are not indexed
labels[api.ContainerNumberLabel] = strconv.Itoa(number)
}
labels[api.ConfigHashLabel] = serviceHash
labels[api.ContainerNumberLabel] = strconv.Itoa(number)

var dependencies []string
for s, d := range service.DependsOn {
Expand Down
74 changes: 72 additions & 2 deletions pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package compose

import (
"bytes"
"encoding/json"
"fmt"
"time"

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
"github.com/opencontainers/go-digest"
)

Expand All @@ -35,11 +39,77 @@
o.DependsOn = nil
o.Profiles = nil

bytes, err := json.Marshal(o)
data, err := json.Marshal(o)
if err != nil {
return "", err
}
return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

// ServiceConfigsHash computes the configuration hash for service configs.
func ServiceConfigsHash(project *types.Project, serviceConfig types.ServiceConfig) (map[string]string, error) {
serviceNameToHash := make(map[string]string)
for _, config := range serviceConfig.Configs {
file := project.Configs[config.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(config), types.FileObjectConfig(file))

if err != nil {
return nil, err
}

Check warning on line 58 in pkg/compose/hash.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/hash.go#L57-L58

Added lines #L57 - L58 were not covered by tests

serviceNameToHash[config.Target] = digest.SHA256.FromBytes(b.Bytes()).Encoded()
}

return serviceNameToHash, nil
}

// ServiceSecretsHash computes the configuration hash for service secrets.
func ServiceSecretsHash(project *types.Project, serviceConfig types.ServiceConfig) (map[string]string, error) {
serviceNameToHash := make(map[string]string)
for _, secret := range serviceConfig.Secrets {
file := project.Secrets[secret.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(secret), types.FileObjectConfig(file))

if err != nil {
return nil, err
}

Check warning on line 75 in pkg/compose/hash.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/hash.go#L74-L75

Added lines #L74 - L75 were not covered by tests

serviceNameToHash[secret.Target] = digest.SHA256.FromBytes(b.Bytes()).Encoded()
}

return serviceNameToHash, nil
}

func createTarForConfig(
project *types.Project,
serviceConfig types.FileReferenceConfig,
file types.FileObjectConfig,
) (*bytes.Buffer, error) {
// fixed time to ensure the tarball is deterministic
modTime := time.Unix(0, 0)

if serviceConfig.Target == "" {
serviceConfig.Target = "/" + serviceConfig.Source
}

switch {
case file.Content != "":
return bytes.NewBuffer([]byte(file.Content)), nil
case file.Environment != "":
env, ok := project.Environment[file.Environment]
if !ok {
return nil, fmt.Errorf(
"environment variable %q required by file %q is not set",
file.Environment,
file.Name,
)
}

Check warning on line 106 in pkg/compose/hash.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/hash.go#L101-L106

Added lines #L101 - L106 were not covered by tests
return bytes.NewBuffer([]byte(env)), nil
case file.File != "":
return utils.CreateTarByPath(file.File, modTime)
}

return nil, fmt.Errorf("config %q is empty", file.Name)

Check warning on line 112 in pkg/compose/hash.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/hash.go#L112

Added line #L112 was not covered by tests
}

// NetworkHash computes the configuration hash for a network.
Expand Down
Loading
Loading