Merge pull request #34896 from cpuguy83/store_labels_when_exists

Create labels when volume exists only remotely
This commit is contained in:
Victor Vieux 2017-11-14 17:05:40 -08:00 committed by GitHub
commit f62aeae977
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 12 deletions

View file

@ -385,35 +385,37 @@ func (s *VolumeStore) create(name, driverName string, opts, labels map[string]st
}
if v != nil {
return v, nil
// there is an existing volume, if we already have this stored locally, return it.
// TODO: there could be some inconsistent details such as labels here
if vv, _ := s.getNamed(v.Name()); vv != nil {
return vv, nil
}
}
// Since there isn't a specified driver name, let's see if any of the existing drivers have this volume name
if driverName == "" {
v, _ := s.getVolume(name)
v, _ = s.getVolume(name)
if v != nil {
return v, nil
}
}
vd, err := volumedrivers.CreateDriver(driverName)
if err != nil {
return nil, &OpErr{Op: "create", Name: name, Err: err}
}
logrus.Debugf("Registering new volume reference: driver %q, name %q", vd.Name(), name)
if v, _ := vd.Get(name); v != nil {
return v, nil
}
v, err = vd.Create(name, opts)
if err != nil {
if _, err := volumedrivers.ReleaseDriver(driverName); err != nil {
logrus.WithError(err).WithField("driver", driverName).Error("Error releasing reference to volume driver")
if v, _ = vd.Get(name); v == nil {
v, err = vd.Create(name, opts)
if err != nil {
if _, err := volumedrivers.ReleaseDriver(driverName); err != nil {
logrus.WithError(err).WithField("driver", driverName).Error("Error releasing reference to volume driver")
}
return nil, err
}
return nil, err
}
s.globalLock.Lock()
s.labels[name] = labels
s.options[name] = opts

View file

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/docker/docker/volume"
"github.com/docker/docker/volume/drivers"
volumetestutils "github.com/docker/docker/volume/testutils"
)
@ -232,3 +233,36 @@ func TestDerefMultipleOfSameRef(t *testing.T) {
t.Fatal(err)
}
}
func TestCreateKeepOptsLabelsWhenExistsRemotely(t *testing.T) {
vd := volumetestutils.NewFakeDriver("fake")
volumedrivers.Register(vd, "fake")
dir, err := ioutil.TempDir("", "test-same-deref")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
s, err := New(dir)
if err != nil {
t.Fatal(err)
}
// Create a volume in the driver directly
if _, err := vd.Create("foo", nil); err != nil {
t.Fatal(err)
}
v, err := s.Create("foo", "fake", nil, map[string]string{"hello": "world"})
if err != nil {
t.Fatal(err)
}
switch dv := v.(type) {
case volume.DetailedVolume:
if dv.Labels()["hello"] != "world" {
t.Fatalf("labels don't match")
}
default:
t.Fatalf("got unexpected type: %T", v)
}
}