Make sure every composefiles sent to a v1beta1 endpoint are parsable

Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
This commit is contained in:
Simon Ferquel 2018-04-25 15:43:18 +02:00
parent 552ee502f7
commit f766aff716
3 changed files with 43 additions and 1 deletions

View File

@ -4,10 +4,14 @@ import (
"fmt" "fmt"
"io" "io"
"github.com/docker/cli/cli/compose/loader"
"github.com/docker/cli/cli/compose/schema"
composetypes "github.com/docker/cli/cli/compose/types" composetypes "github.com/docker/cli/cli/compose/types"
composev1beta1 "github.com/docker/cli/kubernetes/client/clientset/typed/compose/v1beta1" composev1beta1 "github.com/docker/cli/kubernetes/client/clientset/typed/compose/v1beta1"
composev1beta2 "github.com/docker/cli/kubernetes/client/clientset/typed/compose/v1beta2" composev1beta2 "github.com/docker/cli/kubernetes/client/clientset/typed/compose/v1beta2"
v1beta1types "github.com/docker/cli/kubernetes/compose/v1beta1"
"github.com/docker/cli/kubernetes/labels" "github.com/docker/cli/kubernetes/labels"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1" corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
@ -105,6 +109,7 @@ func verify(services corev1.ServiceInterface, stackName string, service string)
} }
func (s *stackV1Beta1) FromCompose(stderr io.Writer, name string, cfg *composetypes.Config) (stack, error) { func (s *stackV1Beta1) FromCompose(stderr io.Writer, name string, cfg *composetypes.Config) (stack, error) {
cfg.Version = v1beta1types.MaxComposeVersion
st, err := fromCompose(stderr, name, cfg) st, err := fromCompose(stderr, name, cfg)
if err != nil { if err != nil {
return stack{}, err return stack{}, err
@ -113,6 +118,15 @@ func (s *stackV1Beta1) FromCompose(stderr io.Writer, name string, cfg *composety
if err != nil { if err != nil {
return stack{}, err return stack{}, err
} }
// reload the result to check that it produced a valid 3.5 compose file
resparsedConfig, err := loader.ParseYAML(res)
if err != nil {
return stack{}, err
}
if err = schema.Validate(resparsedConfig, v1beta1types.MaxComposeVersion); err != nil {
return stack{}, errors.Wrapf(err, "the compose yaml file is invalid with v%s", v1beta1types.MaxComposeVersion)
}
st.composeFile = string(res) st.composeFile = string(res)
return st, nil return st, nil
} }

View File

@ -26,7 +26,7 @@ func TestFromCompose(t *testing.T) {
}) })
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, "foo", s.name) assert.Equal(t, "foo", s.name)
assert.Equal(t, string(`version: "3.1" assert.Equal(t, string(`version: "3.5"
services: services:
bar: bar:
image: bar image: bar
@ -38,3 +38,27 @@ secrets: {}
configs: {} configs: {}
`), s.composeFile) `), s.composeFile)
} }
func TestFromComposeUnsupportedVersion(t *testing.T) {
stackClient := &stackV1Beta1{}
_, err := stackClient.FromCompose(ioutil.Discard, "foo", &composetypes.Config{
Version: "3.6",
Filename: "banana",
Services: []composetypes.ServiceConfig{
{
Name: "foo",
Image: "foo",
Volumes: []composetypes.ServiceVolumeConfig{
{
Type: "tmpfs",
Target: "/app",
Tmpfs: &composetypes.ServiceVolumeTmpfs{
Size: 10000,
},
},
},
},
},
})
assert.ErrorContains(t, err, "the compose yaml file is invalid with v3.5: services.foo.volumes.0 Additional property tmpfs is not allowed")
}

View File

@ -0,0 +1,4 @@
package v1beta1
// MaxComposeVersion is the most recent version of compose file Schema supported in v1beta1
const MaxComposeVersion = "3.5"