Skip to content

Commit

Permalink
Switch extension config file based on languages (#125)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
  • Loading branch information
mathetake authored Mar 25, 2021
1 parent aef7e79 commit 5c46136
Show file tree
Hide file tree
Showing 42 changed files with 486 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ----------------- | ------------------------ | ----------------------------------------------------------------------- |
| `example.yaml` | `Example` descriptor | Describes runtime requirements, e.g. a specific version of `Envoy` |
| `envoy.tmpl.yaml` | `Envoy` bootstrap config | Provides `Envoy` config that demoes extension in action |
| `extension.json` | `Extension` config | Provides configuration for extension itself |
| `${EXTENSION_CONFIG_FILE_NAME}` | `Extension` config | Provides configuration for extension itself |

## Components

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ----------------- | ------------------------ | ----------------------------------------------------------------------- |
| `example.yaml` | `Example` descriptor | Describes runtime requirements, e.g. a specific version of `Envoy` |
| `envoy.tmpl.yaml` | `Envoy` bootstrap config | Provides `Envoy` config that demoes extension in action |
| `extension.json` | `Extension` config | Provides configuration for extension itself |
| `${EXTENSION_CONFIG_FILE_NAME}` | `Extension` config | Provides configuration for extension itself |

## Components

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ----------------- | ------------------------ | ----------------------------------------------------------------------- |
| `example.yaml` | `Example` descriptor | Describes runtime requirements, e.g. a specific version of `Envoy` |
| `envoy.tmpl.yaml` | `Envoy` bootstrap config | Provides `Envoy` config that demoes extension in action |
| `extension.json` | `Extension` config | Provides configuration for extension itself |
| `${EXTENSION_CONFIG_FILE_NAME}` | `Extension` config | Provides configuration for extension itself |

## Components

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# We recommend to use json as the configuration format,
# however, some languages (e.g. TinyGo) does not support ready-to-use json library as of now.
# As a temporary alternative, we use ".txt" format.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0 h1:9CUaOB8CZInfG/VRCjOEOkzS4JLkri/weqogwmrl2a0=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1 h1:m8O4nWCyb+8VlAxVxppobGNnC1N9CoAQBmMfRjTq/hU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"bufio"
"bytes"
"strings"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
Expand Down Expand Up @@ -28,7 +32,20 @@ func (l *accessLogger) OnPluginStart(configurationSize int) types.OnPluginStartS
proxywasm.LogCriticalf("failed to load config: %v", err)
return types.OnPluginStartStatusFailed
}
l.logMessage = string(data)

// Ignore comment lines starting with "#" in the configuration.
// Note that we recommend to use json as the configuration format,
// however, some languages (e.g. TinyGo) does not support ready-to-use json library as of now.
// As a temporary alternative, we use ".txt" format for the plugin configuration.
var lines []string
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "#") {
lines = append(lines, line)
}
}
l.logMessage = strings.Join(lines, "\n")
return types.OnPluginStartStatusOK
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

func TestAccessLogger_OnLog(t *testing.T) {
configuration := `this is my log message`
configuration := `# this is comment line, and should be ignored.
this is my log message`
opt := proxytest.NewEmulatorOption().
WithNewRootContext(newAccessLogger).
WithPluginConfiguration([]byte(configuration))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0 h1:9CUaOB8CZInfG/VRCjOEOkzS4JLkri/weqogwmrl2a0=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1 h1:m8O4nWCyb+8VlAxVxppobGNnC1N9CoAQBmMfRjTq/hU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ func (ctx *rootContext) OnPluginStart(configurationSize int) types.OnPluginStart
return types.OnPluginStartStatusFailed
}

// Each line in the configuration is in the "KEY=VALUE" format.
// Read the configuration.
// Note that we recommend to use json as the configuration format,
// however, some languages (e.g. TinyGo) does not support ready-to-use json library as of now.
// As a temporary alternative, we use ".txt" format for the plugin configuration.
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
tokens := strings.Split(scanner.Text(), "=")
ctx.additionalHeaders[tokens[0]] = tokens[1]
// Ignore comment lines starting with "#".
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
// Each line in the configuration is in the "KEY=VALUE" format.
if tokens := strings.Split(scanner.Text(), "="); len(tokens) == 2 {
ctx.additionalHeaders[tokens[0]] = tokens[1]
}
}
return types.OnPluginStartStatusOK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

func TestHttpFilter_OnHttpRequestHeaders(t *testing.T) {
configuration := `HELLO=WORLD
configuration := `# this is comment line, and should be ignored.
HELLO=WORLD
ENVOY=ISTIO`
opt := proxytest.NewEmulatorOption().
WithNewRootContext(newRootContext).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0 h1:9CUaOB8CZInfG/VRCjOEOkzS4JLkri/weqogwmrl2a0=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.0/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1 h1:m8O4nWCyb+8VlAxVxppobGNnC1N9CoAQBmMfRjTq/hU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.1.1/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"bufio"
"bytes"
"strings"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
Expand Down Expand Up @@ -34,7 +38,20 @@ func (ctx *rootContext) OnPluginStart(configurationSize int) types.OnPluginStart
proxywasm.LogCriticalf("failed to load config: %v", err)
return types.OnPluginStartStatusFailed
}
ctx.config = string(data)

// Ignore comment lines starting with "#" in the extension.txt.
// Note that we recommend to use json as the configuration format,
// however, some languages (e.g. TinyGo) does not support ready-to-use json library as of now.
// As a temporary alternative, we use ".txt" format for the plugin configuration.
var lines []string
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "#") {
lines = append(lines, line)
}
}
ctx.config = strings.Join(lines, "\n")
return types.OnPluginStartStatusOK
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

func TestNetworkFilter_OnNewConnection(t *testing.T) {
configuration := `message: this is new connection!`
configuration := `# this is comment line, and should be ignored.
message: this is new connection!`
opt := proxytest.NewEmulatorOption().
WithPluginConfiguration([]byte(configuration)).
WithNewRootContext(newRootContext)
Expand All @@ -28,7 +29,7 @@ func TestNetworkFilter_OnNewConnection(t *testing.T) {

// Retrieve logs emitted to Envoy.
logs := host.GetLogs(types.LogLevelInfo)
require.Contains(t, logs, configuration)
require.Contains(t, logs, "message: this is new connection!")
}

func TestNetworkFilter_counter(t *testing.T) {
Expand Down
54 changes: 52 additions & 2 deletions pkg/cmd/extension/example/cmd_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,17 @@ Run 'getenvoy extension examples add --help' for usage.
Done!
`))
By("verifying file system")
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/README.md")).To(BeAnExistingFile())
readmePath := filepath.Join(tempDir, ".getenvoy/extension/examples/default/README.md")
Expect(readmePath).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/envoy.tmpl.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/example.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/extension.json")).To(BeAnExistingFile())
// Check README substitution: ${EXTENSION_CONFIG_FILE_NAME} must be replaced with "extension.json".
data, err := ioutil.ReadFile(readmePath)
Expect(err).ToNot(HaveOccurred())
readme := string(data)
Expect(readme).To(ContainSubstring("extension.json"))
Expect(readme).NotTo(ContainSubstring("EXTENSION_CONFIG_FILE_NAME"))
})

It("should create example setup with a given --name", func() {
Expand All @@ -160,10 +167,17 @@ Done!
Done!
`))
By("verifying file system")
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/advanced/README.md")).To(BeAnExistingFile())
readmePath := filepath.Join(tempDir, ".getenvoy/extension/examples/advanced/README.md")
Expect(readmePath).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/advanced/envoy.tmpl.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/advanced/example.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/advanced/extension.json")).To(BeAnExistingFile())
// Check README substitution: ${EXTENSION_CONFIG_FILE_NAME} must be replaced with "extension.json".
data, err := ioutil.ReadFile(readmePath)
Expect(err).ToNot(HaveOccurred())
readme := string(data)
Expect(readme).To(ContainSubstring("extension.json"))
Expect(readme).NotTo(ContainSubstring("${EXTENSION_CONFIG_FILE_NAME}"))
})

It("should fail if such example already exists", func() {
Expand All @@ -186,6 +200,42 @@ Done!
Run 'getenvoy extension examples add --help' for usage.
`))
})

It("should create 'default' example setup when no --name is omitted for TinyGo", func() {
By("simulating a workspace without any examples")
err := copy.Copy("testdata/workspace4", tempDir)
Expect(err).NotTo(HaveOccurred())

By("changing to a workspace dir")
chdir(tempDir)

By("running command")
c.SetArgs([]string{"extension", "examples", "add"})
err = cmdutil.Execute(c)
Expect(err).ToNot(HaveOccurred())

By("verifying command output")
Expect(stdout.String()).To(BeEmpty())
Expect(stderr.String()).To(Equal(`Scaffolding a new example setup:
* .getenvoy/extension/examples/default/README.md
* .getenvoy/extension/examples/default/envoy.tmpl.yaml
* .getenvoy/extension/examples/default/example.yaml
* .getenvoy/extension/examples/default/extension.txt
Done!
`))
By("verifying file system")
readmePath := filepath.Join(tempDir, ".getenvoy/extension/examples/default/README.md")
Expect(readmePath).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/envoy.tmpl.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/example.yaml")).To(BeAnExistingFile())
Expect(filepath.Join(tempDir, ".getenvoy/extension/examples/default/extension.txt")).To(BeAnExistingFile())
// Check README substitution: ${EXTENSION_CONFIG_FILE_NAME} must be replaced with "extension.txt".
data, err := ioutil.ReadFile(readmePath)
Expect(err).ToNot(HaveOccurred())
readme := string(data)
Expect(readme).To(ContainSubstring("extension.txt"))
Expect(readme).NotTo(ContainSubstring("${EXTENSION_CONFIG_FILE_NAME}"))
})
})

Context("outside of a workspace directory", func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Envoy Wasm extension created with getenvoy toolkit.
#
kind: Extension

name: me.filters.http.my_http_filter

category: envoy.filters.http
language: tinygo

# Runtime the extension is being developed against.
runtime:
envoy:
version: standard:1.17.0
37 changes: 37 additions & 0 deletions pkg/cmd/extension/run/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,43 @@ Run 'getenvoy extension run --help' for usage.
Done!
docker stderr
envoy stderr
`))

By("verifying Envoy config")
bootstrap := envoyCaptured.readFileToJSON("envoy.tmpl.yaml")
Expect(bootstrap).NotTo(BeEmpty())
})

It("should create default example if missing for TinyGo", func() {
By("simulating a workspace without 'default' example")
tempDir, err := ioutil.TempDir("", "")
Expect(err).NotTo(HaveOccurred())
defer func() {
Expect(os.RemoveAll(tempDir)).To(Succeed())
}()
err = copy.Copy("testdata/workspace_tinygo", tempDir)
Expect(err).NotTo(HaveOccurred())

By("changing to a workspace dir")
workspaceDir := chdir(tempDir)

By("running command")
c.SetArgs([]string{"extension", "run"})
err = cmdutil.Execute(c)
Expect(err).ToNot(HaveOccurred())

By("verifying command output")
Expect(stdout.String()).To(Equal(fmt.Sprintf(`%s/docker run -u 1001:1002 --rm -t -v %s:/source -w /source --init getenvoy/extension-tinygo-builder:latest build --output-file build/extension.wasm
%s/builds/standard/1.17.0/%s/bin/envoy -c %s/envoy.tmpl.yaml
`, dockerDir, workspaceDir, getenvoyHomeDir, platform, envoyCaptured.cwd())))
Expect(stderr.String()).To(Equal(`Scaffolding a new example setup:
* .getenvoy/extension/examples/default/README.md
* .getenvoy/extension/examples/default/envoy.tmpl.yaml
* .getenvoy/extension/examples/default/example.yaml
* .getenvoy/extension/examples/default/extension.txt
Done!
docker stderr
envoy stderr
`))

By("verifying Envoy config")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Envoy Wasm extension created with getenvoy toolkit.
#
kind: Extension

name: mycompany.filters.http.mytest

category: envoy.filters.http
language: tinygo

# Runtime the extension is being developed against.
runtime:
envoy:
version: standard:1.17.0
6 changes: 3 additions & 3 deletions pkg/extension/example/init/registry/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func globalRegistry() registry {
}

// Get returns an example template registered in a global registry for a given
// extension category and name.
func Get(category extension.Category, name string) (*Entry, error) {
return globalRegistry().Get(category, name)
// extension category, name and language.
func Get(descriptor *extension.Descriptor, name string) (*Entry, error) {
return globalRegistry().Get(descriptor, name)
}
Loading

0 comments on commit 5c46136

Please sign in to comment.