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

Conversation

idsulik
Copy link
Contributor

@idsulik idsulik commented Jun 23, 2024

What I did
Fixed hash.ServiceHash() to support config content change

Related issue
#11900

image

@ndeloof
Copy link
Contributor

ndeloof commented Jul 1, 2024

While I understand the intent, I don't like we get the config content added into the service hash. This also only makes sense as the config content is inlined.
I wonder we could rely on a label to track the config state by the time it was created : com.docker.compose.config.name=<config hash>.


for _, serviceConfig := range o.Configs {
if projectConfig, ok := project.Configs[serviceConfig.Source]; ok {
bytes = append(bytes, []byte(projectConfig.Content)...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't detect changes for config set by environment of file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added support for env and config file

@idsulik
Copy link
Contributor Author

idsulik commented Jul 6, 2024

I don't like we get the config content added into the service hash

why?

I wonder we could rely on a label to track the config state by the time it was created : com.docker.compose.config.name=<config hash>.

I don't get the idea, time it was created - do you mean config file? or docker-compose.yaml? but docker-compose can refer to external config file

@ndeloof
Copy link
Contributor

ndeloof commented Jul 8, 2024

time container was created, so we can check it needs to be recreated if current config doesn't match

@@ -37,5 +38,22 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
if err != nil {
return "", err
}

for _, serviceConfig := range o.Configs {
if projectConfig, ok := project.Configs[serviceConfig.Source]; ok {
Copy link
Contributor

@jhrotko jhrotko Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would invert the if statements so that we do not get the code so nested. Other than that looks good to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhrotko done

@ndeloof
Copy link
Contributor

ndeloof commented Jul 10, 2024

Some poins:

  1. At some point we will have to do the same for secrets (same constraints) and networks/volumes (which can get labeled with a has). Solution we adopt here should be extensible to allow this in the future
  2. Configs set by file are implemented by a bind mount but this may change in near future (see Enable configs.file's on remote docker hosts #11871) so we also need to consider those
  3. There's no technical reason docker engine can't offer secrets/configs natively, just this is guarded by Swarm mode, but AFAIK some discussion happened to get them also available in standalone mode. I can't tell if/when this would take place, but preferably the logic here should consider this may happen
  4. Last but not least, I'd prefer we don't mix service config hash with resources it depends on, so my suggestion to introduce an additional label com.docker.compose.config.xx=hash that we can use to track this relation, and need to recreate container, without the need to change the service hash computation (which as impact on existing installations)

@idsulik
Copy link
Contributor Author

idsulik commented Jul 11, 2024

@ndeloof thanks for the details. pushed changes:

  1. reverted old changes
  2. added new func ServiceDependenciesHash and label
// ConfigHashDependenciesLabel stores configuration hash for a compose service dependencies
ConfigHashDependenciesLabel = "com.docker.compose.config-hash-dependencies"

Let me know if you have better idea for the label name, because I'm not fully satisfied with my name)

if projectConfig.Content != "" {
bytes = append(bytes, []byte(projectConfig.Content)...)
} else if projectConfig.File != "" {
content, err := os.ReadFile(projectConfig.File)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File could also be a folder. Maybe an option would be to read content into a tar (code exists to inject secrets) and compute hash from it

While the general approach looks good, I'd prefer we have a more explicit hash and label per resource, and a dedicated hash function for configs (and secrets), so that it make it easier to diagnose use of those labels

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof thank you the comment. Pushed fixes: added secrets hash, added separate hashes for configs and secrets, added support for file.File directory

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
This reverts commit 64c37bf.

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
…older support

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
@idsulik
Copy link
Contributor Author

idsulik commented Sep 16, 2024

Hi @ndeloof , what do you think about thinking about the PR?

return err
}
if !info.IsDir() {
fileContent, err := os.ReadFile(path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not detect file being rename. Maybe better to create a tar stream from file/dir content

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof it creates a tar from the content here pkg/compose/hash.go:122.

b, err := utils.CreateTar(content, serviceConfig, modTime)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants