Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Uh oh!
There was an error while loading. Please reload this page.
Add stack config command #2740
Changes from all commits
c7bafb8ed780e1e74f0c553247fea72ec7b5c36abbed5d641ab7e34b411f26c867445998d5109File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| return err | ||
| }, | ||
SMFloris marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| 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 | ||
| } | ||
| 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) | ||
| }) | ||
| } | ||
| } |
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.
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 😄