Cache shim v2 exec.LookPath results
Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
parent
3eae8b9c3f
commit
09bf314bfd
@ -24,6 +24,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
@ -32,6 +33,8 @@ import (
|
|||||||
|
|
||||||
const shimBinaryFormat = "containerd-shim-%s-%s"
|
const shimBinaryFormat = "containerd-shim-%s-%s"
|
||||||
|
|
||||||
|
var runtimePaths sync.Map
|
||||||
|
|
||||||
// Command returns the shim command with the provided args and configuration
|
// Command returns the shim command with the provided args and configuration
|
||||||
func Command(ctx context.Context, runtime, containerdAddress, path string, cmdArgs ...string) (*exec.Cmd, error) {
|
func Command(ctx context.Context, runtime, containerdAddress, path string, cmdArgs ...string) (*exec.Cmd, error) {
|
||||||
ns, err := namespaces.NamespaceRequired(ctx)
|
ns, err := namespaces.NamespaceRequired(ctx)
|
||||||
@ -52,7 +55,12 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr
|
|||||||
if name == "" {
|
if name == "" {
|
||||||
return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime)
|
return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdPath string
|
var cmdPath string
|
||||||
|
cmdPathI, cmdPathFound := runtimePaths.Load(name)
|
||||||
|
if cmdPathFound {
|
||||||
|
cmdPath = cmdPathI.(string)
|
||||||
|
} else {
|
||||||
var lerr error
|
var lerr error
|
||||||
if cmdPath, lerr = exec.LookPath(name); lerr != nil {
|
if cmdPath, lerr = exec.LookPath(name); lerr != nil {
|
||||||
if eerr, ok := lerr.(*exec.Error); ok {
|
if eerr, ok := lerr.(*exec.Error); ok {
|
||||||
@ -65,6 +73,12 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if cmdPathI, cmdPathFound = runtimePaths.LoadOrStore(name, cmdPath); cmdPathFound {
|
||||||
|
// We didn't store cmdPath we loaded an already cached value. Use it.
|
||||||
|
cmdPath = cmdPathI.(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(cmdPath, args...)
|
cmd := exec.Command(cmdPath, args...)
|
||||||
cmd.Dir = path
|
cmd.Dir = path
|
||||||
cmd.Env = append(os.Environ(), "GOMAXPROCS=2")
|
cmd.Env = append(os.Environ(), "GOMAXPROCS=2")
|
||||||
|
@ -33,7 +33,7 @@ golang.org/x/sync 450f422ab23cf9881c94e2db30cac0eb1b7cf80c
|
|||||||
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895
|
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895
|
||||||
github.com/grpc-ecosystem/go-grpc-prometheus 6b7015e65d366bf3f19b2b2a000a831940f0f7e0
|
github.com/grpc-ecosystem/go-grpc-prometheus 6b7015e65d366bf3f19b2b2a000a831940f0f7e0
|
||||||
github.com/Microsoft/go-winio v0.4.11
|
github.com/Microsoft/go-winio v0.4.11
|
||||||
github.com/Microsoft/hcsshim v0.8.1
|
github.com/Microsoft/hcsshim v0.8.2
|
||||||
google.golang.org/genproto d80a6e20e776b0b17a324d0ba1ab50a39c8e8944
|
google.golang.org/genproto d80a6e20e776b0b17a324d0ba1ab50a39c8e8944
|
||||||
golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4
|
golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4
|
||||||
github.com/containerd/ttrpc 2a805f71863501300ae1976d29f0454ae003e85a
|
github.com/containerd/ttrpc 2a805f71863501300ae1976d29f0454ae003e85a
|
||||||
|
32
vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go
generated
vendored
32
vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go
generated
vendored
@ -9,6 +9,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
|
irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
|
||||||
"github.com/containerd/go-runc"
|
"github.com/containerd/go-runc"
|
||||||
@ -23,10 +24,35 @@ const (
|
|||||||
Text Format = "text"
|
Text Format = "text"
|
||||||
// JSON is the JSON formatted log output.
|
// JSON is the JSON formatted log output.
|
||||||
JSON Format = "json"
|
JSON Format = "json"
|
||||||
|
|
||||||
command = "runhcs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var runhcsPath atomic.Value
|
||||||
|
|
||||||
|
func getCommandPath() string {
|
||||||
|
const command = "runhcs.exe"
|
||||||
|
|
||||||
|
pathi := runhcsPath.Load()
|
||||||
|
if pathi == nil {
|
||||||
|
path, err := exec.LookPath(command)
|
||||||
|
if err != nil {
|
||||||
|
// Failed to look up command just use it directly and let the
|
||||||
|
// Windows loader find it.
|
||||||
|
path = command
|
||||||
|
runhcsPath.Store(path)
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
apath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
// We couldnt make `path` an `AbsPath`. Just use `path` directly and
|
||||||
|
// let the Windows loader find it.
|
||||||
|
apath = path
|
||||||
|
}
|
||||||
|
runhcsPath.Store(apath)
|
||||||
|
return apath
|
||||||
|
}
|
||||||
|
return pathi.(string)
|
||||||
|
}
|
||||||
|
|
||||||
var bytesBufferPool = sync.Pool{
|
var bytesBufferPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return bytes.NewBuffer(nil)
|
return bytes.NewBuffer(nil)
|
||||||
@ -84,7 +110,7 @@ func (r *Runhcs) args() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runhcs) command(context context.Context, args ...string) *exec.Cmd {
|
func (r *Runhcs) command(context context.Context, args ...string) *exec.Cmd {
|
||||||
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
|
cmd := exec.CommandContext(context, getCommandPath(), append(r.args(), args...)...)
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user