Skip to content

Commit

Permalink
chore: use go:embed instead of ldflags
Browse files Browse the repository at this point in the history
Generate separate file for each variable and assign them during go build using go:embed instead of using ldflags -X.

Resolves #5138

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Mar 30, 2022
1 parent a5d64fc commit b315ed9
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 24 deletions.
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ ENV PROTOTOOL_CACHE_PATH /.cache/prototool
ARG SOURCE_DATE_EPOCH
ENV SOURCE_DATE_EPOCH ${SOURCE_DATE_EPOCH}
WORKDIR /src
ARG NAME
ARG SHA
ARG USERNAME
ARG REGISTRY
ARG TAG
ARG ARTIFACTS
ARG PKGS
ARG EXTRAS
RUN mkdir -p pkg/machinery/gendata/data && \
echo -n ${NAME} > pkg/machinery/gendata/data/name && \
echo -n ${SHA} > pkg/machinery/gendata/data/sha && \
echo -n ${USERNAME} > pkg/machinery/gendata/data/username && \
echo -n ${REGISTRY} > pkg/machinery/gendata/data/registry && \
echo -n ${EXTRAS} > pkg/machinery/gendata/data/extras && \
echo -n ${PKGS} > pkg/machinery/gendata/data/pkgs && \
echo -n ${TAG} > pkg/machinery/gendata/data/tag && \
echo -n ${ARTIFACTS} > pkg/machinery/gendata/data/artifacts

# The build-go target creates a container to build Go code with Go modules downloaded and verified.

Expand Down Expand Up @@ -209,6 +226,11 @@ RUN --mount=type=cache,target=/.cache go generate ./...
RUN goimports -w -local github.com/talos-systems/talos ./
RUN gofumpt -w ./

FROM build AS fix_embed
ARG ABBREV_TAG
RUN echo -n "undefined" > pkg/machinery/gendata/data/sha && \
echo -n ${ABBREV_TAG} > pkg/machinery/gendata/data/tag

FROM --platform=${BUILDPLATFORM} scratch AS generate
COPY --from=proto-format-build /src/api /api/
COPY --from=generate-build /api/common/*.pb.go /pkg/machinery/api/common/
Expand All @@ -225,6 +247,7 @@ COPY --from=go-generate /src/pkg/machinery/resources/network/ /pkg/machinery/res
COPY --from=go-generate /src/pkg/machinery/config/types/v1alpha1/ /pkg/machinery/config/types/v1alpha1/
COPY --from=go-generate /src/pkg/machinery/nethelpers/ /pkg/machinery/nethelpers/
COPY --from=go-generate /src/pkg/machinery/extensions/ /pkg/machinery/extensions/
COPY --from=fix_embed /src/pkg/machinery/gendata/data /pkg/machinery/gendata/data

# The base target provides a container that can be used to build all Talos
# assets.
Expand All @@ -234,6 +257,7 @@ COPY ./cmd ./cmd
COPY ./pkg ./pkg
COPY ./internal ./internal
COPY --from=generate /pkg/machinery/ ./pkg/machinery/
COPY --from=build /src/pkg/machinery/gendata/data ./pkg/machinery/gendata/data
RUN --mount=type=cache,target=/.cache go list all >/dev/null
WORKDIR /src/pkg/machinery
RUN --mount=type=cache,target=/.cache go mod download
Expand Down
20 changes: 7 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ REGISTRY ?= ghcr.io
USERNAME ?= siderolabs
SHA ?= $(shell git describe --match=none --always --abbrev=8 --dirty)
TAG ?= $(shell git describe --tag --always --dirty --match v[0-9]\*)
ABBREV_TAG ?= $(shell git describe --tag --always --dirty --match v[0-9]\* --abbrev=0 )
TAG_SUFFIX ?=
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
IMAGE_REGISTRY ?= $(REGISTRY)
Expand Down Expand Up @@ -40,21 +41,9 @@ SHORT_INTEGRATION_TEST ?=
CUSTOM_CNI_URL ?=
INSTALLER_ARCH ?= all

VERSION_PKG = github.com/talos-systems/talos/pkg/version
IMAGES_PKGS = github.com/talos-systems/talos/pkg/images
MGMT_HELPERS_PKG = github.com/talos-systems/talos/cmd/talosctl/pkg/mgmt/helpers

CGO_ENABLED ?= 0
GO_BUILDFLAGS ?=
GO_LDFLAGS ?= \
-X $(VERSION_PKG).Name=$(NAME) \
-X $(VERSION_PKG).SHA=$(SHA) \
-X $(VERSION_PKG).Tag=$(TAG) \
-X $(VERSION_PKG).PkgsVersion=$(PKGS) \
-X $(VERSION_PKG).ExtrasVersion=$(EXTRAS) \
-X $(IMAGES_PKGS).Username=$(USERNAME) \
-X $(IMAGES_PKGS).Registry=$(REGISTRY) \
-X $(MGMT_HELPERS_PKG).ArtifactsPath=$(ARTIFACTS)
GO_LDFLAGS ?=

WITH_RACE ?= false
WITH_DEBUG ?= false
Expand Down Expand Up @@ -103,6 +92,11 @@ COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)"
COMMON_ARGS += --build-arg=http_proxy=$(http_proxy)
COMMON_ARGS += --build-arg=https_proxy=$(https_proxy)
COMMON_ARGS += --build-arg=NAME=$(NAME)
COMMON_ARGS += --build-arg=SHA=$(SHA)
COMMON_ARGS += --build-arg=USERNAME=$(USERNAME)
COMMON_ARGS += --build-arg=REGISTRY=$(REGISTRY)
COMMON_ARGS += --build-arg=ABBREV_TAG=$(ABBREV_TAG)

CI_ARGS ?=

Expand Down
8 changes: 6 additions & 2 deletions cmd/talosctl/pkg/mgmt/helpers/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

package helpers

import "path/filepath"
import (
"path/filepath"

"github.com/talos-systems/talos/pkg/machinery/gendata"
)

// ArtifactsPath is a path to artifacts output directory (set during the build).
var ArtifactsPath = "default/"
var ArtifactsPath = gendata.ArtifactsPath

// ArtifactPath returns path to the artifact by name.
func ArtifactPath(name string) string {
Expand Down
11 changes: 7 additions & 4 deletions pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
// Package images provides some default images.
package images

import "github.com/talos-systems/talos/pkg/version"
import (
"github.com/talos-systems/talos/pkg/machinery/gendata"
"github.com/talos-systems/talos/pkg/version"
)

var (
// Username the is the default registry username.
Username string
// Username is the default registry username.
Username = gendata.ImagesUsername

// Registry is the default registry.
Registry string
Registry = gendata.ImagesRegistry

// DefaultInstallerImageName is the default container image name for
// the installer.
Expand Down
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/artifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_out
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/extras
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.1.0-alpha.0
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Talos
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/pkgs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.1.0-alpha.0-10-gba98e20
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/registry
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ghcr.io
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/sha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
undefined
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/tag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.15.0-alpha.2
1 change: 1 addition & 0 deletions pkg/machinery/gendata/data/username
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
siderolabs
38 changes: 38 additions & 0 deletions pkg/machinery/gendata/gendata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package gendata contains that a variables generated from Makefile script. It's a proper alternative to using
// -ldflags '-X ...'.
package gendata

import (
_ "embed"
)

var (
// VersionName declares variable used by version package.
//go:embed data/name
VersionName string
// VersionTag declares variable used by version package.
//go:embed data/tag
VersionTag string
// VersionSHA declares variable used by version package.
//go:embed data/sha
VersionSHA string
// VersionPkgs declares variable used by version package.
//go:embed data/pkgs
VersionPkgs string
// VersionExtras declares variable used by version package.
//go:embed data/extras
VersionExtras string
// ImagesUsername declares variable used by images package.
//go:embed data/username
ImagesUsername string
// ImagesRegistry declares variable used by images package.
//go:embed data/registry
ImagesRegistry string
// ArtifactsPath declares variable used by helpers package.
//go:embed data/artifacts
ArtifactsPath string
)
12 changes: 7 additions & 5 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import (
"text/template"

machineapi "github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/gendata"
)

var (
// Name is set at build time.
Name string
Name = gendata.VersionName
// Tag is set at build time.
Tag string
Tag = gendata.VersionTag
// SHA is set at build time.
SHA string
SHA = gendata.VersionSHA
// Built is set at build time.
// TODO: its not.
Built string
// PkgsVersion is set at build time.
PkgsVersion string
PkgsVersion = gendata.VersionPkgs
// ExtrasVersion is set at build time.
ExtrasVersion string
ExtrasVersion = gendata.VersionExtras
)

const versionTemplate = ` Tag: {{ .Tag }}
Expand Down

0 comments on commit b315ed9

Please sign in to comment.