Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kuma-dp: improve envoy binary lookup #268

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 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,11 @@ package envoy
import (
"context"
"io"
"os"
"os/exec"

"path/filepath"

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

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

func getSelfPath() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSelfDir() seems to be a more accurate name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string returned by this function is the actual full path of the binary, so it's more accurate to call it "path".

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 "", errors.New("could not find binary")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change the message to .Errorf("could not find Envoy binary at any of the following locations: %v", candidatePaths) ?

}

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 +97,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a unit test for this ?

runLog.Error(err, "Envoy binary not found; make sure it is in PATH or in the same directory as "+os.Args[0])
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, if envoy binary is not found, konvoy-dp exists with status code 0 and prints nothing on the console.

You should return the error instead of logging it.

}

args := []string{
"-c", configFile,
// "hot restart" (enabled by default) requires each Envoy instance to have
Expand All @@ -63,7 +116,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