Skip to content

bake: recursively resolve groups #1313

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

Merged
merged 1 commit into from
Nov 25, 2022
Merged

Conversation

jedevc
Copy link
Collaborator

@jedevc jedevc commented Sep 12, 2022

Fixes #1058.

Groups that contained other groups were not recursively resolved by ReadTargets, which prevented output from --print from being useable as a self-contained bake file. e.g.

group "default" {
    targets = ["other-group"]
}

group "other-group" {
    targets = ["app"]
}

target "app" {
    dockerfile = "Dockerfile"
    tags = ["docker.io/username/app"]
}

is used as

$ buildx bake --print -f docker-bake.hcl
[+] Building 0.0s (0/0)                                                                                                    
{
  "group": {
    "default": {
      "targets": [
        "other-group"
      ]
    },
    "other-group": {
      "targets": [
        "app"
      ]
    }
  },
  "target": {
    "app": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": [
        "docker.io/username/app"
      ]
    }
  }
}

This patch ensures that all groups that are referenced inside the bake file are actually defined under the groups field. This has required a substantial refactor, as previously only a single group was returned from ReadTargets, notably, returning a map of groups, instead of a slice.

This does introduce a small behavior change to the behavior of --print - while previously, passing a group name to bake would return all the targets of that group back as the default group, now only the name of that group will be inserted into the default group, keeping the original group intact. The impact of this can be observed in some of the changes to the bake_test.go file.

e.g. previously with buildx's docker-bake.hcl:

$ docker buildx bake --print validate
[+] Building 0.0s (0/0)                                                                                                    
{
  "group": {
    "default": {
      "targets": [
        "lint",
        "validate-vendor",
        "validate-docs"
      ]
    }
  },

vs now:

$ buildx bake --print validate
[+] Building 0.0s (0/0)                                                                                                    
{
  "group": {
    "default": {
      "targets": [
        "validate"
      ]
    },
    "validate": {
      "targets": [
        "lint",
        "validate-vendor",
        "validate-docs"
      ]
    }
  },

Groups that contained other groups were not recursively resolved by
ReadTargets, which prevented output from --print from being useable as a
self-contained bake file.

This patch ensures that all groups that are referenced inside the bake
file are actually defined under the groups field. This has required a
substantial refactor, as previously only a single group was returned
from ReadTargets, notably, returning a map of groups, instead of a
slice.

This does introduce a small behavior change to the behavior of --print -
while previously, passing a group name to bake would return all the
targets of that group back as the default group, now only the name of
that group will be inserted into the default group, keeping the original
group intact. The impact of this can be observed in some of the changes
to the bake_test.go file.

Signed-off-by: Justin Chadwell <me@jedevc.com>
Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

LGTM

This does introduce a small behavior change to the behavior of --print - while previously, passing a group name to bake would return all the targets of that group back as the default group, now only the name of that group will be inserted into the default group, keeping the original group intact. The impact of this can be observed in some of the changes to the bake_test.go file.

I think the new behavior is fine and does not break the expected outcome.

@crazy-max
Copy link
Member

PTAL @tonistiigi

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

Successfully merging this pull request may close these issues.

bake --print show wrong docker-bake.json when we target a group which targets groups
3 participants