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

Migrate the Docker context tags version from <stage>.tag to tags.<stage>. #14376

Merged

Conversation

kaos
Copy link
Member

@kaos kaos commented Feb 5, 2022

Fixes #14023

Currently, the context gets values on the form <stage>.tag = <tag>, where stage is either baseimage, stageN or <stage name>.

Given the dynamic nature of this key, it is not well suited to use as a root key for the context, as it may conflict with other values. e.g. if you have a stage named "build_args", it would collide with the context value for build args.

This PR deprecates the current layout in 2.10, in favour of putting them under a tags key instead tags.<stage> = <tag>:

{
  "tags": {
    "baseimage": "latest",
    "stage0": "latest",
    "custom-stage-name": "1.2",
    ...
  },
  "build_args": {...},
  ...
}

…stage>`.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]
@kaos
Copy link
Member Author

kaos commented Feb 5, 2022

Tried the deprecation message on the Docker testproject, and it looks like:

diff --git a/testprojects/src/python/docker/BUILD b/testprojects/src/python/docker/BUILD
index a6e4ffef6..58c01faf4 100644
--- a/testprojects/src/python/docker/BUILD
+++ b/testprojects/src/python/docker/BUILD
@@ -9,7 +9,7 @@ docker_image(
 
 docker_image(
     name="test-example-synth",
-    image_tags=["1.2.5"],
+    image_tags=["1.2.5-{baseimage.tag}", "{tags.baseimage}"],
     instructions=[
         "FROM python:3.7",
         'ENTRYPOINT ["/hello"]',
$ ./pants package testprojects/src/python/docker:test-example-synth
23:19:54.91 [INFO] Completed: Building docker image test-example-synth:1.2.5-3.7 +1 additional tag.
23:19:54.91 [WARN] DEPRECATED: Docker interpolation context type 'DockerfileImageTagsDeprecation' will be removed in version 2.11.0.dev0.

The `<stage>.tag` version values are deprecated in favour of `tags.<stage>`.

See https://github.com/pantsbuild/pants/issues/14023 for more details.
23:19:54.92 [INFO] Built docker images: 
  * test-example-synth:1.2.5-3.7
  * test-example-synth:3.7

Not sure really what to put in as entity on the warn_or_error call there that would make sense here...

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Looks reasonable

class DockerfileImageTagsDeprecation(DeprecatedDockerInterpolationValue):
_removal_version = "2.11.0.dev0"
_hint = (
"The `<stage>.tag` version values are deprecated in favour of `tags.<stage>`.\n\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to Save A Click and give an example:

For example, rather than using {baseimage.tag} in your Docker template, use {tags.baseimage}.

But then still link to this PR like you do if people still aren't understanding.

@Eric-Arellano
Copy link
Contributor

Not sure really what to put in as entity on the warn_or_error call there that would make sense here...

The PR title: "using <stage>.tag rather than tags.<stage> for Docker interpolation

@Eric-Arellano
Copy link
Contributor

Rather than using the Class and getattr to deprecate this, I wonder if we can inline using warn_or_error() into docker_build_context.py line 143? I think that could allow us to have a better warning that points to the docker_image target causing issues; otherwise, you'll have to track that down manually.

Also, I'm not sure if this deprecation is always executed? It looks like it would be. We need to make sure the deprecation is silent if you did nothing wrong.

@kaos
Copy link
Member Author

kaos commented Feb 8, 2022

Not sure really what to put in as entity on the warn_or_error call there that would make sense here...

The PR title: "using .tag rather than tags. for Docker interpolation

That is the hint, so is in there on the second line of the deprecation message.

Rather than using the Class and getattr to deprecate this, I wonder if we can inline using warn_or_error() into docker_build_context.py line 143?

At that point, we don't know if the deprecated name will be used or not, hence why I placed it in getattr, so it will only trigger the deprecation message when actually referenced.

Also, I'm not sure if this deprecation is always executed? It looks like it would be. We need to make sure the deprecation is silent if you did nothing wrong.

See above.

[...] a better warning that points to the docker_image target causing issues

That's a good idea, I'll take a stab at that.

@kaos
Copy link
Member Author

kaos commented Feb 8, 2022

$ ./pants package testprojects/src/python/docker:test-example-synth
10:21:19.84 [WARN] DEPRECATED: Docker interpolation context type 'DockerfileImageTagsDeprecation_baseimage' will be removed in version 2.11.0.dev0.

Interpolation in testprojects/src/python/docker:test-example-synth uses the deprecated placeholder 'baseimage.tag', instead use 'tags.baseimage'.

The `<stage>.tag` version values are deprecated in favour of `tags.<stage>`.
See https://github.com/pantsbuild/pants/issues/14023 for more details.
10:21:21.16 [INFO] Completed: Building docker image test-example-synth:1.2.5 +1 additional tag.

for this build target:

docker_image(
    name="test-example-synth",
    image_tags=["1.2.5", "1.{baseimage.tag}"],
    instructions=[
        "FROM python:3.7",
        'ENTRYPOINT ["/hello"]',
        'CMD ["synthetic!"]',
        "COPY testprojects.src.python.hello.main/main.pex /hello",
    ],
)

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@Eric-Arellano
Copy link
Contributor

Awesome! To be crystal clear, if you remove the bad reference and rerun with --no-pantsd, no deprecation message? If so, lgtm. Thanks for this change :)

@kaos
Copy link
Member Author

kaos commented Feb 8, 2022

if you remove the bad reference and rerun with --no-pantsd, no deprecation message?

Check. Works as expected both with and without the --no-pantsd flag.

$ ./pants package testprojects/src/python/docker:test-example-synth
17:58:32.04 [WARN] DEPRECATED: Docker interpolation context type 'DockerfileImageTagsDeprecation_baseimage' will be removed in version 2.11.0.dev0.

Interpolation in testprojects/src/python/docker:test-example-synth uses the deprecated placeholder 'baseimage.tag', instead use 'tags.baseimage'.

The `<stage>.tag` version values are deprecated in favour of `tags.<stage>`.
See https://github.com/pantsbuild/pants/issues/14023 for more details.
17:58:33.27 [INFO] Completed: Building docker image test-example-synth:1.2.5 +1 additional tag.
17:58:33.27 [INFO] Built docker images: 
  * test-example-synth:1.2.5
  * test-example-synth:1.3.7

# Edited to the `image_tag` to new syntax.

$ ./pants --no-pantsd package testprojects/src/python/docker:test-example-synth
17:59:05.99 [INFO] Completed: Building docker image test-example-synth:1.2.5 +1 additional tag.
17:59:06.00 [INFO] Built docker images: 
  * test-example-synth:1.2.5
  * test-example-synth:1.3.7

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thank you!

@kaos kaos merged commit bf63c74 into pantsbuild:main Feb 8, 2022
@kaos kaos deleted the docker_version_tags_interpolation_context branch February 8, 2022 19:31
kaos added a commit to kaos/pants that referenced this pull request Feb 8, 2022
…stage>`. (pantsbuild#14376)

Fixes pantsbuild#14023 

Currently, the context gets values on the form `<stage>.tag = <tag>`, where stage is either `baseimage`, `stageN` or `<stage name>`. 

Given the dynamic nature of this key, it is not well suited to use as a root key for the context, as it may conflict with other values. e.g. if you have a stage named "build_args", it would collide with the context value for build args.

This PR deprecates the current layout in 2.10, in favour of putting them under a `tags` key instead `tags.<stage> = <tag>`:

```python
{
  "tags": {
    "baseimage": "latest",
    "stage0": "latest",
    "custom-stage-name": "1.2",
    ...
  },
  "build_args": {...},
  ...
}
```

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
kaos added a commit that referenced this pull request Feb 8, 2022
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.

Cleanup Docker interpolation context for version tags
2 participants