Skip to content

Commit ad5b724

Browse files
author
Adrian Cole
committed
Changes from statik to go:embed for examples serving
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. Signed-off-by: Adrian Cole <[email protected]>
1 parent 4f89255 commit ad5b724

File tree

19 files changed

+123
-80
lines changed

19 files changed

+123
-80
lines changed

.circleci/config.yml

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jobs:
3939
resource_class: medium+
4040
steps:
4141
- checkout
42-
- run: make init
4342
# prefetch implicit version needed by pkg/binary/envoy/controlplane/istio_test.go until #136. This avoids:
4443
# Unable to start Envoy process: fork/exec /home/circleci/.getenvoy/builds/standard/1.11.0/linux_glibc/bin/envoy: text file busy
4544
- run: go run cmd/getenvoy/main.go fetch standard:1.11.0

.github/workflows/commit.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ jobs:
2929
with:
3030
go-version: '1.16.2'
3131

32-
- name: "Init on first use"
33-
run: make init
34-
3532
- name: "Build `getenvoy` and `e2e` binaries"
3633
run: make bin
3734

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ dist
33
/getenvoy
44
/build/
55
.idea
6-
7-
# code generated by `github.com/rakyll/statik`
8-
statik.go

.golangci.yml

-13
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ run:
1818
# timeout for analysis, e.g. 30s, 5m, default is 1m
1919
deadline: 5m
2020

21-
# which dirs to skip: they won't be analyzed;
22-
# can use regexp here: generated.*, regexp is applied on full path;
23-
# default value is empty list, but next dirs are always skipped independently
24-
# from this option's value:
25-
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
26-
skip-dirs:
27-
- pkg/binary/envoytest
28-
- vendor$
29-
3021
# If invoked with -mod=readonly, the go command is disallowed from the implicit
3122
# automatic updating of go.mod described above. Instead, it fails when any changes
3223
# to go.mod are needed. This setting is most useful to check that go.mod does
@@ -141,11 +132,7 @@ issues:
141132
- errcheck
142133
- dupl
143134
- gosec
144-
145-
# Exclude lll issues for long lines with go:generate
146-
- linters:
147135
- lll
148-
source: "^//go:generate "
149136

150137
# Independently from option `exclude` we use default exclude patterns,
151138
# it can be disabled by this option. To list all

.goreleaser.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
project_name: getenvoy
1616
env:
1717
- GO111MODULE=on
18-
before:
19-
hooks:
20-
- make init
2118
builds:
2219
- binary: getenvoy
2320
ldflags: "-s -w -X github.com/tetratelabs/getenvoy/pkg/version.version={{.Version}}"

Makefile

+11-16
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ GETENVOY_OUT_PATH = $(BIN_DIR)/$(1)/$(2)/getenvoy
6363

6464
define GEN_GETENVOY_BUILD_TARGET
6565
.PHONY: $(call GETENVOY_OUT_PATH,$(1),$(2))
66-
$(call GETENVOY_OUT_PATH,$(1),$(2)): generate
66+
$(call GETENVOY_OUT_PATH,$(1),$(2)):
6767
CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build $(GO_LD_FLAGS) -o $(call GETENVOY_OUT_PATH,$(1),$(2)) ./cmd/getenvoy/main.go
6868
endef
6969
$(foreach os,$(GOOSES),$(foreach arch,$(GOARCHS),$(eval $(call GEN_GETENVOY_BUILD_TARGET,$(os),$(arch)))))
@@ -77,17 +77,10 @@ $(call E2E_OUT_PATH,$(1),$(2)):
7777
endef
7878
$(foreach os,$(GOOSES),$(foreach arch,$(GOARCHS),$(eval $(call GEN_E2E_BUILD_TARGET,$(os),$(arch)))))
7979

80-
.PHONY: init
81-
init: generate
82-
8380
.PHONY: deps
8481
deps:
8582
go mod download
8683

87-
.PHONY: generate
88-
generate: deps
89-
go generate ./pkg/...
90-
9184
.PHONY: build
9285
build: $(call GETENVOY_OUT_PATH,$(GOOS),$(GOARCH))
9386

@@ -100,12 +93,12 @@ release.dryrun:
10093
goreleaser release --skip-publish --snapshot --rm-dist
10194

10295
.PHONY: test
103-
test: generate
96+
test:
10497
docker-compose up -d
10598
go test $(GO_TEST_OPTS) $(GO_TEST_EXTRA_OPTS) $(TEST_PKG_LIST)
10699

107100
.PHONY: test.ci
108-
test.ci: generate
101+
test.ci:
109102
go test $(GO_TEST_OPTS) $(GO_TEST_EXTRA_OPTS) $(TEST_PKG_LIST)
110103

111104
.PHONY: e2e
@@ -129,7 +122,7 @@ endef
129122
$(foreach os,$(GOOSES),$(foreach arch,$(GOARCHS),$(eval $(call GEN_BIN_GOOS_GOARCH_TARGET,$(os),$(arch)))))
130123

131124
.PHONY: coverage
132-
coverage: generate
125+
coverage:
133126
mkdir -p "$(shell dirname "$(COVERAGE_PROFILE)")"
134127
go test $(GO_COVERAGE_OPTS) $(GO_COVERAGE_EXTRA_OPTS) -coverprofile="$(COVERAGE_PROFILE)" $(COVERAGE_PKG_LIST)
135128
go tool cover -html="$(COVERAGE_PROFILE)" -o "$(COVERAGE_REPORT)"
@@ -186,12 +179,13 @@ builders.pull: $(foreach lang,$(BUILDERS_LANGS), pull/builder/$(lang))
186179

187180
LINT_OPTS ?= --timeout 5m
188181
.PHONY: lint
189-
# generate must be called while generated source is still used
190-
lint: generate $(GOLANGCI_LINT) $(SHFMT) $(LICENSER) .golangci.yml ## Run the linters
182+
lint: $(GOLANGCI_LINT) $(SHFMT) $(LICENSER) .golangci.yml ## Run the linters
191183
@echo "--- lint ---"
192184
@$(SHFMT) -d .
193185
@$(LICENSER) verify -r .
194-
@$(GOLANGCI_LINT) run $(LINT_OPTS) --config .golangci.yml
186+
# We skip tinygo templates which will fail lint. Since skip-dirs does not apply to go modules, we externally filter.
187+
# See https://github.com/golangci/golangci-lint/issues/301#issuecomment-441311986 for explanation.
188+
@go list -f "{{.Dir}}" ./... | grep -v "/tinygo/" | xargs $(GOLANGCI_LINT) run $(LINT_OPTS) --config .golangci.yml
195189

196190
# The goimports tool does not arrange imports in 3 blocks if there are already more than three blocks.
197191
# To avoid that, before running it, we collapse all imports in one block, then run the formatter.
@@ -231,6 +225,7 @@ check: ## CI blocks merge until this passes. If this fails, run "make check" lo
231225
fi
232226

233227
.PHONY: clean
234-
clean: ## Clean all binaries
228+
clean: $(GOLANGCI_LINT) ## Clean all binaries
235229
@echo "--- $@ ---"
236-
go clean -testcache
230+
@go clean -testcache
231+
@$(GOLANGCI_LINT) cache clean

data/example/init/templates.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2021 Tetrate
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package init
16+
17+
import (
18+
"embed"
19+
"io/fs"
20+
)
21+
22+
// templatesFs includes only the relative path of "templates".
23+
//
24+
// Assets must be in this directory because go:embed doesn't support navigation outside (ex. ../)
25+
// See https://pkg.go.dev/embed#hdr-Directives
26+
//go:embed templates/*
27+
var templatesFs embed.FS
28+
29+
// GetTemplates returns the templates directory as a filesystem
30+
func GetTemplates() fs.FS {
31+
f, err := fs.Sub(templatesFs, "templates")
32+
if err != nil {
33+
panic(err) // unexpected or a typo
34+
}
35+
return f
36+
}

data/extension/init/templates.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2021 Tetrate
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package init
16+
17+
import (
18+
"embed"
19+
"io/fs"
20+
)
21+
22+
// templatesFs includes only the relative path of "templates".
23+
//
24+
// Assets must be in this directory because go:embed doesn't support navigation outside (ex. ../)
25+
// See https://pkg.go.dev/embed#hdr-Directives
26+
//go:embed templates/*
27+
var templatesFs embed.FS
28+
29+
// GetTemplates returns the templates directory as a filesystem
30+
func GetTemplates() fs.FS {
31+
f, err := fs.Sub(templatesFs, "templates")
32+
if err != nil {
33+
panic(err) // unexpected or a typo
34+
}
35+
return f
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# We rename go.mod to go.mod_ to workaround https://github.com/golang/go/issues/45197
2+
go.mod

go.mod

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module github.com/tetratelabs/getenvoy
22

3+
// This project uses go:embed, so requires minimally go 1.16
34
go 1.16
45

56
require (
@@ -16,7 +17,7 @@ require (
1617
github.com/go-ole/go-ole v1.2.4 // indirect
1718
github.com/golang/protobuf v1.3.5
1819
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
19-
github.com/manifoldco/promptui v0.0.0-00010101000000-000000000000
20+
github.com/manifoldco/promptui v0.8.0
2021
github.com/mattn/go-isatty v0.0.12
2122
github.com/mattn/go-shellwords v1.0.10
2223
github.com/mholt/archiver v3.1.1+incompatible
@@ -27,7 +28,6 @@ require (
2728
github.com/opencontainers/selinux v1.8.0 // indirect
2829
github.com/otiai10/copy v1.2.0
2930
github.com/pkg/errors v0.9.1
30-
github.com/rakyll/statik v0.0.0-00010101000000-000000000000
3131
github.com/schollz/progressbar/v2 v2.13.2
3232
github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984
3333
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
@@ -36,6 +36,7 @@ require (
3636
github.com/tetratelabs/getenvoy-package v0.0.0-20190730071641-da31aed4333e
3737
github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8
3838
github.com/tetratelabs/multierror v1.1.0
39+
github.com/tetratelabs/proxy-wasm-go-sdk v0.12.0
3940
gotest.tools v2.2.0+incompatible
4041
istio.io/api v0.0.0-20200227213531-891bf31f3c32
4142
istio.io/istio v0.0.0-20200304114959-c3c353285578
@@ -47,7 +48,3 @@ replace github.com/Azure/go-autorest/autorest => github.com/Azure/go-autorest/au
4748
replace github.com/docker/docker => github.com/docker/docker v17.12.1-ce+incompatible
4849

4950
replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.3.1
50-
51-
replace github.com/manifoldco/promptui => github.com/yskopets/promptui v0.7.1-0.20200429230902-361491009c11
52-
53-
replace github.com/rakyll/statik => github.com/yskopets/statik v0.1.8-0.20200501213002-c2d8dcc79889

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN
514514
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
515515
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
516516
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
517+
github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
518+
github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
517519
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
518520
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
519521
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
@@ -747,6 +749,8 @@ github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8 h1:a7FN/XPymdzttMa
747749
github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8/go.mod h1:w+dEBsxcYEFg0I6whrgkMzjD8GBBQgmDq9hykB30pt8=
748750
github.com/tetratelabs/multierror v1.1.0 h1:cKmV/Pbf42K5wp8glxa2YIausbxIraPN8fzru9Pn1Cg=
749751
github.com/tetratelabs/multierror v1.1.0/go.mod h1:kH3SzI/z+FwEbV9bxQDx4GiIgE2djuyb8wiB2DaUBnY=
752+
github.com/tetratelabs/proxy-wasm-go-sdk v0.12.0 h1:n2foXJoViPNPz5Jr3bHMc8a4WXBuXwQ3bKJYpfXZUPE=
753+
github.com/tetratelabs/proxy-wasm-go-sdk v0.12.0/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
750754
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
751755
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
752756
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -775,10 +779,6 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
775779
github.com/yashtewari/glob-intersection v0.0.0-20180206001645-7af743e8ec84/go.mod h1:HptNXiXVDcJjXe9SqMd0v2FsL9f8dz4GnXgltU6q/co=
776780
github.com/yl2chen/cidranger v0.0.0-20180214081945-928b519e5268 h1:lkoOjizoHqOcEFsvYGE5c8Ykdijjnd0R3r1yDYHzLno=
777781
github.com/yl2chen/cidranger v0.0.0-20180214081945-928b519e5268/go.mod h1:mq0zhomp/G6rRTb0dvHWXRHr/2+Qgeq5hMXfJ670+i4=
778-
github.com/yskopets/promptui v0.7.1-0.20200429230902-361491009c11 h1:MlzMpHq1fRfH1RYzfQ7Ch7JjdGnBq/m29jJtPOExWuw=
779-
github.com/yskopets/promptui v0.7.1-0.20200429230902-361491009c11/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
780-
github.com/yskopets/statik v0.1.8-0.20200501213002-c2d8dcc79889 h1:f62aKW+gryXYYtNut+3b6i5n1ioXCXJWpDgA11l6Pak=
781-
github.com/yskopets/statik v0.1.8-0.20200501213002-c2d8dcc79889/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
782782
github.com/yuin/gopher-lua v0.0.0-20180316054350-84ea3a3c79b3/go.mod h1:aEV29XrmTYFr3CiRxZeGHpkvbwq+prZduBqMaascyCU=
783783
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43 h1:+lm10QQTNSBd8DVTNGHx7o/IKu9HYDvLMffDhbyLccI=
784784
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=

pkg/binary/envoytest/util.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/mholt/archiver"
25+
"github.com/tetratelabs/log"
2526

2627
"github.com/tetratelabs/getenvoy/pkg/binary"
2728
"github.com/tetratelabs/getenvoy/pkg/binary/envoy"
@@ -55,7 +56,11 @@ func Run(ctx context.Context, r binary.Runner, bootstrap string) error {
5556
if bootstrap != "" {
5657
args = append(args, "-c", bootstrap)
5758
}
58-
go r.Run(key, args)
59+
go func() {
60+
if err := r.Run(key, args); err != nil {
61+
log.Errorf("unable to run key %s: %v", key, err)
62+
}
63+
}()
5964
r.WaitWithContext(ctx, binary.StatusReady)
6065
return ctx.Err()
6166
}
@@ -65,7 +70,9 @@ func Run(ctx context.Context, r binary.Runner, bootstrap string) error {
6570
func Kill(ctx context.Context, r binary.Runner) error {
6671
r.SendSignal(syscall.SIGINT)
6772
r.WaitWithContext(ctx, binary.StatusTerminated)
68-
archiver.Unarchive(r.DebugStore()+".tar.gz", filepath.Dir(r.DebugStore()))
73+
if err := archiver.Unarchive(r.DebugStore()+".tar.gz", filepath.Dir(r.DebugStore())); err != nil {
74+
return fmt.Errorf("error killing context: %w", err)
75+
}
6976
return ctx.Err()
7077
}
7178

pkg/extension/example/init/registry/default.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@
1414

1515
package registry
1616

17-
//go:generate go run github.com/rakyll/statik -p=templates -m -ns=example/init/templates -src=../../../../../data/example/init/templates -a -include=* -f
18-
1917
import (
18+
"net/http"
2019
"path"
2120

22-
"github.com/rakyll/statik/fs"
23-
24-
// force execution of auto generated code
25-
_ "github.com/tetratelabs/getenvoy/pkg/extension/example/init/registry/templates"
21+
exampleTemplates "github.com/tetratelabs/getenvoy/data/example/init"
2622
"github.com/tetratelabs/getenvoy/pkg/extension/workspace/config/extension"
2723
)
2824

25+
var templatesFs = exampleTemplates.GetTemplates()
26+
2927
func newDefaultRegistry() registry {
30-
fileSystem, err := fs.NewWithNamespace("example/init/templates")
31-
if err != nil {
32-
// must be caught by unit tests
33-
panic(err)
34-
}
3528
return &fsRegistry{
36-
fs: fileSystem,
29+
fs: http.FS(templatesFs),
3730
namingScheme: func(category extension.Category, example string) string {
3831
return "/" + path.Join(category.String(), example)
3932
},

pkg/extension/init/init.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ func (s *scaffolder) walk(sourceDirName, destinationDirName string) (errs error)
101101
}
102102

103103
func (s *scaffolder) visit(sourceDirName, destinationDirName string, sourceFileInfo os.FileInfo) (errs error) {
104-
relOutputFileName := filepath.Join(destinationDirName, sourceFileInfo.Name())
104+
baseOutputFileName := sourceFileInfo.Name()
105+
// We rename go.mod to go.mod_ to workaround https://github.com/golang/go/issues/45197
106+
if baseOutputFileName == "go.mod_" {
107+
baseOutputFileName = "go.mod"
108+
}
109+
relOutputFileName := filepath.Join(destinationDirName, baseOutputFileName)
105110
outputFileName := filepath.Join(s.opts.OutputDir, relOutputFileName)
106111
if err := osutil.EnsureDirExists(filepath.Dir(outputFileName)); err != nil {
107112
return err

0 commit comments

Comments
 (0)