-
Notifications
You must be signed in to change notification settings - Fork 339
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,11 @@ package envoy | |
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"path/filepath" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/pkg/errors" | ||
|
||
|
@@ -38,6 +41,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 "", errors.New("could not find binary") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we change the message to |
||
} | ||
|
||
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 { | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment, if You should return the error instead of logging it. |
||
} | ||
|
||
args := []string{ | ||
"-c", configFile, | ||
// "hot restart" (enabled by default) requires each Envoy instance to have | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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 nameThere was a problem hiding this comment.
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".