Skip to content

Commit

Permalink
refactor: generate /etc/os-release file static way
Browse files Browse the repository at this point in the history
The file which is exported back to source via `make generate` is using
short tag (vX.Y.Z), while the one generated for the actual build comes
with full version tag.

Fixes #8898

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Dec 27, 2024
1 parent e79c9e1 commit b7a7fdc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 101 deletions.
84 changes: 45 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ ARG PKG_TALOSCTL_CNI_BUNDLE_INSTALL=scratch

ARG DEBUG_TOOLS_SOURCE=scratch

ARG EMBED_TARGET=embed

# Resolve package images using ${PKGS} to be used later in COPY --from=.

FROM ${PKG_FHS} AS pkg-fhs
Expand Down Expand Up @@ -285,6 +287,43 @@ RUN --mount=type=cache,target=/.cache go mod verify

# The generate target generates code from protobuf service definitions and machinery config.

FROM build AS embed-generate
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

FROM scratch AS embed
COPY --from=embed-generate /src/pkg/machinery/gendata/data /pkg/machinery/gendata/data

FROM embed-generate AS embed-abbrev-generate
ARG ABBREV_TAG
RUN echo -n "undefined" > pkg/machinery/gendata/data/sha && \
echo -n ${ABBREV_TAG} > pkg/machinery/gendata/data/tag
RUN mkdir -p _out && \
echo PKGS=${PKGS} >> _out/talos-metadata && \
echo TAG=${TAG} >> _out/talos-metadata && \
echo EXTRAS=${EXTRAS} >> _out/talos-metadata

FROM scratch AS embed-abbrev
COPY --from=embed-abbrev-generate /src/pkg/machinery/gendata/data /pkg/machinery/gendata/data
COPY --from=embed-abbrev-generate /src/_out/talos-metadata /_out/talos-metadata

FROM ${EMBED_TARGET} AS embed-target

# generate API descriptors
FROM build AS api-descriptors-build
WORKDIR /src/api
Expand All @@ -308,6 +347,7 @@ COPY --from=proto-format-build /src/api/ /api/
FROM build-go AS go-generate
COPY ./pkg ./pkg
COPY ./hack/boilerplate.txt ./hack/boilerplate.txt
COPY --from=embed-target / ./
RUN --mount=type=cache,target=/.cache go generate ./pkg/...
RUN goimports -w -local github.com/siderolabs/talos ./pkg/
RUN gofumpt -w ./pkg/
Expand Down Expand Up @@ -350,43 +390,6 @@ RUN find /api/resource/definitions/ -type f -name "*.proto" | xargs -I {} /bin/s
RUN goimports -w -local github.com/siderolabs/talos /api/
RUN gofumpt -w /api/

FROM build AS embed-generate
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

FROM scratch AS embed
COPY --from=embed-generate /src/pkg/machinery/gendata/data /pkg/machinery/gendata/data

FROM embed-generate AS embed-abbrev-generate
ARG ABBREV_TAG
RUN echo -n "undefined" > pkg/machinery/gendata/data/sha && \
echo -n ${ABBREV_TAG} > pkg/machinery/gendata/data/tag
RUN mkdir -p _out && \
echo PKGS=${PKGS} >> _out/talos-metadata && \
echo TAG=${TAG} >> _out/talos-metadata && \
echo EXTRAS=${EXTRAS} >> _out/talos-metadata
COPY --from=pkg-kernel /certs/signing_key.x509 _out/signing_key.x509

FROM scratch AS embed-abbrev
COPY --from=embed-abbrev-generate /src/pkg/machinery/gendata/data /pkg/machinery/gendata/data
COPY --from=embed-abbrev-generate /src/_out/talos-metadata /_out/talos-metadata
COPY --from=embed-abbrev-generate /src/_out/signing_key.x509 /_out/signing_key.x509

FROM tools AS selinux
COPY ./internal/pkg/selinux/policy/* /selinux/
RUN mkdir /policy; secilc -o /policy/policy.33 -f /policy/file_contexts -c 33 /selinux/**/*.cil -vvvvv -O
Expand Down Expand Up @@ -430,6 +433,7 @@ COPY --from=go-generate /src/pkg/machinery/config/schemas/ /pkg/machinery/config
COPY --from=go-generate /src/pkg/machinery/config/types/ /pkg/machinery/config/types/
COPY --from=go-generate /src/pkg/machinery/nethelpers/ /pkg/machinery/nethelpers/
COPY --from=go-generate /src/pkg/machinery/extensions/ /pkg/machinery/extensions/
COPY --from=go-generate /src/pkg/machinery/version/os-release /pkg/machinery/version/os-release
COPY --from=ipxe-generate / /pkg/provision/providers/vm/internal/ipxe/data/ipxe/
COPY --from=selinux-generate / /internal/pkg/selinux/
COPY --from=embed-abbrev / /
Expand Down Expand Up @@ -743,9 +747,10 @@ COPY --chmod=0644 hack/cri-plugin.part /rootfs/etc/cri/conf.d/00-base.part
COPY --chmod=0644 hack/udevd/99-default.link /rootfs/usr/lib/systemd/network/
COPY --chmod=0644 hack/udevd/90-selinux.rules /rootfs/usr/lib/udev/rules.d/
COPY --chmod=0644 hack/lvm.conf /rootfs/etc/lvm/lvm.conf
COPY --chmod=0644 --from=base /src/pkg/machinery/version/os-release /rootfs/etc/os-release
RUN <<END
ln -s /usr/share/zoneinfo/Etc/UTC /rootfs/etc/localtime
touch /rootfs/etc/{extensions.yaml,resolv.conf,hosts,os-release,machine-id,cri/conf.d/cri.toml,cri/conf.d/01-registries.part,cri/conf.d/20-customization.part,cri/conf.d/base-spec.json,ssl/certs/ca-certificates,selinux/targeted/contexts/files/file_contexts}
touch /rootfs/etc/{extensions.yaml,resolv.conf,hosts,machine-id,cri/conf.d/cri.toml,cri/conf.d/01-registries.part,cri/conf.d/20-customization.part,cri/conf.d/base-spec.json,ssl/certs/ca-certificates,selinux/targeted/contexts/files/file_contexts}
ln -s ca-certificates /rootfs/etc/ssl/certs/ca-certificates.crt
ln -s /etc/ssl /rootfs/etc/pki
ln -s /etc/ssl /rootfs/usr/share/ca-certificates
Expand Down Expand Up @@ -819,9 +824,10 @@ COPY --chmod=0644 hack/cri-plugin.part /rootfs/etc/cri/conf.d/00-base.part
COPY --chmod=0644 hack/udevd/99-default.link /rootfs/usr/lib/systemd/network/
COPY --chmod=0644 hack/udevd/90-selinux.rules /rootfs/usr/lib/udev/rules.d/
COPY --chmod=0644 hack/lvm.conf /rootfs/etc/lvm/lvm.conf
COPY --chmod=0644 --from=base /src/pkg/machinery/version/os-release /rootfs/etc/os-release
RUN <<END
ln -s /usr/share/zoneinfo/Etc/UTC /rootfs/etc/localtime
touch /rootfs/etc/{extensions.yaml,resolv.conf,hosts,os-release,machine-id,cri/conf.d/cri.toml,cri/conf.d/01-registries.part,cri/conf.d/20-customization.part,cri/conf.d/base-spec.json,ssl/certs/ca-certificates,selinux/targeted/contexts/files/file_contexts}
touch /rootfs/etc/{extensions.yaml,resolv.conf,hosts,machine-id,cri/conf.d/cri.toml,cri/conf.d/01-registries.part,cri/conf.d/20-customization.part,cri/conf.d/base-spec.json,ssl/certs/ca-certificates,selinux/targeted/contexts/files/file_contexts}
ln -s /etc/ssl /rootfs/etc/pki
ln -s ca-certificates /rootfs/etc/ssl/certs/ca-certificates.crt
ln -s /etc/ssl /rootfs/usr/share/ca-certificates
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ARTIFACTS := _out
TOOLS ?= ghcr.io/siderolabs/tools:v1.10.0-alpha.0-3-g0393558

DEBUG_TOOLS_SOURCE := scratch
EMBED_TARGET ?= embed

PKGS_PREFIX ?= ghcr.io/siderolabs
PKGS ?= v1.10.0-alpha.0-18-ge7eddcf
Expand Down Expand Up @@ -199,6 +200,7 @@ COMMON_ARGS += --build-arg=TOOLS=$(TOOLS)
COMMON_ARGS += --build-arg=DEBUG_TOOLS_SOURCE=$(DEBUG_TOOLS_SOURCE)
COMMON_ARGS += --build-arg=PKGS=$(PKGS)
COMMON_ARGS += --build-arg=EXTRAS=$(EXTRAS)
COMMON_ARGS += --build-arg=EMBED_TARGET=$(EMBED_TARGET)
COMMON_ARGS += --build-arg=GOFUMPT_VERSION=$(GOFUMPT_VERSION)
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION=$(GOIMPORTS_VERSION)
COMMON_ARGS += --build-arg=STRINGER_VERSION=$(STRINGER_VERSION)
Expand Down Expand Up @@ -356,7 +358,7 @@ hack-test-%: ## Runs the specified script in ./hack/test with well known environ

.PHONY: generate
generate: ## Generates code from protobuf service definitions and machinery config.
@$(MAKE) local-$@ DEST=./ PLATFORM=linux/amd64
@$(MAKE) local-$@ DEST=./ PLATFORM=linux/amd64 EMBED_TARGET=embed-abbrev

.PHONY: docs
docs: ## Generates the documentation for machine config, and talosctl.
Expand Down
60 changes: 0 additions & 60 deletions internal/app/machined/pkg/startup/os_release.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/app/machined/pkg/startup/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ func DefaultTasks() []Task {
SetEnvironmentVariables,
WriteIMAPolicy,
CreateSystemCgroups,
OSRelease,
}
}
25 changes: 25 additions & 0 deletions pkg/machinery/version/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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/.

//go:build generate

package main

import (
"log"
"os"

"github.com/siderolabs/talos/pkg/machinery/version"
)

func main() {
data, err := version.OSRelease()
if err != nil {
log.Fatal(err)
}

if err = os.WriteFile("os-release", data, 0o644); err != nil {
log.Fatal(err)
}
}
8 changes: 8 additions & 0 deletions pkg/machinery/version/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME="Talos"
ID=talos
VERSION_ID=v1.10.0-alpha.0
PRETTY_NAME="Talos (v1.10.0-alpha.0)"
HOME_URL="https://www.talos.dev/"
BUG_REPORT_URL="https://github.com/siderolabs/talos/issues"
VENDOR_NAME="Sidero Labs"
VENDOR_URL="https://www.siderolabs.com/"
2 changes: 2 additions & 0 deletions pkg/machinery/version/osrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package version

//go:generate go run ./gen.go

import (
"bytes"
"strings"
Expand Down

0 comments on commit b7a7fdc

Please sign in to comment.