Skip to content
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

Make compose file allow to specify names for non-external volume #306

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/compose/convert/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func convertVolumeToMount(
return result, nil
}

if stackVolume.Name != "" {
result.Source = stackVolume.Name
}

result.VolumeOptions.Labels = AddStackLabel(namespace, stackVolume.Labels)
if stackVolume.Driver != "" || stackVolume.DriverOpts != nil {
result.VolumeOptions.DriverConfig = &mount.Driver{
Expand Down
47 changes: 47 additions & 0 deletions cli/compose/convert/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,53 @@ func TestConvertVolumeToMountNamedVolume(t *testing.T) {
assert.Equal(t, expected, mount)
}

func TestConvertVolumeToMountNamedVolumeWithNameCustomizd(t *testing.T) {
stackVolumes := volumes{
"normal": composetypes.VolumeConfig{
Name: "user_specified_name",
Driver: "vsphere",
DriverOpts: map[string]string{
"opt": "value",
},
Labels: map[string]string{
"something": "labeled",
},
},
}
namespace := NewNamespace("foo")
expected := mount.Mount{
Type: mount.TypeVolume,
Source: "user_specified_name",
Target: "/foo",
ReadOnly: true,
VolumeOptions: &mount.VolumeOptions{
Labels: map[string]string{
LabelNamespace: "foo",
"something": "labeled",
},
DriverConfig: &mount.Driver{
Name: "vsphere",
Options: map[string]string{
"opt": "value",
},
},
NoCopy: true,
},
}
config := composetypes.ServiceVolumeConfig{
Type: "volume",
Source: "normal",
Target: "/foo",
ReadOnly: true,
Volume: &composetypes.ServiceVolumeVolume{
NoCopy: true,
},
}
mount, err := convertVolumeToMount(config, stackVolumes, namespace)
assert.NoError(t, err)
assert.Equal(t, expected, mount)
}

func TestConvertVolumeToMountNamedVolumeExternal(t *testing.T) {
stackVolumes := volumes{
"outside": composetypes.VolumeConfig{
Expand Down
18 changes: 17 additions & 1 deletion cli/compose/loader/full-example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.3"
version: "3.4"

services:
foo:
Expand Down Expand Up @@ -280,6 +280,15 @@ volumes:
foo: "bar"
baz: 1

another-volume:
name: "user_specified_name"
driver: vsphere

driver_opts:
# Values can be strings or numbers
foo: "bar"
baz: 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see an example using a name and external, for example;

  external-volume3:
    name: this-is-volume3
    external: true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also (not sure if that needs to be done in this file, or another one); can we have a test that tests if setting both volume.name and volume.external.name produces the "conflicting options" error? i.e.;

  external-volume4-fail:
    # name cannot be provided both as volume.name and volume.external.name
    name: custom-name
    external:
      name: conflicting-name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate test case for testing the conflict would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Add a test case in for that.

external-volume:
# Specifies that a pre-existing volume called "external-volume"
# can be referred to within this file as "external-volume"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for other-external-volume below, should we update the comment to mention volume.external.name is deprecated? (can't comment on that line, so leaving the comment here 😄)

# This example uses the deprecated "volume.external.name" (replaced by "volume.name")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Add the comment.

Expand All @@ -288,5 +297,12 @@ volumes:
other-external-volume:
# Specifies that a pre-existing volume called "my-cool-volume"
# can be referred to within this file as "other-external-volume"
# This example uses the deprecated "volume.external.name" (replaced by "volume.name")
external:
name: my-cool-volume

external-volume3:
# Specifies that a pre-existing volume called "this-is-volume3"
# can be referred to within this file as "external-volume3"
name: this-is-volume3
external: true
6 changes: 6 additions & 0 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ func LoadVolumes(source map[string]interface{}) (map[string]types.VolumeConfig,
if volume.External.Name == "" {
volume.External.Name = name
volumes[name] = volume
} else {
logrus.Warnf("volume %s: volume.external.name is deprecated in favor of volume.name", name)

if volume.Name != "" {
return nil, errors.Errorf("volume %s: volume.external.name and volume.name conflict; only use volume.name", name)
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,22 @@ volumes:
assert.Contains(t, err.Error(), "external_volume")
}

func TestInvalidExternalNameAndNameCombination(t *testing.T) {
_, err := loadYAML(`
version: "3.4"
volumes:
external_volume:
name: user_specified_name
external:
name: external_name
`)

assert.Error(t, err)
fmt.Println(err)
assert.Contains(t, err.Error(), "volume.external.name and volume.name conflict; only use volume.name")
assert.Contains(t, err.Error(), "external_volume")
}

func durationPtr(value time.Duration) *time.Duration {
return &value
}
Expand Down Expand Up @@ -983,6 +999,14 @@ func TestFullExample(t *testing.T) {
"baz": "1",
},
},
"another-volume": {
Name: "user_specified_name",
Driver: "vsphere",
DriverOpts: map[string]string{
"foo": "bar",
"baz": "1",
},
},
"external-volume": {
External: types.External{
Name: "external-volume",
Expand All @@ -995,6 +1019,13 @@ func TestFullExample(t *testing.T) {
External: true,
},
},
"external-volume3": {
Name: "this-is-volume3",
External: types.External{
Name: "external-volume3",
External: true,
},
},
}

assert.Equal(t, expectedVolumeConfig, config.Volumes)
Expand Down
23 changes: 23 additions & 0 deletions cli/compose/schema/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading