Skip to content

Commit 3320999

Browse files
committed
refactor: replace go-bindata with native embed package
Signed-off-by: muicoder <[email protected]>
1 parent 6e7df3a commit 3320999

File tree

13 files changed

+72
-876
lines changed

13 files changed

+72
-876
lines changed

cmd/k3s/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func stageAndRun(dataDir, cmd string, args []string, calledAsInternal bool) erro
250250
// getAssetAndDir returns the name of the bindata asset, along with a directory path
251251
// derived from the data-dir and bindata asset name.
252252
func getAssetAndDir(dataDir string) (string, string) {
253-
asset := data.AssetNames()[0]
253+
asset := data.AssetNames(data.FS)[0]
254254
dir := filepath.Join(dataDir, "data", strings.SplitN(filepath.Base(asset), ".", 2)[0])
255255
return asset, dir
256256
}
@@ -291,7 +291,7 @@ func extract(dataDir string) (string, error) {
291291

292292
logrus.Infof("Preparing data dir %s", dir)
293293

294-
content, err := data.Asset(asset)
294+
content, err := data.Asset(data.FS, asset)
295295
if err != nil {
296296
return "", err
297297
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ require (
9494
github.com/erikdubbelboer/gspt v0.0.0-20190125194910-e68493906b83
9595
github.com/flannel-io/flannel v0.27.0
9696
github.com/fsnotify/fsnotify v1.7.0
97-
github.com/go-bindata/go-bindata v3.1.2+incompatible
9897
github.com/go-logr/logr v1.4.2
9998
github.com/go-logr/stdr v1.2.3-0.20220714215716-96bad1d688c5
10099
github.com/go-test/deep v1.0.7

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,6 @@ github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj2
487487
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
488488
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
489489
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
490-
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
491-
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
492490
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
493491
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
494492
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=

main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//go:generate go run pkg/codegen/main.go
2-
//go:generate go fmt pkg/deploy/zz_generated_bindata.go
3-
//go:generate go fmt pkg/static/zz_generated_bindata.go
4-
51
package main
62

73
import (

pkg/codegen/main.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/data/data.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
package data
2+
3+
import (
4+
"embed"
5+
"io/fs"
6+
"sort"
7+
)
8+
9+
//go:embed embed/*
10+
var FS embed.FS
11+
12+
func Asset(embedFS embed.FS, name string) ([]byte, error) {
13+
return embedFS.ReadFile("embed/" + name)
14+
}
15+
16+
func AssetNames(embedFS embed.FS) []string {
17+
var assets []string
18+
fs.WalkDir(embedFS, ".", func(path string, entry fs.DirEntry, err error) error {
19+
if entry.Type().IsRegular() {
20+
assets = append(assets, path[6:])
21+
}
22+
return nil
23+
})
24+
sort.Strings(assets)
25+
return assets
26+
}

pkg/deploy/stage.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
//go:build !no_stage
2+
// +build !no_stage
23

34
package deploy
45

56
import (
67
"bytes"
8+
"embed"
79
"os"
810
"path/filepath"
911
"strings"
1012

13+
"github.com/k3s-io/k3s/pkg/util"
1114
pkgerrors "github.com/pkg/errors"
1215
"github.com/sirupsen/logrus"
1316
)
1417

18+
//go:embed embed/*
19+
var embedFS embed.FS
20+
1521
func Stage(dataDir string, templateVars map[string]string, skips map[string]bool) error {
1622
staging:
17-
for _, name := range AssetNames() {
23+
for _, name := range util.AssetNames(embedFS) {
1824
nameNoExtension := strings.TrimSuffix(name, filepath.Ext(name))
1925
if skips[name] || skips[nameNoExtension] {
2026
continue staging
@@ -27,7 +33,7 @@ staging:
2733
}
2834
}
2935

30-
content, err := Asset(name)
36+
content, err := util.Asset(embedFS, name)
3137
if err != nil {
3238
return err
3339
}

pkg/deploy/zz_generated_bindata.go

Lines changed: 0 additions & 527 deletions
This file was deleted.

pkg/static/stage.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
//go:build !no_stage
2+
// +build !no_stage
23

34
package static
45

56
import (
7+
"embed"
68
"os"
79
"path/filepath"
810

11+
"github.com/k3s-io/k3s/pkg/util"
912
pkgerrors "github.com/pkg/errors"
1013
"github.com/sirupsen/logrus"
1114
)
1215

16+
//go:embed embed/*
17+
var embedFS embed.FS
18+
1319
func Stage(dataDir string) error {
14-
for _, name := range AssetNames() {
15-
content, err := Asset(name)
20+
for _, name := range util.AssetNames(embedFS) {
21+
content, err := util.Asset(embedFS, name)
1622
if err != nil {
1723
return err
1824
}

pkg/static/zz_generated_bindata.go

Lines changed: 0 additions & 274 deletions
This file was deleted.

0 commit comments

Comments
 (0)