-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes from statik to go:embed for example and extension templates (#…
…129) This reduces build complexity by eliminating a generation step with go:embed. This implicitly reduces tech debt even more because not only were we using a stalled project, statik, but also a fork of it. go:embed is not perfect, as it disallows the file name go.mod. The workaround is to rename it to go.mod_ per golang/go#45197. While this is imperfect an if-statement is a far better punch than a dependency on a fork of a stalled project which also requires a codegen step. Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io> Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
3841240
commit 212d750
Showing
22 changed files
with
186 additions
and
69 deletions.
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
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
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 |
---|---|---|
|
@@ -3,6 +3,3 @@ dist | |
/getenvoy | ||
/build/ | ||
.idea | ||
|
||
# code generated by `github.com/rakyll/statik` | ||
statik.go |
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
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
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
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,67 @@ | ||
# Notable Rationale | ||
|
||
## go:embed for embedding example and init templates | ||
|
||
This project resolves templates into working examples or extensions via `getenvoy example add` and | ||
`getenvoy extension init`. Input [example](data/example/init/templates) and [extension](data/extension/init/templates) | ||
templates are embedded in the `getenvoy` binary for user convenience. | ||
|
||
We implement embedding with a Go 1.16+ directive `//go:embed templates/*`, which presents an `fs.FS` interface of the | ||
directory tree. Using this requires no extra build steps. Ex. `go build -o getenvoy ./cmd/getenvoy/main.go` works. | ||
|
||
However, there are some constraints to understand. It is accepted that while imperfect, this solution is an acceptable | ||
alternative to a custom solution. | ||
|
||
### Embedded files must be in a sub-path | ||
The go source file embedding content via `go:embed` must reference paths in the current directory tree. In other words, | ||
paths like `../../templates` are not allowed. | ||
|
||
This means we have to move the embedded directory tree where code refers to it, or make an accessor utility for each | ||
directory root. We use the latter pattern, specifically here: | ||
* [data/example/init/templates.go](data/example/init/templates.go) | ||
* [data/extension/init/templates.go](data/extension/init/templates.go) | ||
|
||
See https://pkg.go.dev/embed#hdr-Directives for more information. | ||
|
||
### Limitations of `go:embed` impact extension init templates | ||
`getenvoy extension init` creates a workspace directory for a given category and programming language. Some constraints | ||
of `go:embed` impact how these template directories are laid out, and the workarounds are file rename in basis. The | ||
impacts are noted below for information and future follow-up: | ||
|
||
#### `go:embed` doesn't traverse hidden directories, but Rust projects include a hidden directory | ||
Our Rust examples use [Cargo](https://doc.rust-lang.org/cargo/reference/config.html) as a build tool. This stores | ||
configuration in a hidden directory `.cargo`. As of Go 1.16, hidden directories are not yet supported with `go:embed`. | ||
See https://github.com/golang/go/issues/43854 | ||
|
||
#### `go:embed` stops traversing at module boundaries, and TinyGo examples look like sub-modules | ||
Go modules need to be built from the zip-file uploaded to the mirror. This implies `go:embed` must refer to the | ||
current module. https://github.com/golang/go/issues/45197 explains embedding stops traversing when it encounters a | ||
`go.mod` file, and there is no plan to change this. | ||
|
||
This presents a challenge with TinyGo templates, which except for parameterization like `module {{ .Extension.Name }}`, | ||
appear as normal go modules (ex `go.mod`) files. When `go:embed` encounters a `go.mod` file, it stops traversing. If we | ||
didn't work around this, no TinyGo project would end up in the embedded filesystem. | ||
|
||
The workaround is to rename the template `go.mod` to `go.mod_`, but this introduces a couple glitches: | ||
|
||
* TinyGo extension templates imports appear in the root project after running `go mod tidy` | ||
* As of the writing, there is only one github.com/tetratelabs/proxy-wasm-go-sdk | ||
* TinyGo extension templates can't be run from their directory without temporarily renaming `go.mod_` back to `go.mod` | ||
* To do this anyway, you'd need to fix any template variables like `module {{ .Extension.Name }}` | ||
|
||
### Former solution | ||
In the past, we embedded via [statik](https://github.com/rakyll/statik). This is a code generation solution which | ||
encodes files into internal variables. Entry-points use an `http.FileSystem` to access the embedded files. | ||
|
||
This solution worked well, except that it introduced build complexity. Code generation required an `init` phase in the | ||
`Makefile`, as well lint exclusions. This implied steps for developers to remember and CI to invoke. | ||
|
||
It also introduced maintenance and risk as the statik library stalled. This project ended up pinned to a personal fork | ||
in order to work around unmerged pull requests. | ||
``` | ||
replace github.com/rakyll/statik => github.com/yskopets/statik v0.1.8-0.20200501213002-c2d8dcc79889 | ||
``` | ||
|
||
Go 1.16's `go:embed` has limitations of its own, but brings with it shared understanding. While we may need workarounds | ||
to some issues, those issues are understood by the go community. `go:embed` does not require our maintenance, nor has | ||
any key person risk. |
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,35 @@ | ||
// Copyright 2021 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 init | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
// templatesFs includes only the relative path of "templates". | ||
// | ||
// See RATIONALE.md for more information on embedding | ||
//go:embed templates/* | ||
var templatesFs embed.FS | ||
|
||
// GetTemplates returns the templates directory as a filesystem | ||
func GetTemplates() fs.FS { | ||
f, err := fs.Sub(templatesFs, "templates") | ||
if err != nil { | ||
panic(err) // unexpected or a typo | ||
} | ||
return f | ||
} |
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,35 @@ | ||
// Copyright 2021 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 init | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
// templatesFs includes only the relative path of "templates". | ||
// | ||
// See RATIONALE.md for more information on embedding | ||
//go:embed templates/* | ||
var templatesFs embed.FS | ||
|
||
// GetTemplates returns the templates directory as a filesystem | ||
func GetTemplates() fs.FS { | ||
f, err := fs.Sub(templatesFs, "templates") | ||
if err != nil { | ||
panic(err) // unexpected or a typo | ||
} | ||
return f | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# We rename .cargo to cargo See /RATIONALE.md for why | ||
.cargo | ||
Cargo.lock | ||
target/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
build/ | ||
|
||
# We rename go.mod to go.mod_ See /RATIONALE.md for why | ||
go.mod |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
Oops, something went wrong.