Files
nvke/agent/Makefile
Alex Pelavin f41e9b693c ci: version and release every component by the bare vX.Y.Z tag (#2164)
* ci: version every artifact by the bare v* describe

Every published artifact is now versioned by
`git describe --tags --always --match 'v*' "${GITHUB_SHA}"`, with no
component prefix, so component versions follow the nvke tag rather than
moving independently. The controller image, chart, provisioned provider
image, agent binaries and packages, nvkectl and the OS image resolve
their version this way, and the version is passed to each build so the
binaries report the same version they are published under.

The release registry describes and resolves components against the
bare v* tags, so a beta qualifies the artifacts its tag published.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: build and publish every component on a bare vX.Y.Z tag

Every component build that a vX.Y.Z-beta.N tag runs now runs on a bare
vX.Y.Z tag too: the controller and provisioned provider images, the
controller chart, the agent binaries and packages, the OS image and
nvkectl. The gates that admitted only beta tags admit any bare v* tag,
and nvkectl is versioned by the bare describe for both.

Beta-only behaviour is unchanged: a beta still skips DSX, stays out of
the apt index and bakes non-FDE images only. A release does all three.
promote-dsx now runs on vX.Y.Z, so it tags DSX with the bare describe
the NVCR copy was published under.

The beta flag in tag-publish.yml is removed, so nvkectl is published to
GHCR for every tag it is built for, betas included. cli-artifact.yml
versions it by the bare describe.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* build: version every module by the bare v* describe

The Makefiles, the root cmd_version and the scripts that derive the
agent version described against component tags (agent/v*, controller/v*,
cli/v*, os-image/v*, cluster-api-provider-provisioned/v*,
management-engine/v*). They now use
`git describe --tags --always --dirty --match 'v*'`, the version the
publish workflows already stamp, so a local or CI build reports the same
version as the artifact published from that commit.

The fallbacks for an untagged checkout and a build outside git are
unchanged, as is the agent's 0.0.0- prefix for an untagged .deb. The
e2e VALIDATE_VERSION now pairs with the nvke-validate image, and the
comments and log lines that named the old describe are updated.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* docs: describe one vX.Y.Z tag releasing every component

Components are no longer tagged on their own. The release docs, the
module AGENTS.md and README files and SECURITY.md now say one signed
vX.Y.Z tag releases the repository and every component built from it,
versioned by `git describe --tags --always --match 'v*'`.

docs/artifact-publication.md lists what each workflow publishes for a
release and for a nightly beta, and that an RC publishes nothing of its
own. The prefixed tag instructions, and the go get example that relied
on them, are removed. Mentions of past component tags that remain true
as history are kept.

The qualification docs and the qualify-e2e resolution input now say a
release, an RC and a beta are all qualified with commitDescribe, which
describes the commit the version marks and so resolves every component
to it. gitTag takes the highest v* tag in the repository and is not for
qualification.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: stop publishing artifacts from component release tags

A component release tag (agent/vX.Y.Z, controller/vX.Y.Z,
cluster-api-provider-provisioned/vX.Y.Z, os-image/vX.Y.Z, cli/vX.Y.Z) no
longer starts a publisher; a release is a bare vX.Y.Z tag. Component
release candidate tags still start the same workflows as before.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: publish every GHCR release artifact on vX.Y.Z

agent-artifact.yml, chart-publish.yml and image-validate-nvcr.yml only ran
on component tags, so a release published none of them. Each now runs on a
bare vX.Y.Z tag and versions by the bare v* describe. The controller and
provider GHCR promotions and the agent .deb GHCR push also run on vX.Y.Z
(not betas), since they were gated on component tags too and the GHCR
chart waits for the controller image there.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: name every tag build by its describe version, not the tag

The publishers versioned the build by the bare v* describe but still took
version aliases, GHCR promotion tags, the agent .deb version, the nvkectl
S3 release path and the os-image version from the tag name. On a component
RC tag the two disagreed, and on a vX.Y.Z tag agent-deb's GHCR push would
have been tagged with the commit SHA. Every one now uses the describe
version, which on the tagged commit is the tag itself.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: pass the describe version to build commands through the environment

The version is git describe output, which a tag name controls. It was
interpolated with ${{ }} into the run script of six build steps, so a tag
containing shell syntax would have run in jobs that hold registry
credentials. Each step now reads it from an environment variable.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: give component RC tags no version alias

A component RC tag still starts the agent and nvkectl publishers, and
they name the build by the bare v* describe. On a commit that already
carries a release tag that is the release's version, so the RC run would
move the release's alias to a new manifest. Only a bare v* tag now adds
the version alias; a component RC tag publishes sha-<commit> only.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: resolve every publisher's version through one checked script

Every publisher resolves its version through
release/hack/describe-version.sh instead of calling git describe itself.
A bare v* tag run is published under its own tag; any other run under
the v* describe. Before anything is pushed, the script refuses:

- a describe that is not a v* release, RC or beta, its describe suffix,
  or a bare commit. The describe can name any v* tag on the commit, and
  agent/Makefile passes it to the shell inside -ldflags.
- on a v* tag run, a tag that is not a version or does not name the
  commit being built.
- on a component tag run, a commit a v* tag already names. Its describe
  is that tag, so a component RC there would republish that tag's
  .debs, images, charts and nvkectl.

Naming a tag run by its tag rather than the describe matters when the
commit carries several: git describe prefers the annotated release, and
of two lightweight betas the first by name. A beta re-run on a released
commit, or a second beta on one commit, now publishes under its own
version instead of another tag's.

release/hack/test_describe_version.py covers these cases against fixture
repositories, and runs with next-tag-test in the merge gate.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: let only a release version into the apt index and GHCR

agent-deb-publish.yml chose whether to update index.json, and whether to
push the .debs to GHCR, from the ref: anything but a beta tag did both.
An agent/v* RC tag carries no -beta., but its packages are versioned by
the describe, so on a commit past a beta it made that build installable
from apt and pushed it to GHCR.

The build job now reports whether its version is a bare vX.Y.Z, and both
follow that instead. A release enters the index and GHCR; a beta, a
component RC and anything else is uploaded under releases/<version>/ and
pushed to NVCR only.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: stamp the published version into standalone nvkectl builds

A dispatched cli-artifact, cli-artifact-nvcr or cli-s3-publish run with no
dist_artifact builds nvkectl itself. The build took its version from the
Makefile's own git describe, which on a commit with a beta and a signed
release names the release, while the run publishes under the beta it was
dispatched from. Pass the resolved version as NVKECTL_VERSION, as
tag-publish.yml already does, so the binary reports the version it is
published under.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: skip a beta dispatch of the GHCR chart publisher

A beta's controller image is never promoted to GHCR, so a chart-publish.yml
run dispatched from a beta tag waited up to 30 minutes for an image that
would not appear, then failed. Skip beta refs, as the automatic beta trigger
already does.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* test: cover the triggering-tag check and a bare RC in describe-version.sh

Every invalid tag in the existing test was also the commit's describe, so
the describe check refused it before the check on the triggering tag ran,
and disabling that check left every test passing. Tag the bad names beside a
valid signed release, which wins the describe, so only the tag check can
refuse them.

Also pin that the helper names a bare RC run by its tag: the publishers'
tag filters, not the helper, keep an RC from publishing.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

* ci: recover a vX.Y.Z agent package release by dispatch

A failed agent/vX.Y.Z publish could be recovered by dispatching Agent Deb
Publish from that tag with ref set to it. Releases are now vX.Y.Z, which the
ref check refused, and a SHA got past it only for every publish job to skip,
since their bare-tag arm requires a push.

Accept a vX.Y.Z ref when the run is dispatched from that same tag, and let
it into the apt intake, GHCR and NVCR jobs. The tag names the run's version,
so the index.json and GHCR gates still admit only a release. A beta is not
admitted, as before.

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>

---------

Signed-off-by: Alex Pelavin <apelavin@nvidia.com>
2026-11-16 10:05:29 -06:00

411 lines
18 KiB
Makefile

# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# The licence header template lives once, at the repository root.
REPO_ROOT ?= $(shell git rev-parse --show-toplevel)
# Makefile for multi-arch agent
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
# ?= means: only set if not already defined (preserves env var from CI)
GOBIN ?= $(shell go env GOBIN)
ifeq (,$(GOBIN))
GOBIN = $(shell go env GOPATH)/bin
endif
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
# Every component is versioned by the repository's bare vX.Y.Z tags.
# Override with VERSION=... when building outside of a tagged checkout.
VERSION ?= $(shell git describe --tags --always --dirty --match 'v*' 2>/dev/null || git rev-parse --short HEAD)
# dpkg refuses a version that does not start with a digit:
#
# dpkg-deb: error: 'Version' field value 'f0578cc8':
# version number does not start with digit
#
# `git describe --always` falls back to a bare commit sha when no v* tag is
# reachable, such as in a fork without tags. Prefixing 0.0.0- keeps an untagged build working and
# sorts below every real release, so a development package can never be
# mistaken for, or upgrade over, a tagged one.
# The test is "did this come from a tag", NOT "does it start with a digit".
# Tags are vX.Y.Z, so VERSION starts with `v` exactly when describe found one;
# anything else is the --always sha fallback.
#
# Testing the first character instead is wrong and looks right: roughly ten of
# the sixteen possible leading hex digits ARE digits, so a sha like 587f4bbd
# passes and becomes the package version verbatim -- and 587f4bbd sorts ABOVE
# every real release, so a development build would upgrade over a tagged one.
# The bug reproduces on about 62% of commits, which is the worst kind.
#
# Pure Make, no $(shell): Make counts parentheses to find the end of
# $(shell ...), so an unbalanced `)` in a shell pattern like [0-9]*) closes it
# early and the rest of the statement leaks into the value.
DEB_VERSION := $(if $(filter v%,$(VERSION)),$(patsubst v%,%,$(VERSION)),0.0.0-$(VERSION))
$(info $$VERSION is [${VERSION}])
$(info $$DEB_VERSION is [${DEB_VERSION}])
RELEASE_LDFLAGS ?= -s -w -buildid=
# -trimpath removes the build machine's filesystem paths from the binary. These
# binaries are published to ghcr.io/dsx-ai-factory/nvke/agent, so without it every
# release carries the layout of whatever machine built it, and two builds of the
# same commit from different directories differ.
LDFLAGS += -trimpath -ldflags="-X main.VERSION=$(VERSION) $(RELEASE_LDFLAGS)"
ARCHES := amd64 arm64
# golangci-lint pin and bootstrap, shared with every other module.
include ../make/tool-versions.mk
include ../make/golangci.mk
include ../make/coverage.mk
GIT_HOOKS_DIR := $(shell git rev-parse --git-path hooks 2>/dev/null || echo .git/hooks)
BIN_DIR ?= $(CURDIR)/bin
NFPM_BIN ?= $(BIN_DIR)/nfpm-v$(NFPM_VERSION)/nfpm
AGENT_PACKAGING_DIR := $(abspath ../os-image/packages/nvke-agent/packaging)
# Optional exact output path for consumers such as the Packer image build.
DEB_OUTPUT ?=
##@ Help
.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Git hooks
.PHONY: setup-hooks
setup-hooks: ## Install or update Git pre-commit hook (monorepo-aware)
@set -e; \
echo "🔧 Ensuring Git pre-commit hook is up to date..."; \
mkdir -p "$(GIT_HOOKS_DIR)"; \
src="internal-scripts/hooks/pre-commit"; \
dst="$(GIT_HOOKS_DIR)/pre-commit"; \
if [ ! -f "$$dst" ] || ! cmp -s "$$src" "$$dst"; then \
cp "$$src" "$$dst"; \
chmod +x "$$dst"; \
echo "✅ Installed/updated Git pre-commit hook at $$dst"; \
else \
echo "✅ Git pre-commit hook already up to date."; \
fi
##@ Docker images
.PHONY: build-builder-image push-builder-image setup-buildx
BUILDER_IMAGE := $(or $(BUILDER_IMAGE),$(if $(NKE_CONTAINER_REGISTRY),$(NKE_CONTAINER_REGISTRY)/$(or $(NKE_BUILDER_IMAGE_NAME),nke-builder),ghcr.io/example/nvke/nke-builder))
BUILDER_TAG := $(or $(BUILDER_TAG),$(or $(NKE_BUILDER_IMAGE_TAG),latest))
BUILDX_PLATFORMS := linux/amd64,linux/arm64
BUILDX_BUILDER := nke-multiarch-builder
# The docker-container driver runs BuildKit in its own container, so it does not
# inherit the Docker daemon's registry-mirror configuration. CI runners ship a
# prepopulated /etc/buildkit/buildkitd.toml pointing at the local mirror; pass it
# through when present so builds are cached and are not subject to Docker Hub
# rate limits. Absent (developer laptops), the builder is created without it and
# pulls directly, which is the current behaviour.
BUILDKITD_CONFIG ?= /etc/buildkit/buildkitd.toml
BUILDX_CONFIG_ARG = $(if $(wildcard $(BUILDKITD_CONFIG)),--config $(BUILDKITD_CONFIG),)
setup-buildx: ## Create buildx builder for multi-arch builds (if not exists)
@docker buildx inspect $(BUILDX_BUILDER) >/dev/null 2>&1 || \
docker buildx create --name $(BUILDX_BUILDER) --driver docker-container \
--driver-opt env.GOGC=100 \
--buildkitd-flags '--allow-insecure-entitlement network.host' \
$(BUILDX_CONFIG_ARG) \
--bootstrap
docker buildx use $(BUILDX_BUILDER)
build-builder-image: setup-buildx ## Build the CI/CD builder image for amd64 and arm64
docker buildx build \
--platform $(BUILDX_PLATFORMS) \
-t $(BUILDER_IMAGE):$(BUILDER_TAG) \
-f Dockerfile.nke-builder \
--shm-size=2g \
--ulimit nofile=1048576:1048576 \
--progress=plain \
.
push-builder-image: setup-buildx ## Build and push the CI/CD builder image for amd64 and arm64
docker buildx build \
--platform $(BUILDX_PLATFORMS) \
-t $(BUILDER_IMAGE):$(BUILDER_TAG) \
-f Dockerfile.nke-builder \
--shm-size=2g \
--ulimit nofile=1048576:1048576 \
--progress=plain \
--push \
.
##@ Proto generation
.PHONY: buf
buf: ## Install buf if not present or wrong version
@$(GOBIN)/buf version 2>/dev/null | grep -q $(BUF_VERSION) || \
go install github.com/bufbuild/buf/cmd/buf@$(BUF_VERSION)
# One definition of where the generated Go lands. PROTO_GEN_FILES expands when
# this file is parsed, so check-protos sees the committed set before
# clean-proto removes it.
PROTO_DIR := pkg/agent/proto
PROTO_GEN_FILES := $(wildcard $(PROTO_DIR)/*.pb.go)
.PHONY: protos
protos: buf clean-proto ## Generate proto files using buf
$(GOBIN)/buf generate
.PHONY: proto-fmt
proto-fmt: buf ## Format proto files using buf
$(GOBIN)/buf format -w
.PHONY: check-protos
check-protos: buf ## Check proto formatting, and that *.pb.go matches the .proto
@$(GOBIN)/buf format --diff --exit-code \
|| \
(echo "❌ Proto files need formatting. Run 'make proto-fmt' to fix." && false)
@# Regenerate and diff, the shape controller/Makefile:126-134 uses for the
@# OpenAPI output. Without it this target only ever checked its own
@# formatter, so a .proto change committed without `make protos` reached
@# main unseen. Expects a clean working directory.
@# git diff reports nothing for an untracked path, so a new generated file
@# would pass silently -- check tracking first, as notices-check does.
@for f in $(PROTO_GEN_FILES); do \
git ls-files --error-unmatch "$$f" >/dev/null 2>&1 \
|| { echo "❌ $$f is not tracked. Run 'make protos' and commit it."; exit 1; }; \
done
@$(MAKE) --no-print-directory protos
@git diff --exit-code -- $(PROTO_DIR) \
|| { echo "❌ *.pb.go is stale. Run 'make protos' and commit the result."; exit 1; }
@# git diff says nothing about a path git is not tracking, so a newly
@# generated file would otherwise pass while never being committed. Same
@# guard as Makefile's licences-check.
@untracked=$$(git ls-files --others --exclude-standard -- $(PROTO_DIR)); \
[ -z "$$untracked" ] \
|| { echo "❌ generated proto files are not tracked. Run 'make protos' and commit them:"; printf '%s\n' "$$untracked" | sed 's/^/ /'; exit 1; }
.PHONY: clean-proto
clean-proto: ## Remove generated proto files
rm -f $(PROTO_DIR)/*.pb.go
##@ Code quality
.PHONY: fmt vet lint test
fmt: ## Run go fmt
go fmt ./...
vet: ## Run go vet
go vet ./...
.PHONY: lint
lint: ## Lint source files. (run by the merge gate)
@$(GOLANGCI_LINT_RUN) run --timeout 10m --show-stats ./...
# GO_TEST_FLAGS lets CI add -race without the workflow having to restate the
# test command, so `make test` stays the one definition of what "the tests" are.
GO_TEST_FLAGS ?=
# macOS puts TMPDIR under /var/folders, and /var there is a symlink to
# /private/var. internal/agent refuses a destination whose parent path contains a
# symlink, so 23 tests failed on a Mac with "invalid destination ...: parent path
# /var is a symlink". The agent was right, and the tests were only reading the
# developer's TMPDIR. /private/tmp is the same directory with the symlink already
# resolved. Linux and CI are unaffected.
ifeq ($(shell uname -s),Darwin)
export TMPDIR := /private/tmp
endif
test: ## Run tests (run by the merge gate)
@$(COVERAGE_MKDIR)
go test $(GO_TEST_FLAGS) $(GO_COVER_FLAGS) ./...
# Sources in this module carry the repository's Apache-2.0 identifier, per the
# "License headers" section of the root CONTRIBUTING.md. Ignored paths are
# generated output (*.pb.go, rewritten by `make protos`), build tooling under
# hack/, build artifacts, and samples/kubeadm-deploy/calico.yaml, which is an
# upstream Calico manifest and not ours to stamp. Dockerfiles are ignored too:
# addlicense keys off the file extension, so Dockerfile.proto gets C-style `//`
# comments, which Docker cannot parse.
ADDLICENSE_IGNORES := \
-ignore '**/Dockerfile*' \
-ignore 'bin/**' \
-ignore 'build/**' \
-ignore 'release/**' \
-ignore 'vendor/**' \
-ignore 'hack/**' \
-ignore '**/*.pb.go' \
-ignore 'samples/kubeadm-deploy/calico.yaml'
.PHONY: license
license: ## Ensure license headers at code.
go run github.com/google/addlicense@$(ADDLICENSE_VERSION) -f $(REPO_ROOT)/hack/license-header.tmpl -c "NVIDIA CORPORATION & AFFILIATES" $(ADDLICENSE_IGNORES) .
# Expected to be run on clean working directory.
.PHONY: file-license-check
file-license-check: license ## Verify all files have license headers.
@if ! git diff --exit-code -- . > /dev/null 2>&1; then \
echo "License headers are missing or outdated. Please run 'make license' locally and commit the changes."; \
echo "Files with missing/outdated license headers:"; \
git diff --name-only -- .; \
exit 1; \
fi
##@ Utility
.PHONY: clean
clean: ## Remove all build artifacts except protos
rm -rf bin release
##@ Build
.PHONY: build-amd64 build-arm64 build-all build
# cgo is left to Go on a native host and turned off when cross-compiling.
#
# On a native Linux runner this is a no-op: cgo stays on, the binary links
# against libc, and `Depends: libc6` in the nfpm spec is true. That is what
# deb.yml relies on by giving each architecture a runner of its own arch.
#
# Cross-compiling is where it matters. Go turns cgo off by itself when there is
# no C toolchain for the target, but an environment that exports CC -- the
# repository's Flox environment sets CC=clang -- makes it try anyway, and the
# build then fails against the host SDK:
#
# /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:609:6:
# note: 'setreuid' declared here
# make: *** [Makefile:230: build-amd64] Error 1
#
# So `make build` worked on a Mac outside Flox and failed inside it, for the same
# checkout. Deciding here makes the result the same either way.
GOHOSTOS := $(shell go env GOHOSTOS)
GOHOSTARCH := $(shell go env GOHOSTARCH)
agent_cgo = $(if $(and $(filter linux,$(GOHOSTOS)),$(filter $(1),$(GOHOSTARCH))),,CGO_ENABLED=0)
build-amd64:
$(call agent_cgo,amd64) GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o bin/linux_amd64/nvke-agent ./cmd/agent/main.go
$(call agent_cgo,amd64) GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-X main.version=$(VERSION) $(RELEASE_LDFLAGS)" -o bin/linux_amd64/nvke-agent-updater ./cmd/updater/main.go
build-arm64:
$(call agent_cgo,arm64) GOOS=linux GOARCH=arm64 go build ${LDFLAGS} -o bin/linux_arm64/nvke-agent ./cmd/agent/main.go
$(call agent_cgo,arm64) GOOS=linux GOARCH=arm64 go build -trimpath -ldflags="-X main.version=$(VERSION) $(RELEASE_LDFLAGS)" -o bin/linux_arm64/nvke-agent-updater ./cmd/updater/main.go
build-all: build-amd64 build-arm64
build: build-all ## Default build = all arches
##@ Package
.PHONY: package-amd64 package-arm64 package-all package
package-amd64: build-amd64
rm -rf release/agent_${VERSION}_amd64
mkdir -p release/agent_${VERSION}_amd64
cp -r bin/linux_amd64/* release/agent_${VERSION}_amd64/
cp -r scripts/* release/agent_${VERSION}_amd64/
tar -czf release/agent_${VERSION}_amd64.tgz -C release agent_${VERSION}_amd64
package-arm64: build-arm64
rm -rf release/agent_${VERSION}_arm64
mkdir -p release/agent_${VERSION}_arm64
cp -r bin/linux_arm64/* release/agent_${VERSION}_arm64/
cp -r scripts/* release/agent_${VERSION}_arm64/
tar -czf release/agent_${VERSION}_arm64.tgz -C release agent_${VERSION}_arm64
package-all: package-amd64 package-arm64
package: package-all ## Default package = all arches
# Use the same pinned nfpm as the other node packages.
.PHONY: nfpm
nfpm: $(NFPM_BIN) ## Build the pinned Debian packaging tool.
$(NFPM_BIN):
@GOBIN=$(abspath $(dir $(NFPM_BIN))) GOPROXY='https://proxy.golang.org|direct' go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v$(NFPM_VERSION)
# Usage: $(call build_deb,amd64)
define build_deb
cd "$(AGENT_PACKAGING_DIR)" && ARCH="$(1)" DEB_VERSION="$(DEB_VERSION)" \
AGENT_DIR="$(CURDIR)" "$(abspath $(NFPM_BIN))" package -f nfpm.yaml -p deb \
-t "$(abspath $(or $(DEB_OUTPUT),bin/nvke-agent_$(DEB_VERSION)_$(1).deb))"
endef
##@ Debian Packaging
# The .deb declares `Depends: libc6` (the nfpm spec), so the binary inside it
# must link against libc. That happens only when cgo is on, and Go turns cgo off
# by itself when cross-compiling, because there is no C toolchain for the target.
#
# Nothing in this file sets CGO_ENABLED, so what you get is decided by the host:
# native builds are dynamic and correct, cross builds are static and ship a
# Depends that is false for them. deb.yml:24-33 records the same thing and solves
# it by building each architecture on a runner of that architecture.
#
# The same build-amd64 and build-arm64 targets also feed the OCI artifact, which
# wants the opposite: agent-artifact.yml sets CGO_ENABLED=0 and asserts
# "statically linked". One pair of targets, two callers, opposite requirements,
# and the Makefile said nothing about either.
#
# It says something now. assert_dynamic runs the check CI runs, so a cross build
# fails here with the reason rather than producing a package whose Depends is
# wrong. Set AGENT_SKIP_LINKAGE_CHECK=1 to build a deb you know is not for
# release.
define assert_dynamic
@if [ -z "$(AGENT_SKIP_LINKAGE_CHECK)" ] && command -v file >/dev/null 2>&1; then \
linkage=$$(file bin/linux_$(1)/nvke-agent); \
case "$$linkage" in \
*"dynamically linked"*) ;; \
*) echo "ERROR: bin/linux_$(1)/nvke-agent is not dynamically linked, so"; \
echo " 'Depends: libc6' in the nfpm spec is wrong for it."; \
echo " Go disables cgo when cross-compiling, so build the $(1) package"; \
echo " on a $(1) host, as deb.yml does. Override with"; \
echo " AGENT_SKIP_LINKAGE_CHECK=1 for a package you will not release."; \
echo " file said: $$linkage"; \
exit 1 ;; \
esac; \
fi
endef
.PHONY: deb-amd64 deb-arm64 deb-all debs
deb-amd64: build-amd64 $(NFPM_BIN) ## Build the amd64 Debian package (advisory; not in the merge gate)
$(call assert_dynamic,amd64)
$(call build_deb,amd64)
deb-arm64: build-arm64 $(NFPM_BIN) ## Build the arm64 Debian package (advisory; not in the merge gate)
$(call assert_dynamic,arm64)
$(call build_deb,arm64)
deb-all: deb-amd64 deb-arm64 ## Build all Debian packages
debs: deb-all
##@ Third-party notices
# go-licenses embeds go/packages, and must be built with the *same* toolchain
# that `go list` actually runs inside this module. A module's `go` directive can
# make Go switch toolchains (GOTOOLCHAIN=auto), and on a mismatch go-licenses
# reports every stdlib package as having no module info and emits nothing at
# all. `go install pkg@version` deliberately ignores the current module, so the
# toolchain has to be pinned explicitly rather than inherited.
# GOWORK=off: this must be the toolchain that builds THIS module. With a root
# go.work the bare command reports the workspace's Go version instead, and a
# go-licenses built with a different Go than runs it reports "no module info"
# and emits nothing -- the failure warned about just above.
GO_TOOLCHAIN := $(shell GOWORK=off go version | awk '{print $$3}')
bin/go-licenses:
GOTOOLCHAIN=$(GO_TOOLCHAIN) GOBIN=$(CURDIR)/bin \
go install github.com/google/go-licenses/v2@$(GO_LICENSES_VERSION)
.PHONY: notices
notices: bin/go-licenses ## Regenerate THIRD_PARTY_NOTICES.md.
@bash hack/generate-notices.sh
.PHONY: notices-generator-check
notices-generator-check: ## Check deterministic offline license/NOTICE rendering.
@bash hack/test-generate-notices.sh
.PHONY: notices-check
notices-check: notices-generator-check ## Verify THIRD_PARTY_NOTICES.md matches the dependency tree. (advisory; not in the merge gate)
@echo "- Checking THIRD_PARTY_NOTICES.md is up to date..."
@# Cheap gate first, before spending minutes regenerating. git diff reports
@# nothing for an untracked path, so an uncommitted notices file would
@# otherwise pass silently.
@git ls-files --error-unmatch THIRD_PARTY_NOTICES.md >/dev/null 2>&1 \
|| { echo "ERROR: THIRD_PARTY_NOTICES.md is not tracked. Run 'make notices' and commit it."; exit 1; }
@$(MAKE) notices
@git diff --exit-code -- THIRD_PARTY_NOTICES.md \
|| { echo "ERROR: THIRD_PARTY_NOTICES.md is stale. Run 'make notices' and commit the change."; exit 1; }