From 9f9f1c8515fe76acaf6262dee7a879a4510a984a Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 20 Feb 2018 17:50:49 +0100 Subject: [PATCH] =?UTF-8?q?Make=20sure=20we=20marshall=20version=20too?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … otherwise the k8s controller might fail to parse the file as it will think it's version 1. Signed-off-by: Vincent Demeester --- cli/command/stack/kubernetes/loader.go | 24 ++++++++++++++++----- cli/command/stack/kubernetes/loader_test.go | 1 + cli/compose/types/types.go | 22 +++++++++---------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cli/command/stack/kubernetes/loader.go b/cli/command/stack/kubernetes/loader.go index f39d31529c..0eca43b283 100644 --- a/cli/command/stack/kubernetes/loader.go +++ b/cli/command/stack/kubernetes/loader.go @@ -8,16 +8,30 @@ import ( ) type versionedConfig struct { - *composetypes.Config `yaml:",inline"` - Version string + composetypes.Config + version string +} + +func (c versionedConfig) MarshalYAML() (interface{}, error) { + services := map[string]composetypes.ServiceConfig{} + for _, service := range c.Services { + services[service.Name] = service + } + return map[string]interface{}{ + "services": services, + "networks": c.Networks, + "volumes": c.Volumes, + "secrets": c.Secrets, + "configs": c.Configs, + "version": c.version, + }, nil } // LoadStack loads a stack from a Compose config, with a given name. func LoadStack(name, version string, cfg composetypes.Config) (*apiv1beta1.Stack, error) { - cfg.Filename = "" res, err := yaml.Marshal(versionedConfig{ - Version: version, - Config: &cfg, + version: version, + Config: cfg, }) if err != nil { return nil, err diff --git a/cli/command/stack/kubernetes/loader_test.go b/cli/command/stack/kubernetes/loader_test.go index 7e2ff8d810..e79202cf2f 100644 --- a/cli/command/stack/kubernetes/loader_test.go +++ b/cli/command/stack/kubernetes/loader_test.go @@ -37,6 +37,7 @@ services: image: bar foo: image: foo +version: "3.1" volumes: {} `), }, diff --git a/cli/compose/types/types.go b/cli/compose/types/types.go index 2299871cfb..26f7139a5a 100644 --- a/cli/compose/types/types.go +++ b/cli/compose/types/types.go @@ -70,7 +70,7 @@ func (cd ConfigDetails) LookupEnv(key string) (string, bool) { // Config is a full compose file configuration type Config struct { - Filename string + Filename string `yaml:"-"` Services []ServiceConfig Networks map[string]NetworkConfig Volumes map[string]VolumeConfig @@ -80,24 +80,22 @@ type Config struct { // MarshalYAML makes Config implement yaml.Marshaller func (c *Config) MarshalYAML() (interface{}, error) { - m := map[string]interface{}{} services := map[string]ServiceConfig{} for _, service := range c.Services { - s := service - s.Name = "" - services[service.Name] = s + services[service.Name] = service } - m["services"] = services - m["networks"] = c.Networks - m["volumes"] = c.Volumes - m["secrets"] = c.Secrets - m["configs"] = c.Configs - return m, nil + return map[string]interface{}{ + "services": services, + "networks": c.Networks, + "volumes": c.Volumes, + "secrets": c.Secrets, + "configs": c.Configs, + }, nil } // ServiceConfig is the configuration of one service type ServiceConfig struct { - Name string `yaml:",omitempty"` + Name string `yaml:"-"` Build BuildConfig `yaml:",omitempty"` CapAdd []string `mapstructure:"cap_add" yaml:"cap_add,omitempty"`