refactor: replace go-bindata with native embed package

Signed-off-by: muicoder <muicoder@gmail.com>
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
muicoder
2025-07-29 15:50:35 +08:00
committed by Brad Davidson
parent 6e7df3ad1b
commit 8fff7f573b
16 changed files with 77 additions and 875 deletions

4
.gitignore vendored
View File

@@ -22,7 +22,9 @@
/image/main.squashfs
/package/k3s
/package/data-*
/pkg/data/zz_generated_bindata.go
/pkg/deploy/embed/
/pkg/static/embed/
/pkg/data/embed/
__pycache__
/tests/.pytest_cache/
/tests/.tox/

1
go.mod
View File

@@ -94,7 +94,6 @@ require (
github.com/erikdubbelboer/gspt v0.0.0-20190125194910-e68493906b83
github.com/flannel-io/flannel v0.27.0
github.com/fsnotify/fsnotify v1.7.0
github.com/go-bindata/go-bindata v3.1.2+incompatible
github.com/go-logr/logr v1.4.2
github.com/go-logr/stdr v1.2.3-0.20220714215716-96bad1d688c5
github.com/go-test/deep v1.0.7

2
go.sum
View File

@@ -487,8 +487,6 @@ github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj2
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=

View File

@@ -1,7 +1,3 @@
//go:generate go run pkg/codegen/main.go
//go:generate go fmt pkg/deploy/zz_generated_bindata.go
//go:generate go fmt pkg/static/zz_generated_bindata.go
package main
import (

View File

@@ -1,62 +0,0 @@
package main
import (
"os"
bindata "github.com/go-bindata/go-bindata"
"github.com/sirupsen/logrus"
)
func main() {
os.Unsetenv("GOPATH")
bc := &bindata.Config{
Input: []bindata.InputConfig{
{
Path: "build/data",
Recursive: true,
},
},
Package: "data",
NoCompress: true,
NoMemCopy: true,
NoMetadata: true,
Output: "pkg/data/zz_generated_bindata.go",
}
if err := bindata.Translate(bc); err != nil {
logrus.Fatal(err)
}
bc = &bindata.Config{
Input: []bindata.InputConfig{
{
Path: "manifests",
Recursive: true,
},
},
Package: "deploy",
NoMetadata: true,
Prefix: "manifests/",
Output: "pkg/deploy/zz_generated_bindata.go",
Tags: "!no_stage",
}
if err := bindata.Translate(bc); err != nil {
logrus.Fatal(err)
}
bc = &bindata.Config{
Input: []bindata.InputConfig{
{
Path: "build/static",
Recursive: true,
},
},
Package: "static",
NoMetadata: true,
Prefix: "build/static/",
Output: "pkg/static/zz_generated_bindata.go",
Tags: "!no_stage",
}
if err := bindata.Translate(bc); err != nil {
logrus.Fatal(err)
}
}

View File

@@ -1 +1,17 @@
package data
import (
"embed"
"github.com/k3s-io/k3s/pkg/util/bindata"
)
//go:embed embed/*
var embedFS embed.FS
var (
bd = bindata.Bindata{FS: &embedFS, Prefix: "embed"}
Asset = bd.Asset
AssetNames = bd.AssetNames
)

0
pkg/data/embed/.empty Normal file
View File

0
pkg/deploy/embed/.empty Normal file
View File

View File

@@ -1,20 +1,28 @@
//go:build !no_stage
// +build !no_stage
package deploy
import (
"bytes"
"embed"
"os"
"path/filepath"
"strings"
"github.com/k3s-io/k3s/pkg/util/bindata"
pkgerrors "github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
//go:embed embed/*
var embedFS embed.FS
var bd = bindata.Bindata{FS: &embedFS, Prefix: "embed"}
func Stage(dataDir string, templateVars map[string]string, skips map[string]bool) error {
staging:
for _, name := range AssetNames() {
for _, name := range bd.AssetNames() {
nameNoExtension := strings.TrimSuffix(name, filepath.Ext(name))
if skips[name] || skips[nameNoExtension] {
continue staging
@@ -27,7 +35,7 @@ staging:
}
}
content, err := Asset(name)
content, err := bd.Asset(name)
if err != nil {
return err
}

File diff suppressed because one or more lines are too long

0
pkg/static/embed/.empty Normal file
View File

View File

@@ -1,18 +1,26 @@
//go:build !no_stage
// +build !no_stage
package static
import (
"embed"
"os"
"path/filepath"
"github.com/k3s-io/k3s/pkg/util/bindata"
pkgerrors "github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
//go:embed embed/*
var embedFS embed.FS
var bd = bindata.Bindata{FS: &embedFS, Prefix: "embed"}
func Stage(dataDir string) error {
for _, name := range AssetNames() {
content, err := Asset(name)
for _, name := range bd.AssetNames() {
content, err := bd.Asset(name)
if err != nil {
return err
}

File diff suppressed because one or more lines are too long

35
pkg/util/bindata/embed.go Normal file
View File

@@ -0,0 +1,35 @@
package bindata
import (
"embed"
"io/fs"
"path/filepath"
"sort"
"strings"
)
// Bindata is a wrapper around embed.FS that allows us to continue to use
// go-bindata style Asset and AssetNames functions to access the embedded FS.
type Bindata struct {
FS *embed.FS
Prefix string
}
func (b Bindata) Asset(name string) ([]byte, error) {
return b.FS.ReadFile(filepath.Join(b.Prefix, name))
}
func (b Bindata) AssetNames() []string {
var assets []string
fs.WalkDir(b.FS, ".", func(path string, entry fs.DirEntry, err error) error {
// do not list hidden files - there is a .empty file checked in as a
// placeholder for files that are generated at build time to satisy
// `go vet`, but these should not be include when listing assets.
if n := entry.Name(); entry.Type().IsRegular() && !strings.HasPrefix(n, ".") && !strings.HasPrefix(n, "_") {
assets = append(assets, strings.TrimPrefix(path, b.Prefix))
}
return nil
})
sort.Strings(assets)
return assets
}

View File

@@ -172,6 +172,8 @@ if [ ! -x ${INSTALLBIN}/cni${BINARY_POSTFIX} ]; then
fi
echo Building k3s
cp -av manifests/* pkg/deploy/embed/
cp -av build/static/* pkg/static/embed/
CGO_CFLAGS="-DSQLITE_ENABLE_DBSTAT_VTAB=1 -DSQLITE_USE_ALLOCA=1" \
CGO_ENABLED=1 "${GO}" build $BLDFLAGS -tags "$TAGS" -buildvcs=false -gcflags="all=${GCFLAGS}" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/k3s${BINARY_POSTFIX} ./cmd/server

View File

@@ -79,6 +79,7 @@ LDFLAGS="
"
TAGS="urfave_cli_no_docs"
STATIC="-extldflags '-static'"
cp -av build/data/* pkg/data/embed/
CGO_ENABLED=0 "${GO}" build -tags "$TAGS" -buildvcs=false -ldflags "$LDFLAGS $STATIC" -o ${CMD_NAME} ./cmd/k3s
stat ${CMD_NAME}