From 4aee2b8358a177db90c2561eeb913c80bfccec03 Mon Sep 17 00:00:00 2001 From: Henrique Matulis Date: Thu, 11 Dec 2025 04:42:20 -0300 Subject: [PATCH] build(frontend): make embedded assets the default behavior Inverts the build tag logic to ensure that standard `go build` commands produce self-contained binaries with embedded assets. The dev workflow allowing for live updates must be explicitly specified now. --- Makefile | 2 +- cmd/frontend/Dockerfile | 2 -- pkg/ui/.gitignore | 3 +-- pkg/ui/assets_embed.go | 4 ++-- pkg/ui/assets_local.go | 3 ++- .../base/web/ui/embed.go.tmpl => pkg/ui/embed.go | 8 ++++++-- third_party/prometheus_ui/base/scripts/compress_assets.sh | 6 +++--- third_party/prometheus_ui/base/web/ui/README.md | 4 +--- 8 files changed, 16 insertions(+), 16 deletions(-) rename third_party/prometheus_ui/base/web/ui/embed.go.tmpl => pkg/ui/embed.go (82%) diff --git a/Makefile b/Makefile index 1ad924ee7a..8ce3aa6497 100644 --- a/Makefile +++ b/Makefile @@ -113,7 +113,7 @@ bin-go: @echo ">> building binaries" ifeq ($(NO_DOCKER), 1) if [ "$(BIN_GO_NAME)" = "frontend" ]; then pkg/ui/build.sh; fi - CGO_ENABLED=0 go build -tags builtinassets -o ./build/bin/$(BIN_GO_NAME) ./$(BIN_GO_DIR)/$(BIN_GO_NAME)/*.go + CGO_ENABLED=0 go build -o ./build/bin/$(BIN_GO_NAME) ./$(BIN_GO_DIR)/$(BIN_GO_NAME)/*.go # If pushing, build and tag native arch image to GCR. else ifeq ($(DOCKER_PUSH), 1) $(call docker_build, --tag gmp/$(BIN_GO_NAME) -f ./$(BIN_GO_DIR)/$(BIN_GO_NAME)/Dockerfile .) diff --git a/cmd/frontend/Dockerfile b/cmd/frontend/Dockerfile index 35570293a4..74f12658b3 100644 --- a/cmd/frontend/Dockerfile +++ b/cmd/frontend/Dockerfile @@ -50,13 +50,11 @@ ENV GOOS=${TARGETOS} ENV GOARCH=${TARGETARCH} RUN if [ "${TARGETARCH}" = "arm64" ] && [ "${BUILDARCH}" != "arm64" ]; then \ apt install -y --no-install-recommends \ - -tags builtinassets \ gcc-aarch64-linux-gnu libc6-dev-arm64-cross; \ CC=aarch64-linux-gnu-gcc; \ fi && \ GOOS=${TARGETOS} GOARCH=${TARGETARCH} CC=${CC} \ go build \ - -tags builtinassets \ -ldflags="-X github.com/prometheus/common/version.Version=$(cat charts/values.global.yaml | go tool yq ".version" ) \ -X github.com/prometheus/common/version.BuildDate=$(date --iso-8601=seconds)" \ -o frontend \ diff --git a/pkg/ui/.gitignore b/pkg/ui/.gitignore index 1e9b5b624c..6b9f4b3638 100644 --- a/pkg/ui/.gitignore +++ b/pkg/ui/.gitignore @@ -1,3 +1,2 @@ /build -/static -/embed.go \ No newline at end of file +/static \ No newline at end of file diff --git a/pkg/ui/assets_embed.go b/pkg/ui/assets_embed.go index 99a1c9ebce..12430620d9 100644 --- a/pkg/ui/assets_embed.go +++ b/pkg/ui/assets_embed.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build builtinassets -// +build builtinassets +//go:build !localassets +// +build !localassets package ui diff --git a/pkg/ui/assets_local.go b/pkg/ui/assets_local.go index 9ece033688..7a6ab3cb88 100644 --- a/pkg/ui/assets_local.go +++ b/pkg/ui/assets_local.go @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !builtinassets +//go:build localassets +// +build localassets package ui diff --git a/third_party/prometheus_ui/base/web/ui/embed.go.tmpl b/pkg/ui/embed.go similarity index 82% rename from third_party/prometheus_ui/base/web/ui/embed.go.tmpl rename to pkg/ui/embed.go index 1d7dbf5958..ce2f463a9c 100644 --- a/third_party/prometheus_ui/base/web/ui/embed.go.tmpl +++ b/pkg/ui/embed.go @@ -11,10 +11,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build builtinassets -// +build builtinassets +//go:build !localassets +// +build !localassets package ui import "embed" +// Following line replaced by compress_assets.sh +//{{go:embed}} + +var EmbedFS embed.FS diff --git a/third_party/prometheus_ui/base/scripts/compress_assets.sh b/third_party/prometheus_ui/base/scripts/compress_assets.sh index 615e30c43c..859511d6f1 100755 --- a/third_party/prometheus_ui/base/scripts/compress_assets.sh +++ b/third_party/prometheus_ui/base/scripts/compress_assets.sh @@ -5,12 +5,12 @@ set -euo pipefail cd web/ui -cp embed.go.tmpl embed.go GZIP_OPTS="-fk" # gzip option '-k' may not always exist in the latest gzip available on different distros. if ! gzip -k -h &>/dev/null; then GZIP_OPTS="-f"; fi find static -type f -name '*.gz' -delete -find static -type f -exec gzip $GZIP_OPTS '{}' \; -print0 | xargs -0 -I % echo %.gz | xargs echo //go:embed >> embed.go -echo var EmbedFS embed.FS >> embed.go +find static -type f -exec gzip $GZIP_OPTS '{}' \; +FILES=$(find static -name "*.gz" -type f | tr '\n' ' ') +sed -i "s|// {{go:embed}}|//go:embed $FILES|" embed.go \ No newline at end of file diff --git a/third_party/prometheus_ui/base/web/ui/README.md b/third_party/prometheus_ui/base/web/ui/README.md index b954228676..23b8924a77 100644 --- a/third_party/prometheus_ui/base/web/ui/README.md +++ b/third_party/prometheus_ui/base/web/ui/README.md @@ -5,9 +5,7 @@ into the Prometheus binary using the embed package. During development it is more convenient to always use the files on disk to directly see changes without recompiling. -To make this work, remove the `builtinassets` build tag in the `flags` entry -in `.promu.yml`, and then `make build` (or build Prometheus using -`go build ./cmd/prometheus`). +To make this work, add the `localassets` build tag in the `go build` command in `make` that builds the frontend (add `-tags localassets`) and then `make build` as usual. This will serve all files from your local filesystem. This is for development purposes only.