Skip to content

Commit

Permalink
builder: fix duplicate calls to mountable
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jul 10, 2018
1 parent c04f9ae commit ffa7233
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
1 change: 0 additions & 1 deletion api/server/router/build/build_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
80 changes: 51 additions & 29 deletions builder/builder-next/adapters/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
},
Expand Down Expand Up @@ -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()
}

0 comments on commit ffa7233

Please sign in to comment.