[compose] Share the compose loading code between swarm and k8s stack deploy#845
Conversation
0bb3d17 to
3169e97
Compare
Codecov Report
@@ Coverage Diff @@
## master #845 +/- ##
==========================================
+ Coverage 53.19% 55.03% +1.84%
==========================================
Files 246 234 -12
Lines 16021 15449 -572
==========================================
- Hits 8522 8502 -20
+ Misses 6929 6382 -547
+ Partials 570 565 -5 |
| if err != nil { | ||
| return details, err | ||
| } | ||
| // TODO: support multiple files |
| return nil, nil, errors.Wrapf(err, "cannot load compose file") | ||
| } | ||
|
|
||
| fmt.Println(string(res)) |
There was a problem hiding this comment.
yeah it was for debugging purpose, I'll remove once not wip anymore
a18de0b to
d0f6ed5
Compare
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPlaceholders(t *testing.T) { |
There was a problem hiding this comment.
Was this test really useless, as it just disappeared?
There was a problem hiding this comment.
Yes 😏 interpolation is already tested in the cli/compose/... packages 😉
| return err | ||
| } | ||
| return kubernetes.RunDeploy(kli, opts) | ||
| return kubernetes.RunDeploy(kli, opts, loadComposefile) |
There was a problem hiding this comment.
I don't like this functor. I think the only reason is to avoid circular dependency between stack package and swarm/kubernetes packages. A more cleaner way could be moving the loader in its own package, like cli/command/stack/loader.
e1dbd5c to
bd61f77
Compare
|
So… yaml marshaling the |
|
Ya, I guess it's not necessarily designed to be round tripped. How is it invalid? |
|
Services are marshalled as a list, but they are unmarshalled as a mapping. This PR is not mergeable until we can fix that 😅 |
1210082 to
6cb919f
Compare
|
Updated: it should now generate a valid yaml composefile — also tried it out on docker-for-mac kubernetes and it works ! Next step should be to upgrade the current test with everything, probably reuse |
| } | ||
| m["services"] = services | ||
| } | ||
| if c.Networks != nil { |
There was a problem hiding this comment.
Are these nil checks necessary (below)? If it is nil, it can assign nil without any problems, right? I think they can be removed.
|
With full test looks good! |
eda38cc to
ec92b3a
Compare
ec92b3a to
98e5148
Compare
| ports: | ||
| - 3000 | ||
| - "3000-3005" | ||
| - "3001-3005" |
There was a problem hiding this comment.
Changed that to not have duplicate in the marshalled struct later.
|
@dnephin Updated with the full-example 🎉 |
| return nil, err | ||
| } | ||
|
|
||
| yaml := "version: \"" + version + "\"\n" + string(res) |
There was a problem hiding this comment.
Could we do this by marshaling a local struct:
type versionedConfig struct {
composetypes.Config
Version string
}98e5148 to
1322abe
Compare
| Args: cli.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| opts.Namespace = args[0] | ||
|
|
| fmt.Fprintf(dockerCli.Err(), "Ignoring deprecated options:\n\n%s\n\n", | ||
| propertyWarnings(deprecatedProperties)) | ||
| } | ||
| return config, configDetails.Version, nil |
There was a problem hiding this comment.
Not for this PR, but getConfigDetails() actually loads all the config files, and takes the "WorkDir" and "Version" of the first file that's loaded. If we added a Version to composetypes.ConfigFile, that would probably be a bit cleaner, because in loader.Load() we check the version again (we both set the version if it's empty, and check if versions are mixed);
cli/cli/compose/loader/loader.go
Lines 52 to 59 in 1872bd8
Basically, these types;
cli/cli/compose/types/types.go
Lines 50 to 62 in 1872bd8
Would then look something like;
// ConfigFile is a filename and the contents of the file as a Dict
type ConfigFile struct {
Version string
Filename string
WorkingDir string
Config map[string]interface{}
}
// ConfigDetails are the details about a group of ConfigFiles
type ConfigDetails struct {
ConfigFiles []ConfigFile
Environment map[string]string
}(Perhaps there should be an option to overwrite the WorkingDir)
Which leads to the question if ConfigDetails on its own is very useful (Environment is currently just the result of os.Environ(), and could be either passed as separate argument to loader.Load(), or fetched there?)
| return details, errors.New("no composefile(s)") | ||
| } | ||
|
|
||
| if composefiles[0] == "-" && len(composefiles) == 1 { |
There was a problem hiding this comment.
We need to check if this behaviour is documented;
- working-directory of the first compose-file is used
- if the first file is from
stdin, then the current working-directory is used
/cc @gbarr01
thaJeztah
left a comment
There was a problem hiding this comment.
one nit (unrelated whitespace change), and a suggestion for a follow up, but LGTM
To ensure we are loading the composefile the same wether we are pointing to swarm or kubernetes, we need to share the loading code between both. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1322abe to
570ee9c
Compare
|
ping @dnephin still LGTY? |
| func TestMarshallConfig(t *testing.T) { | ||
| cfg := fullExampleConfig("/foo", "/bar") | ||
| expected := `configs: {} | ||
| networks: |
There was a problem hiding this comment.
Hopefully MarshalYAML is stable and doesn't change order randomly when we change fields.
There was a problem hiding this comment.
so far it did not 😇
[compose] Share the compose loading code between swarm and k8s stack deploy Upstream-commit: f56265a Component: cli
To ensure we are loading the composefile the same wether we are pointing
to swarm or kubernetes, we need to share the loading code between both.
This means though that
docker stack deploypointing to k8s doesn't handle.envanymore (same beahviour asdocker stack deployon swarm).Signed-off-by: Vincent Demeester vincent@sbr.pm