-
Notifications
You must be signed in to change notification settings - Fork 617
bake: ignore unrelated fields when parsing compose files #3292
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/compose-spec/compose-go/v2/consts" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/compose-spec/compose-go/v2/dotenv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/compose-spec/compose-go/v2/loader" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| composeschema "github.com/compose-spec/compose-go/v2/schema" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| composetypes "github.com/compose-spec/compose-go/v2/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/buildx/util/buildflags" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dockeropts "github.com/docker/cli/opts" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -35,21 +36,7 @@ func ParseComposeFiles(fs []File) (*Config, error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Config, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if envs == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envs = make(map[string]string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cfg, err := loader.LoadWithContext(context.Background(), composetypes.ConfigDetails{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigFiles: cfgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment: envs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, func(options *loader.Options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectName := "bake" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if v, ok := envs[consts.ComposeProjectName]; ok && v != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectName = v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SetProjectName(projectName, false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SkipNormalization = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.Profiles = []string{"*"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cfg, err := loadComposeFiles(cfgs, envs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -208,6 +195,67 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &c, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func loadComposeFiles(cfgs []composetypes.ConfigFile, envs map[string]string, options ...func(*loader.Options)) (*composetypes.Project, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if envs == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envs = make(map[string]string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cfgDetails := composetypes.ConfigDetails{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigFiles: cfgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment: envs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raw, err := loader.LoadModelWithContext(context.Background(), cfgDetails, append([]func(*loader.Options){func(opts *loader.Options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectName := "bake" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if v, ok := envs[consts.ComposeProjectName]; ok && v != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectName = v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opts.SetProjectName(projectName, false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opts.SkipNormalization = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opts.SkipValidation = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }}, options...)...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filtered := make(map[string]any) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, key := range []string{"services", "secrets"} { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if key == "services" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if services, ok := raw["services"].(map[string]any); ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredServices := make(map[string]any) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for svcName, svc := range services { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if svc == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredServices[svcName] = map[string]any{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if svcMap, ok := svc.(map[string]any); ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredService := make(map[string]any) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, svcField := range []string{"image", "build", "environment", "env_file"} { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT you don't need
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need them to evaluate build arguments: Lines 396 to 424 in a007368
If not set then this test fails:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a bug then. Runtime environment MUST not impact build environment; Sounds there's a confusion here with client environment (which is defined by
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh yes seems it was a mistake, this was introduced in #905. What about
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For parity with Compose, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if val, ok := svcMap[svcField]; ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredService[svcField] = val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filteredServices[svcName] = filteredService | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filtered["services"] = filteredServices | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if v, ok := raw[key]; ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filtered[key] = v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := composeschema.Validate(filtered); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return loader.ModelToProject(filtered, loader.ToOptions(&cfgDetails, append([]func(*loader.Options){func(options *loader.Options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SkipNormalization = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.Profiles = []string{"*"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. building with all profiles enabled is not consistent with Compose. Is this expected ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We allow all profiles because Bake doesn't support them: #1903. If we add something similar in our spec I think we could reconsider that. cc @colinhemmings
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In follow-up we could consider mapping compose profiles to bake groups and maybe defining a default profile that would be used if one is defined. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }}, options...)), composetypes.ConfigDetails{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigFiles: cfgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment: envs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func validateComposeFile(dt []byte, fn string) (bool, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envs, err := composeEnv() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -225,16 +273,7 @@ func validateComposeFile(dt []byte, fn string) (bool, error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func validateCompose(dt []byte, envs map[string]string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _, err := loader.LoadWithContext(context.Background(), composetypes.ConfigDetails{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigFiles: []composetypes.ConfigFile{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content: dt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment: envs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, func(options *loader.Options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SetProjectName("bake", false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SkipNormalization = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _, err := loadComposeFiles([]composetypes.ConfigFile{{Content: dt}}, envs, func(options *loader.Options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // consistency is checked later in ParseCompose to ensure multiple | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // compose files can be merged together | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.SkipConsistencyCheck = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do this as follow-up as not directly related but seems fine to pass in the right context from the command.