-
Notifications
You must be signed in to change notification settings - Fork 42
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
Allow downstream projects to import func-e #446
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2022 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package api allows go projects to use func-e as a library. | ||
package api | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/tetratelabs/func-e/internal/cmd" | ||
"github.com/tetratelabs/func-e/internal/globals" | ||
"github.com/tetratelabs/func-e/internal/version" | ||
) | ||
|
||
// HomeDir is an absolute path which most importantly contains "versions" | ||
// installed from EnvoyVersionsURL. Defaults to "${HOME}/.func-e" | ||
func HomeDir(homeDir string) RunOption { | ||
return func(o *runOpts) { | ||
o.homeDir = homeDir | ||
} | ||
} | ||
|
||
// EnvoyVersionsURL is the path to the envoy-versions.json. | ||
// Defaults to "https://archive.tetratelabs.io/envoy/envoy-versions.json" | ||
func EnvoyVersionsURL(envoyVersionsURL string) RunOption { | ||
return func(o *runOpts) { | ||
o.envoyVersionsURL = envoyVersionsURL | ||
} | ||
} | ||
|
||
// EnvoyVersion overrides the version of Envoy to run. Defaults to the | ||
// contents of "$HomeDir/versions/version". | ||
// | ||
// When that file is missing, it is generated from ".latestVersion" from the | ||
// EnvoyVersionsURL. Its value can be in full version major.minor.patch format, | ||
// e.g. 1.18.1 or without patch component, major.minor, e.g. 1.18. | ||
func EnvoyVersion(envoyVersion string) RunOption { | ||
return func(o *runOpts) { | ||
o.envoyVersion = envoyVersion | ||
} | ||
} | ||
|
||
// Out is where status messages are written. Defaults to os.Stdout | ||
func Out(out io.Writer) RunOption { | ||
return func(o *runOpts) { | ||
o.out = out | ||
} | ||
} | ||
|
||
// RunOption is configuration for Run. | ||
type RunOption func(*runOpts) | ||
|
||
type runOpts struct { | ||
homeDir string | ||
envoyVersion string | ||
envoyVersionsURL string | ||
out io.Writer | ||
} | ||
|
||
// Run downloads Envoy and runs it as a process with the arguments | ||
// passed to it. Use RunOption for configuration options. | ||
func Run(ctx context.Context, args []string, options ...RunOption) error { | ||
ro := &runOpts{ | ||
homeDir: globals.DefaultHomeDir, | ||
envoyVersion: "", // default to lookup | ||
envoyVersionsURL: globals.DefaultEnvoyVersionsURL, | ||
out: os.Stdout, | ||
} | ||
for _, option := range options { | ||
option(ro) | ||
} | ||
|
||
o := globals.GlobalOpts{ | ||
HomeDir: ro.homeDir, | ||
EnvoyVersion: version.PatchVersion(ro.envoyVersion), | ||
EnvoyVersionsURL: ro.envoyVersion, | ||
Out: ro.out, | ||
} | ||
|
||
funcECmd := cmd.NewApp(&o) | ||
|
||
funcERunArgs := []string{"func-e", "--platform", runtime.GOOS + "/" + runtime.GOARCH, "run"} | ||
funcERunArgs = append(funcERunArgs, args...) | ||
|
||
errChan := make(chan error) | ||
go func() { | ||
errChan <- funcECmd.RunContext(ctx, funcERunArgs) | ||
}() | ||
|
||
// Wait for run to exit or an explicit stop. | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case err := <-errChan: | ||
return err | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2022 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tetratelabs/func-e/internal/test" | ||
"github.com/tetratelabs/func-e/internal/version" | ||
) | ||
|
||
var ( | ||
runArgs = []string{"--version"} | ||
) | ||
|
||
func TestRunWithCtxDone(t *testing.T) { | ||
|
||
tmpDir := t.TempDir() | ||
envoyVersion := version.LastKnownEnvoy | ||
versionsServer := test.RequireEnvoyVersionsTestServer(t, envoyVersion) | ||
defer versionsServer.Close() | ||
envoyVersionsURL := versionsServer.URL + "/envoy-versions.json" | ||
b := bytes.NewBufferString("") | ||
|
||
require.Equal(t, 0, b.Len()) | ||
|
||
ctx := context.Background() | ||
// Use a very small ctx timeout | ||
ctx, cancel := context.WithTimeout(ctx, 1*time.Second) | ||
defer cancel() | ||
err := Run(ctx, runArgs, Out(b), HomeDir(tmpDir), EnvoyVersionsURL(envoyVersionsURL)) | ||
require.NoError(t, err) | ||
|
||
require.NotEqual(t, 0, b.Len()) | ||
_, err = os.Stat(filepath.Join(tmpDir, "versions")) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestRunToCompletion(t *testing.T) { | ||
|
||
tmpDir := t.TempDir() | ||
envoyVersion := version.LastKnownEnvoy | ||
versionsServer := test.RequireEnvoyVersionsTestServer(t, envoyVersion) | ||
defer versionsServer.Close() | ||
envoyVersionsURL := versionsServer.URL + "/envoy-versions.json" | ||
b := bytes.NewBufferString("") | ||
|
||
require.Equal(t, 0, b.Len()) | ||
|
||
ctx := context.Background() | ||
// Set a large ctx timeout value | ||
ctx, cancel := context.WithTimeout(ctx, 1000*time.Minute) | ||
defer cancel() | ||
|
||
err := Run(ctx, runArgs, Out(b), HomeDir(tmpDir), EnvoyVersionsURL(envoyVersionsURL)) | ||
require.NoError(t, err) | ||
|
||
require.NotEqual(t, 0, b.Len()) | ||
_, err = os.Stat(filepath.Join(tmpDir, "versions")) | ||
require.NoError(t, err) | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
shouldn't we kill the envoy process in this case? (not sure if there's such function in func-e now)
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.
isn't this done in the called func libs
func-e/internal/envoy/run.go
Line 78 in 82577c9
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.
ok cool but it seems to me that the code block only reaches when the OS signal was sent. Is there any way to verify that it works as intended? e.g. passing the fake config that runs indefinitely in tests?
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.
can you point me to some tests in the code base that verify this, that I can peruse here ?
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.
sorry no idea. Could you do that by yourself ?