Skip to content

Commit

Permalink
build: add BranchReleaseSeries
Browse files Browse the repository at this point in the history
This change adds `build.BranchReleaseSeries()` which returns the major
and minor in `version.txt`. This will be used to know the current
release series when the latest `clusterversion` is not finalized.

We also clean up the code a bit: we separate the variables that are
overridden by Bazel, and we use a different variable for the testing
override (to make things more clear).

Epic: none
Release note: None
  • Loading branch information
RaduBerinde committed Nov 6, 2023
1 parent 56cf731 commit a65ede5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
72 changes: 49 additions & 23 deletions pkg/build/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,34 @@ import (
// with the string passed to the linker in the root Makefile.
const TimeFormat = "2006/01/02 15:04:05"

// These variables are initialized by Bazel via the linker -X flag
// when compiling release binaries.
var (
// These variables are initialized by Bazel via the linker -X flag
// when compiling release binaries.
utcTime string // Build time in UTC (year/month/day hour:min:sec)
rev string // SHA-1 of this build (git rev-parse)
buildTagOverride string
cgoCompiler = cgoVersion()
cgoTargetTriple string
platform = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)
// Distribution is changed by the CCL init-time hook in non-APL builds.
Distribution = "OSS"
typ string // Type of this build: <empty>, "development", or "release"
channel string
typ string // Type of this build: <empty>, "development", or "release"
channel string
)

// Distribution is changed by the CCL init-time hook in non-APL builds.
var Distribution = "OSS"

var (
cgoCompiler = cgoVersion()
platform = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)
envChannel = envutil.EnvOrDefaultString("COCKROACH_CHANNEL", "unknown")
enabledAssertions = buildutil.CrdbTestBuild
)

var (
//go:embed version.txt
cockroachVersion string
binaryVersion = computeBinaryVersion(cockroachVersion, rev)
versionTxt string
parsedVersionTxt *version.Version = parseCockroachVersion(versionTxt)
binaryVersion string = computeBinaryVersion(buildTagOverride, parsedVersionTxt, rev)
// binaryVersionTestingOverride is modified by TestingOverrideVersion.
binaryVersionTestingOverride string
)

const (
Expand All @@ -64,38 +74,54 @@ func SeemsOfficial() bool {
return channel == DefaultTelemetryChannel || channel == FIPSTelemetryChannel
}

func computeBinaryVersion(versionTxt, revision string) string {
if buildTagOverride != "" {
return buildTagOverride
}
func parseCockroachVersion(versionTxt string) *version.Version {
txt := strings.TrimSuffix(versionTxt, "\n")
v, err := version.Parse(txt)
if err != nil {
panic(fmt.Errorf("could not parse version.txt: %w", err))
}
return v
}

func computeBinaryVersion(
buildTagOverride string, parsedVersionTxt *version.Version, revision string,
) string {
if buildTagOverride != "" {
return buildTagOverride
}
if IsRelease() {
return v.String()
return parsedVersionTxt.String()
}
if revision != "" {
return fmt.Sprintf("%s-dev-%s", v.String(), revision)
return fmt.Sprintf("%s-dev-%s", parsedVersionTxt.String(), revision)
}
return fmt.Sprintf("%s-dev", v.String())
return fmt.Sprintf("%s-dev", parsedVersionTxt.String())
}

// BinaryVersion returns the version prefix, patch number and metadata of the current build.
// BinaryVersion returns the version prefix, patch number and metadata of the
// current build.
func BinaryVersion() string {
if binaryVersionTestingOverride != "" {
return binaryVersionTestingOverride
}
return binaryVersion
}

// BinaryVersionPrefix returns the version prefix of the current build.
func BinaryVersionPrefix() string {
v, err := version.Parse(binaryVersion)
v, err := version.Parse(BinaryVersion())
if err != nil {
return "dev"
}
return fmt.Sprintf("v%d.%d", v.Major(), v.Minor())
}

// BranchReleaseSeries returns tha major and minor in version.txt, without
// allowing for any overrides.
func BranchReleaseSeries() (major, minor int) {
return parsedVersionTxt.Major(), parsedVersionTxt.Minor()
}

func init() {
// Allow tests to override the version.txt contents.
if versionOverride := envutil.EnvOrDefaultString(
Expand Down Expand Up @@ -161,7 +187,7 @@ func GetInfo() Info {
}
return Info{
GoVersion: runtime.Version(),
Tag: binaryVersion,
Tag: BinaryVersion(),
Time: utcTime,
Revision: rev,
CgoCompiler: cgoCompiler,
Expand All @@ -178,9 +204,9 @@ func GetInfo() Info {
// TestingOverrideVersion allows tests to override the binary version
// reported by cockroach.
func TestingOverrideVersion(v string) func() {
prevBinaryVersion := binaryVersion
binaryVersion = v
return func() { binaryVersion = prevBinaryVersion }
prevOverride := binaryVersionTestingOverride
binaryVersionTestingOverride = v
return func() { binaryVersionTestingOverride = prevOverride }
}

// MakeIssueURL produces a URL to a CockroachDB issue.
Expand Down
5 changes: 3 additions & 2 deletions pkg/build/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func TestComputeBinaryVersion(t *testing.T) {
defer func() { typ = oldBuildType }()

if tc.panicExpected {
require.Panics(t, func() { computeBinaryVersion(tc.versionTxt, tc.revision) })
require.Panics(t, func() { parseCockroachVersion(tc.versionTxt) })
} else {
actualVersion := computeBinaryVersion(tc.versionTxt, tc.revision)
v := parseCockroachVersion(tc.versionTxt)
actualVersion := computeBinaryVersion("" /* buildTagOverride */, v, tc.revision)
require.Equal(t, tc.expectedVersion, actualVersion)
}
})
Expand Down

0 comments on commit a65ede5

Please sign in to comment.