Skip to content

Commit

Permalink
Replace go-bindata with go:embed
Browse files Browse the repository at this point in the history
Use Go's built-in way of embedding static assets in binaries, instead
of relying on the somewhat legacy go-bindata tool. This allows the
embedding to be handled transparently by the Go compiler, eliminating
some extra Makefile steps.

Hide the path prefixes used to embed the data behind a sub-fs.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Aug 26, 2024
1 parent 10cdf08 commit 1166bb6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 53 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
aws_private.pem
out.json
.vscode
static/zz_generated_assets.go
.k0sbuild.docker-image.*
docs/cli
site
Expand Down
16 changes: 3 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,6 @@ pkg/client/clientset/.client-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/bo
&& mv -f -- "$$gendir/out/clientset" pkg/client/.
touch -- '$@'

codegen_targets += static/zz_generated_assets.go
static/zz_generated_assets.go: $(controller_gen_targets) # to generate the CRDs into static/_crds/*
static/zz_generated_assets.go: $(shell find static/manifests/calico static/manifests/windows -type f)
static/zz_generated_assets.go: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
CGO_ENABLED=0 $(GO) run github.com/kevinburke/go-bindata/go-bindata@v$(go-bindata_version) \
-o '$@' -pkg static -prefix static \
$(foreach gv,$(api_group_versions),static/_crds/$(dir $(gv))...) \
static/manifests/calico/... \
static/manifests/windows/...

ifeq ($(EMBEDDED_BINS_BUILDMODE),none)
BUILD_GO_TAGS += noembedbins
else
Expand All @@ -195,7 +185,7 @@ k0s: .k0sbuild.docker-image.k0s
k0s.exe: TARGET_OS = windows
k0s.exe: BUILD_GO_CGO_ENABLED = 0

k0s.exe k0s: go.sum $(codegen_targets) $(GO_SRCS) pkg/autopilot/checks/removed-gvks.yaml
k0s.exe k0s: go.sum $(codegen_targets) $(GO_SRCS) pkg/autopilot/checks/removed-gvks.yaml $(shell find static/_crds static/manifests/calico static/manifests/windows -type f)
rm -f -- '$@'
CGO_ENABLED=$(BUILD_GO_CGO_ENABLED) CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' GOOS=$(TARGET_OS) $(GO) build $(BUILD_GO_FLAGS) -ldflags='$(LD_FLAGS)' -o '$@' main.go
ifneq ($(EMBEDDED_BINS_BUILDMODE),none)
Expand All @@ -214,7 +204,7 @@ codegen: $(codegen_targets)

# bindata contains the parts of codegen which aren't version controlled.
.PHONY: bindata
bindata: static/zz_generated_assets.go
bindata:
ifneq ($(EMBEDDED_BINS_BUILDMODE),none)
bindata: pkg/assets/zz_generated_offsets_$(TARGET_OS).go
endif
Expand Down Expand Up @@ -287,7 +277,7 @@ clean-airgap-image-bundles:

.PHONY: clean
clean: clean-gocache clean-docker-image clean-airgap-image-bundles
-rm -f pkg/assets/zz_generated_offsets_*.go k0s k0s.exe .bins.*stamp bindata* static/zz_generated_assets.go
-rm -f pkg/assets/zz_generated_offsets_*.go k0s k0s.exe .bins.*stamp bindata*
-rm -rf $(K0S_GO_BUILD_CACHE)
-find pkg/apis -type f -name .controller-gen.stamp -delete
-rm pkg/client/clientset/.client-gen.stamp
Expand Down
2 changes: 1 addition & 1 deletion embedded-bins/kubernetes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG BUILDIMAGE
FROM $BUILDIMAGE AS build

ARG VERSION
RUN apk add build-base git go-bindata linux-headers rsync grep coreutils bash
RUN apk add build-base git linux-headers rsync grep coreutils bash

RUN mkdir -p $GOPATH/src/github.com/kubernetes/kubernetes
RUN git -c advice.detachedHead=false clone -b v$VERSION --depth=1 https://github.com/kubernetes/kubernetes.git $GOPATH/src/github.com/kubernetes/kubernetes
Expand Down
1 change: 0 additions & 1 deletion hack/tools/Makefile.variables
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
controller-gen_version = 0.15.0
go-bindata_version = 3.23.0+incompatible
golangci-lint_version = 1.57.1
21 changes: 13 additions & 8 deletions pkg/component/controller/calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"context"
"fmt"
"io/fs"
"path"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -104,17 +106,18 @@ func (c *Calico) dumpCRDs() error {
var emptyStruct struct{}

// Write the CRD definitions only at "boot", they do not change during runtime
crds, err := static.AssetDir("manifests/calico/CustomResourceDefinition")
crds, err := fs.ReadDir(static.CalicoManifests, "CustomResourceDefinition")
if err != nil {
return err
}

for _, filename := range crds {
for _, entry := range crds {
filename := entry.Name()
manifestName := fmt.Sprintf("calico-crd-%s", filename)

output := bytes.NewBuffer([]byte{})

contents, err := static.Asset(fmt.Sprintf("manifests/calico/CustomResourceDefinition/%s", filename))
contents, err := fs.ReadFile(static.CalicoManifests, path.Join("CustomResourceDefinition", filename))
if err != nil {
return fmt.Errorf("failed to fetch crd %s: %w", filename, err)
}
Expand All @@ -137,17 +140,18 @@ func (c *Calico) dumpCRDs() error {
}

func (c *Calico) processConfigChanges(newConfig calicoConfig) error {
manifestDirectories, err := static.AssetDir("manifests/calico")
manifestDirectories, err := fs.ReadDir(static.CalicoManifests, ".")
if err != nil {
return fmt.Errorf("error retrieving calico manifests: %w, will retry", err)
}

for _, dir := range manifestDirectories {
for _, entry := range manifestDirectories {
dir := entry.Name()
// CRDs are handled separately on boot
if dir == "CustomResourceDefinition" {
continue
}
manifestPaths, err := static.AssetDir(fmt.Sprintf("manifests/calico/%s", dir))
manifestPaths, err := fs.ReadDir(static.CalicoManifests, dir)
if err != nil {
return fmt.Errorf("error retrieving calico manifests: %w, will retry", err)
}
Expand All @@ -158,10 +162,11 @@ func (c *Calico) processConfigChanges(newConfig calicoConfig) error {
}
}

for _, filename := range manifestPaths {
for _, entry := range manifestPaths {
filename := entry.Name()
manifestName := fmt.Sprintf("calico-%s-%s", dir, filename)
output := bytes.NewBuffer([]byte{})
contents, err := static.Asset(fmt.Sprintf("manifests/calico/%s/%s", dir, filename))
contents, err := fs.ReadFile(static.CalicoManifests, path.Join(dir, filename))
if err != nil {
return fmt.Errorf("can't find manifest %s: %w", manifestName, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/controller/calico_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestCalicoManifests(t *testing.T) {
calico := NewCalico(k0sVars, crdSaver, saver)
cfg, err := calico.getConfig(clusterConfig)
require.NoError(t, err)
_ = calico.processConfigChanges(cfg)
require.NoError(t, calico.processConfigChanges(cfg))

daemonSetManifestRaw, foundRaw := saver["calico-DaemonSet-calico-node.yaml"]
require.True(t, foundRaw, "must have daemon set for calico")
Expand Down
11 changes: 6 additions & 5 deletions pkg/component/controller/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"fmt"
"io/fs"
"path"

"github.com/k0sproject/k0s/pkg/component/manager"
Expand Down Expand Up @@ -69,16 +70,16 @@ func (c CRD) Init(_ context.Context) error {
}

// Run unpacks manifests from bindata
func (c CRD) Start(_ context.Context) error {
crdAssetsPath := path.Join("_crds", c.assetsDir)
crds, err := static.AssetDir(crdAssetsPath)
func (c CRD) Start(context.Context) error {
crds, err := fs.ReadDir(static.CRDs, c.assetsDir)
if err != nil {
return fmt.Errorf("can't unbundle CRD `%s` manifests: %w", c.bundle, err)
}

for _, filename := range crds {
for _, entry := range crds {
filename := entry.Name()
manifestName := fmt.Sprintf("%s-crd-%s", c.bundle, filename)
content, err := static.Asset(path.Join(crdAssetsPath, filename))
content, err := fs.ReadFile(static.CRDs, path.Join(c.assetsDir, filename))
if err != nil {
return fmt.Errorf("failed to fetch crd `%s`: %w", filename, err)
}
Expand Down
14 changes: 9 additions & 5 deletions pkg/component/controller/windowsstackcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"context"
"fmt"
"io/fs"
"path"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -173,12 +175,13 @@ func (n *WindowsStackComponent) Stop() error {
// createWindowsStack creates windows stack

func (n *WindowsStackComponent) createWindowsStack(newConfig windowsStackRenderingContext) error {
manifestDirectories, err := static.AssetDir("manifests/windows")
manifestDirectories, err := fs.ReadDir(static.WindowsManifests, ".")
if err != nil {
return fmt.Errorf("error retrieving manifests: %w", err)
}
for _, dir := range manifestDirectories {
manifestPaths, err := static.AssetDir(fmt.Sprintf("manifests/windows/%s", dir))
for _, entry := range manifestDirectories {
dir := entry.Name()
manifestPaths, err := fs.ReadDir(static.WindowsManifests, dir)
if err != nil {
return fmt.Errorf("error retrieving manifests: %w, will retry", err)
}
Expand All @@ -189,11 +192,12 @@ func (n *WindowsStackComponent) createWindowsStack(newConfig windowsStackRenderi
}
}

for _, filename := range manifestPaths {
for _, entry := range manifestPaths {
filename := entry.Name()
manifestName := fmt.Sprintf("windows-%s-%s", dir, filename)
output := bytes.NewBuffer([]byte{})
n.log.Debugf("Reading manifest template %s", manifestName)
contents, err := static.Asset(fmt.Sprintf("manifests/windows/%s/%s", dir, filename))
contents, err := fs.ReadFile(static.WindowsManifests, path.Join(dir, filename))
if err != nil {
return fmt.Errorf("can't unpack manifest %s: %w", manifestName, err)
}
Expand Down
49 changes: 49 additions & 0 deletions static/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2024 k0s authors
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 static

import (
"embed"
"io/fs"
"path"
)

var (
//go:embed _crds
crds embed.FS
CRDs fs.FS = subFS(crds, "_crds")
)

var (
//go:embed manifests/calico
calicoManifests embed.FS
CalicoManifests fs.FS = subFS(calicoManifests, "manifests", "calico")
)

var (
//go:embed manifests/windows
windowsManifests embed.FS
WindowsManifests fs.FS = subFS(windowsManifests, "manifests", "windows")
)

func subFS(f fs.FS, segments ...string) fs.FS {
f, err := fs.Sub(f, path.Join(segments...))
if err != nil {
panic(err)
}
return f
}
18 changes: 0 additions & 18 deletions static/doc.go

This file was deleted.

0 comments on commit 1166bb6

Please sign in to comment.