Skip to content

Commit

Permalink
kuma-dp: improve envoy binary lookup
Browse files Browse the repository at this point in the history
- Search in configured path
- Fallback to same directory as kuma-dp
  • Loading branch information
gszr committed Sep 17, 2019
1 parent 0187c90 commit 223377f
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion app/kuma-dp/pkg/dataplane/envoy/envoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package envoy
import (
"context"
"io"
"os"
"os/exec"

err "errors"
"path/filepath"

"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"

Expand Down Expand Up @@ -38,6 +42,49 @@ type Envoy struct {
opts Opts
}

func getSelfPath() (string, error) {
ex, err := os.Executable()
if err != nil {
return "", err
}

return filepath.Dir(ex), nil
}

func lookupBinaryPath(candidatePaths []string) (string, error) {
for _, candidatePath := range candidatePaths {
path, err := exec.LookPath(candidatePath)
if err == nil {
return path, nil
}
}

return "", err.New("could not find binary")
}

func lookupEnvoyPath(configuredPath string) (string, error) {
selfPath, err := getSelfPath()
if err != nil {
return "", err
}

cwd, err := os.Getwd()
if err != nil {
return "", err
}

path, err := lookupBinaryPath([]string{
configuredPath,
selfPath + "/envoy",
cwd + "/envoy",
})
if err != nil {
return "", err
}

return path, nil
}

func (e *Envoy) Run(stop <-chan struct{}) error {
bootstrapConfig, err := e.opts.Generator(e.opts.Config)
if err != nil {
Expand All @@ -51,6 +98,13 @@ func (e *Envoy) Run(stop <-chan struct{}) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

binaryPathConfig := e.opts.Config.DataplaneRuntime.BinaryPath
resolvedPath, err := lookupEnvoyPath(binaryPathConfig)
if err != nil {
runLog.Error(err, "Envoy binary not found; make sure it is in PATH or in the same directory as "+os.Args[0])
return nil
}

args := []string{
"-c", configFile,
// "hot restart" (enabled by default) requires each Envoy instance to have
Expand All @@ -63,7 +117,7 @@ func (e *Envoy) Run(stop <-chan struct{}) error {
// so, let's turn it off to simplify getting started experience.
"--disable-hot-restart",
}
command := exec.CommandContext(ctx, e.opts.Config.DataplaneRuntime.BinaryPath, args...)
command := exec.CommandContext(ctx, resolvedPath, args...)
command.Stdout = e.opts.Stdout
command.Stderr = e.opts.Stderr
if err := command.Start(); err != nil {
Expand Down

0 comments on commit 223377f

Please sign in to comment.