From ab27c9d5f18cb080aff37770d1c55f2554c6b307 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Tue, 19 Dec 2023 12:56:46 -0500 Subject: [PATCH 1/2] version: use go list -m It appears that the value of Package is intended to be what is nowadays called the module path, not the path to the version package. This also fixes the issue of the version file being regenerated incorrectly under shell redirection as the go list command no longer attempts to parse .go files under the version package. $ ./version.sh > version.go version.go:1:1: expected 'package', found 'EOF' Signed-off-by: Cory Snider --- version/version.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/version.sh b/version/version.sh index 75dca7847..bef4e74db 100755 --- a/version/version.sh +++ b/version/version.sh @@ -12,7 +12,7 @@ package version // Package is the overall, canonical project import path under which the // package was built. -var Package = "$(go list)" +var Package = "$(go list -m)" // Version indicates which version of the binary is running. This is set to // the latest release tag by hand, always suffixed by "+unknown". During From a74cacff04e76ec91230ed407a10d50f64c028ef Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Tue, 19 Dec 2023 13:02:44 -0500 Subject: [PATCH 2/2] version: export getter functions Future-proof the version package's exported interface by only making the data available through getter functions. This affords us the flexibility to e.g. implement them in terms of "runtime/debug".ReadBuildInfo() in the future. Signed-off-by: Cory Snider --- Makefile | 2 +- registry/handlers/app.go | 2 +- registry/registry.go | 2 +- tracing/tracing.go | 2 +- version/{print.go => get.go} | 20 +++++++++++++++++++- version/version.go | 12 ++++++------ version/version.sh | 12 ++++++------ 7 files changed, 35 insertions(+), 17 deletions(-) rename version/{print.go => get.go} (53%) diff --git a/Makefile b/Makefile index 2723a2aff..235678988 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ WHALE = "+" TESTFLAGS_RACE= GOFILES=$(shell find . -type f -name '*.go') GO_TAGS=$(if $(BUILDTAGS),-tags "$(BUILDTAGS)",) -GO_LDFLAGS=-ldflags '-extldflags "-Wl,-z,now" -s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG) $(EXTRA_LDFLAGS)' +GO_LDFLAGS=-ldflags '-extldflags "-Wl,-z,now" -s -w -X $(PKG)/version.version=$(VERSION) -X $(PKG)/version.revision=$(REVISION) -X $(PKG)/version.mainpkg=$(PKG) $(EXTRA_LDFLAGS)' BINARIES=$(addprefix bin/,$(COMMANDS)) diff --git a/registry/handlers/app.go b/registry/handlers/app.go index 64e49e411..2983176b8 100644 --- a/registry/handlers/app.go +++ b/registry/handlers/app.go @@ -114,7 +114,7 @@ func NewApp(ctx context.Context, config *configuration.Configuration) *App { storageParams = make(configuration.Parameters) } if storageParams["useragent"] == "" { - storageParams["useragent"] = fmt.Sprintf("distribution/%s %s", version.Version, runtime.Version()) + storageParams["useragent"] = fmt.Sprintf("distribution/%s %s", version.Version(), runtime.Version()) } var err error diff --git a/registry/registry.go b/registry/registry.go index 6fb79f189..875e776a0 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -99,7 +99,7 @@ var ServeCmd = &cobra.Command{ Long: "`serve` stores and distributes Docker images.", Run: func(cmd *cobra.Command, args []string) { // setup context - ctx := dcontext.WithVersion(dcontext.Background(), version.Version) + ctx := dcontext.WithVersion(dcontext.Background(), version.Version()) config, err := resolveConfiguration(args) if err != nil { diff --git a/tracing/tracing.go b/tracing/tracing.go index 8a8559e7b..c75acf81e 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -26,7 +26,7 @@ func InitOpenTelemetry(ctx context.Context) error { res := resource.NewWithAttributes( semconv.SchemaURL, semconv.ServiceNameKey.String(serviceName), - semconv.ServiceVersionKey.String(version.Version), + semconv.ServiceVersionKey.String(version.Version()), ) exp, err := autoexport.NewSpanExporter(ctx) diff --git a/version/print.go b/version/get.go similarity index 53% rename from version/print.go rename to version/get.go index 5529da85f..db68e6d46 100644 --- a/version/print.go +++ b/version/get.go @@ -6,6 +6,24 @@ import ( "os" ) +// Package returns the overall, canonical project import path under +// which the package was built. +func Package() string { + return mainpkg +} + +// Version returns returns the module version the running binary was +// built from. +func Version() string { + return version +} + +// Revision returns the VCS (e.g. git) revision being used to build +// the program at linking time. +func Revision() string { + return revision +} + // FprintVersion outputs the version string to the writer, in the following // format, followed by a newline: // @@ -16,7 +34,7 @@ import ( // // registry github.com/distribution/distribution v2.0 func FprintVersion(w io.Writer) { - fmt.Fprintln(w, os.Args[0], Package, Version) + fmt.Fprintln(w, os.Args[0], Package(), Version()) } // PrintVersion outputs the version information, from Fprint, to stdout. diff --git a/version/version.go b/version/version.go index a99b77598..ceaa82f2a 100644 --- a/version/version.go +++ b/version/version.go @@ -1,15 +1,15 @@ package version -// Package is the overall, canonical project import path under which the +// mainpkg is the overall, canonical project import path under which the // package was built. -var Package = "github.com/distribution/distribution/v3" +var mainpkg = "github.com/distribution/distribution/v3" -// Version indicates which version of the binary is running. This is set to +// version indicates which version of the binary is running. This is set to // the latest release tag by hand, always suffixed by "+unknown". During // build, it will be replaced by the actual version. The value here will be // used if the registry is run after a go get based install. -var Version = "v3.0.0-alpha.1" +var version = "v3.0.0-alpha.1.m+unknown" -// Revision is filled with the VCS (e.g. git) revision being used to build +// revision is filled with the VCS (e.g. git) revision being used to build // the program at linking time. -var Revision = "" +var revision = "" diff --git a/version/version.sh b/version/version.sh index bef4e74db..176a370a2 100755 --- a/version/version.sh +++ b/version/version.sh @@ -10,17 +10,17 @@ set -e cat <