Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit 79f13f7

Browse files
authored
Merge pull request #936 from kinvolk/invidian/simplify-assets-handling
pkg/assets: cleanup exported API
2 parents 63d6d78 + 78a0d6b commit 79f13f7

File tree

6 files changed

+57
-88
lines changed

6 files changed

+57
-88
lines changed

pkg/assets/assets.go

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ package assets
1919
import (
2020
"fmt"
2121
"io"
22-
"net/http"
2322
"os"
24-
"time"
2523

26-
"github.com/prometheus/alertmanager/pkg/modtimevfs"
27-
"github.com/shurcooL/httpfs/union"
28-
"github.com/shurcooL/vfsgen"
24+
// Currently pkg/assets/assets_generate.go file is ignored from regular
25+
// builds and it's only used when running 'go generate'.
26+
//
27+
// Because of that, external dependencies of assets_generate.go are not
28+
// normally added to go.mod and go.sum files, unless you run 'go generate'.
29+
// But then 'go mod tidy' removes them again.
30+
//
31+
// Those anonymous imports ensure, that those external dependencies
32+
// will always be pulled into go.mod file.
33+
//
34+
// The alternative would be to refactor the function to actually use the
35+
// dependencies here, but then we have an exported function, which
36+
// shouldn't really be exported.
37+
//
38+
// Also see https://github.com/golang/go/issues/25922 and https://github.com/golang/go/issues/29516
39+
// for more details about this issue.
40+
_ "github.com/prometheus/alertmanager/pkg/modtimevfs"
41+
_ "github.com/shurcooL/httpfs/union"
42+
_ "github.com/shurcooL/vfsgen"
2943
)
3044

3145
const (
@@ -37,46 +51,24 @@ const (
3751
TerraformModulesSource = "/terraform-modules"
3852
)
3953

40-
type WalkFunc func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error
54+
type walkFunc func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error
4155

42-
type AssetsIface interface {
56+
type assetsIface interface {
4357
// WalkFiles calls cb for every regular file within path.
4458
//
4559
// Usually, fileName passed to the cb will be relative to
4660
// path. But in case of error, it is possible that it will
4761
// not.be relative. Also, in case of error, fileInfo or r may
4862
// be nil.
49-
WalkFiles(path string, cb WalkFunc) error
63+
WalkFiles(path string, cb walkFunc) error
5064
}
5165

52-
var Assets AssetsIface
53-
54-
func init() {
55-
Assets = newEmbeddedAssets()
66+
func get() assetsIface {
5667
if value, found := os.LookupEnv("LOKOCTL_USE_FS_ASSETS"); found {
57-
Assets = newFsAssets(value)
68+
return newFsAssets(value)
5869
}
59-
}
6070

61-
// Generate function wraps vfsgen.Generate function.
62-
// Additionally to vfsgen.Generate, it also takes map of directories,
63-
// where key represents path in the assets and a value represents path
64-
// to the assets directory in local filesystem (which should be relative).
65-
//
66-
// This function also resets modification time for every file, so creating a new copy
67-
// of code does not trigger changes in all asset files.
68-
func Generate(fileName string, packageName string, variableName string, dirs map[string]string) error {
69-
ufs := make(map[string]http.FileSystem)
70-
for assetsPath, fsPath := range dirs {
71-
ufs[assetsPath] = http.Dir(fsPath)
72-
}
73-
u := union.New(ufs)
74-
fs := modtimevfs.New(u, time.Unix(1, 0))
75-
return vfsgen.Generate(fs, vfsgen.Options{
76-
Filename: fileName,
77-
PackageName: packageName,
78-
VariableName: variableName,
79-
})
71+
return newEmbeddedAssets()
8072
}
8173

8274
// Extract recursively extracts the assets at src into the directory dst. If dst doesn't exist, the
@@ -85,8 +77,8 @@ func Generate(fileName string, packageName string, variableName string, dirs map
8577
// The assets are read either from data embedded in the binary or from the filesystem, depending on
8678
// whether the LOKOCTL_USE_FS_ASSETS environment variable is set.
8779
func Extract(src, dst string) error {
88-
walk := CopyingWalker(dst, 0755)
89-
if err := Assets.WalkFiles(src, walk); err != nil {
80+
walk := copyingWalker(dst, 0700)
81+
if err := get().WalkFiles(src, walk); err != nil {
9082
return fmt.Errorf("failed to walk assets: %v", err)
9183
}
9284

pkg/assets/assets_generate.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,36 @@
1818
package main
1919

2020
import (
21-
"log"
21+
"net/http"
22+
"time"
23+
24+
// External dependencies should also be added to assets.go file.
25+
"github.com/prometheus/alertmanager/pkg/modtimevfs"
26+
"github.com/shurcooL/httpfs/union"
27+
"github.com/shurcooL/vfsgen"
2228

2329
"github.com/kinvolk/lokomotive/pkg/assets"
2430
)
2531

2632
func main() {
27-
dirs := map[string]string{
28-
assets.TerraformModulesSource: "../../assets/terraform-modules",
29-
assets.ControlPlaneSource: "../../assets/charts/control-plane",
30-
assets.ComponentsSource: "../../assets/charts/components",
33+
directoriesToEmbed := map[string]http.FileSystem{
34+
assets.TerraformModulesSource: http.Dir("../../assets/terraform-modules"),
35+
assets.ControlPlaneSource: http.Dir("../../assets/charts/control-plane"),
36+
assets.ComponentsSource: http.Dir("../../assets/charts/components"),
3137
}
32-
err := assets.Generate("generated_assets.go", "assets", "vfsgenAssets", dirs)
33-
if err != nil {
34-
log.Fatalln(err)
38+
39+
// Reset modification time on files, so after cloning the repository all
40+
// assets don't get modified.
41+
u := union.New(directoriesToEmbed)
42+
fs := modtimevfs.New(u, time.Unix(1, 0))
43+
44+
options := vfsgen.Options{
45+
Filename: "generated_assets.go",
46+
PackageName: "assets",
47+
VariableName: "vfsgenAssets",
48+
}
49+
50+
if err := vfsgen.Generate(fs, options); err != nil {
51+
panic(err)
3552
}
3653
}

pkg/assets/embedded.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ type embeddedAssets struct {
2929
fs http.FileSystem
3030
}
3131

32-
var _ AssetsIface = &embeddedAssets{}
33-
3432
func newEmbeddedAssets() *embeddedAssets {
3533
return &embeddedAssets{
3634
fs: vfsgenAssets,
3735
}
3836
}
3937

40-
func (a *embeddedAssets) WalkFiles(location string, cb WalkFunc) error {
38+
func (a *embeddedAssets) WalkFiles(location string, cb walkFunc) error {
4139
return vfsutil.WalkFiles(a.fs, location, func(filePath string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
4240
if err != nil {
4341
return cb(filePath, fileInfo, r, err)

pkg/assets/fs.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ type fsAssets struct {
2626
assetsDir string
2727
}
2828

29-
var _ AssetsIface = &fsAssets{}
30-
3129
func newFsAssets(dir string) *fsAssets {
3230
if dir == "" {
3331
execDir, err := osext.ExecutableFolder()
@@ -41,7 +39,7 @@ func newFsAssets(dir string) *fsAssets {
4139
}
4240
}
4341

44-
func (a *fsAssets) WalkFiles(location string, cb WalkFunc) error {
42+
func (a *fsAssets) WalkFiles(location string, cb walkFunc) error {
4543
relativeLocation := strings.TrimLeft(location, string(os.PathSeparator))
4644
assetsLocation := filepath.Join(a.assetsDir, relativeLocation)
4745
return filepath.Walk(assetsLocation, func(path string, info os.FileInfo, err error) error {

pkg/assets/walkers.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ package assets
1717
import (
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
)
2423

25-
// CopyingWalker returns a WalkFunc which writes the given file to disk.
26-
func CopyingWalker(path string, newDirPerms os.FileMode) WalkFunc {
24+
// copyingWalker returns a WalkFunc which writes the given file to disk.
25+
func copyingWalker(path string, newDirPerms os.FileMode) walkFunc {
2726
return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
2827
if err != nil {
2928
return fmt.Errorf("error while walking at %q: %w", fileName, err)
@@ -39,39 +38,6 @@ func CopyingWalker(path string, newDirPerms os.FileMode) WalkFunc {
3938
}
4039
}
4140

42-
// DumpingWalker returns a WalkFunc which sets the contents of the given file in a map.
43-
func DumpingWalker(contentsMap map[string]string, allowedExts ...string) WalkFunc {
44-
var extsMap map[string]struct{}
45-
46-
if len(allowedExts) > 0 {
47-
extsMap = make(map[string]struct{}, len(allowedExts))
48-
for _, ext := range allowedExts {
49-
extsMap[ext] = struct{}{}
50-
}
51-
}
52-
53-
return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
54-
if err != nil {
55-
return fmt.Errorf("error while walking at %q: %w", fileName, err)
56-
}
57-
58-
if extsMap != nil {
59-
if _, ok := extsMap[filepath.Ext(fileName)]; !ok {
60-
return nil
61-
}
62-
}
63-
64-
contents, err := ioutil.ReadAll(r)
65-
if err != nil {
66-
return fmt.Errorf("failed to read %q: %w", fileName, err)
67-
}
68-
69-
contentsMap[fileName] = string(contents)
70-
71-
return nil
72-
}
73-
}
74-
7541
// writeFile writes data from given io.Reader to the file and makes sure, that
7642
// this is the only content stored in the file.
7743
func writeFile(p string, r io.Reader) error {

pkg/helm/charts.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ func ChartFromAssets(location string) (*chart.Chart, error) {
4646
// isn't trivial since we call os.RemoveAll() in a defer statement.
4747
defer os.RemoveAll(tmpDir) //nolint: errcheck
4848

49-
// Rendered files could contain secrets - allow r/w access to owner only.
50-
walk := assets.CopyingWalker(tmpDir, 0700)
51-
if err := assets.Assets.WalkFiles(location, walk); err != nil {
49+
if err := assets.Extract(location, tmpDir); err != nil {
5250
return nil, fmt.Errorf("traversing assets: %w", err)
5351
}
5452

0 commit comments

Comments
 (0)