-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add stack config command #2740
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
Closed
Closed
Add stack config command #2740
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7bafb8
Add stack config command
SMFloris ed780e1
Change merge strategy for service volumes
SMFloris e74f0c5
Add merge to ShellCommand properties in config
SMFloris 53247fe
Add documentation for stack config command
SMFloris a72ec7b
Remove alias
SMFloris 5c36abb
Use the configured cli output
SMFloris ed5d641
Don't export outputConfig
SMFloris ab7e34b
Merged the two tests by using table driven test pattern
SMFloris 411f26c
Return err directly when outputting to the configured cli output
SMFloris 8674459
remove composer version check since we are not calling the api
SMFloris 98d5109
add experimental flag to config command
SMFloris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package stack | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/docker/cli/cli" | ||
| "github.com/docker/cli/cli/command" | ||
| "github.com/docker/cli/cli/command/stack/loader" | ||
| "github.com/docker/cli/cli/command/stack/options" | ||
| composeLoader "github.com/docker/cli/cli/compose/loader" | ||
| composetypes "github.com/docker/cli/cli/compose/types" | ||
| "github.com/spf13/cobra" | ||
| yaml "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| func newConfigCommand(dockerCli command.Cli) *cobra.Command { | ||
| var opts options.Config | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "config [OPTIONS]", | ||
| Short: "Outputs the final config file, after doing merges and interpolations", | ||
| Args: cli.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| configDetails, err := loader.GetConfigDetails(opts.Composefiles, dockerCli.In()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cfg, err := outputConfig(configDetails, opts.SkipInterpolation) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = fmt.Fprintf(dockerCli.Out(), "%s", cfg) | ||
SMFloris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return err | ||
| }, | ||
SMFloris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Annotations: map[string]string{"experimental": ""}, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringSliceVarP(&opts.Composefiles, "compose-file", "c", []string{}, `Path to a Compose file, or "-" to read from stdin`) | ||
| flags.BoolVar(&opts.SkipInterpolation, "skip-interpolation", false, "Skip interpolation and output only merged config") | ||
| return cmd | ||
| } | ||
|
|
||
| // outputConfig returns the merged and interpolated config file | ||
| func outputConfig(configFiles composetypes.ConfigDetails, skipInterpolation bool) (string, error) { | ||
| optsFunc := func(options *composeLoader.Options) { | ||
| options.SkipInterpolation = skipInterpolation | ||
silvin-lubecki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| config, err := composeLoader.Load(configFiles, optsFunc) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| d, err := yaml.Marshal(&config) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return string(d), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package stack | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "testing" | ||
|
|
||
| "github.com/docker/cli/cli/compose/loader" | ||
| composetypes "github.com/docker/cli/cli/compose/types" | ||
| "github.com/docker/cli/internal/test" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestConfigWithEmptyComposeFile(t *testing.T) { | ||
| cmd := newConfigCommand(test.NewFakeCli(&fakeClient{})) | ||
| cmd.SetOut(ioutil.Discard) | ||
|
|
||
| assert.ErrorContains(t, cmd.Execute(), `Please specify a Compose file`) | ||
| } | ||
|
|
||
| var configMergeTests = []struct { | ||
| name string | ||
| skipInterpolation bool | ||
| first string | ||
| second string | ||
| merged string | ||
| }{ | ||
| { | ||
| name: "With Interpolation", | ||
| skipInterpolation: false, | ||
| first: `version: "3.7" | ||
| services: | ||
| foo: | ||
| image: busybox:latest | ||
| command: cat file1.txt | ||
| `, | ||
| second: `version: "3.7" | ||
| services: | ||
| foo: | ||
| image: busybox:${VERSION} | ||
| command: cat file2.txt | ||
| `, | ||
| merged: `version: "3.7" | ||
| services: | ||
| foo: | ||
| command: | ||
| - cat | ||
| - file2.txt | ||
| image: busybox:1.0 | ||
| `, | ||
| }, | ||
| { | ||
| name: "Without Interpolation", | ||
| skipInterpolation: true, | ||
| first: `version: "3.7" | ||
| services: | ||
| foo: | ||
| image: busybox:latest | ||
| command: cat file1.txt | ||
| `, | ||
| second: `version: "3.7" | ||
| services: | ||
| foo: | ||
| image: busybox:${VERSION} | ||
| command: cat file2.txt | ||
| `, | ||
| merged: `version: "3.7" | ||
| services: | ||
| foo: | ||
| command: | ||
| - cat | ||
| - file2.txt | ||
| image: busybox:${VERSION} | ||
| `, | ||
| }, | ||
| } | ||
|
|
||
| func TestConfigMergeInterpolation(t *testing.T) { | ||
|
|
||
| for _, tt := range configMergeTests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| firstConfig := []byte(tt.first) | ||
| secondConfig := []byte(tt.second) | ||
|
|
||
| firstConfigData, err := loader.ParseYAML(firstConfig) | ||
| assert.NilError(t, err) | ||
| secondConfigData, err := loader.ParseYAML(secondConfig) | ||
| assert.NilError(t, err) | ||
|
|
||
| env := map[string]string{ | ||
| "VERSION": "1.0", | ||
| } | ||
|
|
||
| cfg, err := outputConfig(composetypes.ConfigDetails{ | ||
| ConfigFiles: []composetypes.ConfigFile{ | ||
| {Config: firstConfigData, Filename: "firstConfig"}, | ||
| {Config: secondConfigData, Filename: "secondConfig"}, | ||
| }, | ||
| Environment: env, | ||
| }, tt.skipInterpolation) | ||
| assert.NilError(t, err) | ||
|
|
||
| assert.Equal(t, cfg, tt.merged) | ||
| }) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
question: @SMFloris why not using
LoadComposefilefunction instead, which actually callsgetConfigDetails?Uh oh!
There was an error while loading. Please reload this page.
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.
Great question!
If I recall correctly, I didn't use it because I wanted to have control on how the compose files are actually getting loaded.
The
--skip-interpolationflag is being sent down to thecomposeLoader.load()call, like so:This is kinda important to the use-case of this new command, seeing the output with and without interpolation to figure out where things went wrong and if you're deploying the right thing.
Another reason is that
LoadComposefilealso does some other things besides loading compose files, it also does some validation (forbidden, deprecated and unsupported properties). I thought those validations were just for deploy since the validation of thecomposeLoader.loadis also used. 🤔 Also, since in my mind I'm piping the output back todocker stack deploy -c -I saw no reason in doing deploy validations when not doing a deploy.The final reason, since I wasn't that familiar with the code-base and because I added a new command I didn't want to mess too much with the existing signatures of the methods 😅
Now that I'm more familiar with the codebase. I can change the
LoadComposefilefunction to pass down some options tocomposeLoader.load()call if you want me too (perhaps even adding an option to skip those other validations).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.
@thaJeztah any thoughts on that? I'm a bit on the fence, as if the output is pipped to the docker stack deploy command, then the config should print the warnings I guess... but it can also be pipped to the docker-compose command...
@SMFloris propose to call
LoadComposeFileand add the--skip-validationoption so one can disable the validation... WDYT?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.
@thaJeztah , could we get your input on the above, please? 😄 We are doing some ugly hacks with downgrading docker-compose in order to get valid docker stack deploy files
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.
So if we're trying to replicate
docker-compose config, that command is described as "Validate and view the Compose file"; from that perspective, I think it would make sense to have it validate the compose-file, so that only a valid file would be generated.Not sure if we should consider if for that (as
docker-composehas aconfigcommand itself 🤔Do we know why the
--skip-validationoption was added in compose? Wondering what the exact use-case is if that would allow it to produce invalid output (or is it skipping specific validation steps?)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.
@thaJeztah , I managed to track the inclusion of these options down to #1128
Maybe reading through that a bit can help jog some memories 😄