Merge pull request #37431 from tonistiigi/mountable-fix

builder: fix duplicate calls to mountable
This commit is contained in:
Yong Tang 2018-07-10 18:26:57 -07:00 committed by GitHub
commit e6aa71b190
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 30 deletions

View file

@ -208,7 +208,6 @@ func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *
output.Write(notVerboseBuffer.Bytes())
}
logrus.Debugf("isflushed %v", output.Flushed())
// Do not write the error in the http output if it's still empty.
// This prevents from writing a 200(OK) when there is an internal error.
if !output.Flushed() {

View file

@ -245,21 +245,23 @@ func (s *snapshotter) Mounts(ctx context.Context, key string) (snapshot.Mountabl
}
if l != nil {
id := identity.NewID()
rwlayer, err := s.opt.LayerStore.CreateRWLayer(id, l.ChainID(), nil)
if err != nil {
return nil, err
}
rootfs, err := rwlayer.Mount("")
if err != nil {
return nil, err
}
mnt := []mount.Mount{{
Source: rootfs.Path(),
Type: "bind",
Options: []string{"rbind"},
}}
return &constMountable{
mounts: mnt,
var rwlayer layer.RWLayer
return &mountable{
acquire: func() ([]mount.Mount, error) {
rwlayer, err = s.opt.LayerStore.CreateRWLayer(id, l.ChainID(), nil)
if err != nil {
return nil, err
}
rootfs, err := rwlayer.Mount("")
if err != nil {
return nil, err
}
return []mount.Mount{{
Source: rootfs.Path(),
Type: "bind",
Options: []string{"rbind"},
}}, nil
},
release: func() error {
_, err := s.opt.LayerStore.ReleaseRWLayer(rwlayer)
return err
@ -269,17 +271,18 @@ func (s *snapshotter) Mounts(ctx context.Context, key string) (snapshot.Mountabl
id, _ := s.getGraphDriverID(key)
rootfs, err := s.opt.GraphDriver.Get(id, "")
if err != nil {
return nil, err
}
mnt := []mount.Mount{{
Source: rootfs.Path(),
Type: "bind",
Options: []string{"rbind"},
}}
return &constMountable{
mounts: mnt,
return &mountable{
acquire: func() ([]mount.Mount, error) {
rootfs, err := s.opt.GraphDriver.Get(id, "")
if err != nil {
return nil, err
}
return []mount.Mount{{
Source: rootfs.Path(),
Type: "bind",
Options: []string{"rbind"},
}}, nil
},
release: func() error {
return s.opt.GraphDriver.Put(id)
},
@ -428,18 +431,37 @@ func (s *snapshotter) Close() error {
return s.db.Close()
}
type constMountable struct {
type mountable struct {
mu sync.Mutex
mounts []mount.Mount
acquire func() ([]mount.Mount, error)
release func() error
}
func (m *constMountable) Mount() ([]mount.Mount, error) {
func (m *mountable) Mount() ([]mount.Mount, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.mounts != nil {
return m.mounts, nil
}
mounts, err := m.acquire()
if err != nil {
return nil, err
}
m.mounts = mounts
return m.mounts, nil
}
func (m *constMountable) Release() error {
func (m *mountable) Release() error {
m.mu.Lock()
defer m.mu.Unlock()
if m.release == nil {
return nil
}
m.mounts = nil
return m.release()
}