mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
fix: Get version from the BuildInfo.Main.Version if not found in deps and build flag
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -31,3 +31,4 @@ site/.hugo_build.lock
|
|||||||
|
|
||||||
# goreleaser artifacts
|
# goreleaser artifacts
|
||||||
**/dist/
|
**/dist/
|
||||||
|
/output/
|
||||||
|
|||||||
@@ -19,12 +19,21 @@ var (
|
|||||||
// During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
|
// During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
|
||||||
version = developmentVersion
|
version = developmentVersion
|
||||||
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||||
buildDate = "unknown"
|
buildDate = unknown
|
||||||
)
|
)
|
||||||
|
|
||||||
// This default value, (devel), matches
|
const (
|
||||||
// the value debug.BuildInfo uses for an unset main module version.
|
// This default value, (devel), matches
|
||||||
const developmentVersion = "(devel)"
|
// the value debug.BuildInfo uses for an unset main module version.
|
||||||
|
developmentVersion = "(devel)"
|
||||||
|
|
||||||
|
// ModulePath is kustomize module path, defined in kustomize/go.mod
|
||||||
|
ModulePath = "sigs.k8s.io/kustomize/kustomize/v5"
|
||||||
|
|
||||||
|
// This is default value, unknown, substituted when
|
||||||
|
// the value can't be determined from debug.BuildInfo.
|
||||||
|
unknown = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
// Provenance holds information about the build of an executable.
|
// Provenance holds information about the build of an executable.
|
||||||
type Provenance struct {
|
type Provenance struct {
|
||||||
@@ -47,7 +56,7 @@ func GetProvenance() Provenance {
|
|||||||
p := Provenance{
|
p := Provenance{
|
||||||
BuildDate: buildDate,
|
BuildDate: buildDate,
|
||||||
Version: version,
|
Version: version,
|
||||||
GitCommit: "unknown",
|
GitCommit: unknown,
|
||||||
GoOs: runtime.GOOS,
|
GoOs: runtime.GOOS,
|
||||||
GoArch: runtime.GOARCH,
|
GoArch: runtime.GOARCH,
|
||||||
GoVersion: runtime.Version(),
|
GoVersion: runtime.Version(),
|
||||||
@@ -62,12 +71,20 @@ func GetProvenance() Provenance {
|
|||||||
// We could consider adding other info such as the commit date in the future.
|
// We could consider adding other info such as the commit date in the future.
|
||||||
if setting.Key == "vcs.revision" {
|
if setting.Key == "vcs.revision" {
|
||||||
p.GitCommit = setting.Value
|
p.GitCommit = setting.Value
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
p.Version = FindVersion(info, p.Version)
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindVersion searches for a version in the depth of dependencies including replacements,
|
||||||
|
// otherwise, it tries to get version from debug.BuildInfo Main.
|
||||||
|
func FindVersion(info *debug.BuildInfo, version string) string {
|
||||||
for _, dep := range info.Deps {
|
for _, dep := range info.Deps {
|
||||||
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
|
if dep != nil && dep.Path == ModulePath {
|
||||||
if dep.Version != "devel" {
|
if dep.Version == developmentVersion {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
v, err := GetMostRecentTag(*dep)
|
v, err := GetMostRecentTag(*dep)
|
||||||
@@ -75,11 +92,16 @@ func GetProvenance() Provenance {
|
|||||||
fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
|
fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
p.Version = v
|
|
||||||
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return p
|
if version == developmentVersion && info.Main.Version != "" {
|
||||||
|
return info.Main.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
return version
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMostRecentTag(m debug.Module) (string, error) {
|
func GetMostRecentTag(m debug.Module) (string, error) {
|
||||||
|
|||||||
@@ -49,12 +49,60 @@ func TestProvenance_Semver(t *testing.T) {
|
|||||||
|
|
||||||
func mockModule(version string) debug.Module {
|
func mockModule(version string) debug.Module {
|
||||||
return debug.Module{
|
return debug.Module{
|
||||||
Path: "sigs.k8s.io/kustomize/kustomize/v5",
|
Path: provenance.ModulePath,
|
||||||
Version: version,
|
Version: version,
|
||||||
Replace: nil,
|
Replace: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mockBuildInfo(mainVersion, depsVersion string) *debug.BuildInfo {
|
||||||
|
module := mockModule(depsVersion)
|
||||||
|
|
||||||
|
return &debug.BuildInfo{
|
||||||
|
Main: debug.Module{
|
||||||
|
Version: mainVersion,
|
||||||
|
},
|
||||||
|
Deps: []*debug.Module{
|
||||||
|
&module,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindVersion(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
version string
|
||||||
|
buildInfo *debug.BuildInfo
|
||||||
|
expectedVersion string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "The version from LD_FLAGS is not overridden by main and dependencies versions",
|
||||||
|
version: "v2.3.4",
|
||||||
|
buildInfo: mockBuildInfo("v1.2.3", "(devel)"),
|
||||||
|
expectedVersion: "v2.3.4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "The version from LD_FLAGS is overridden by the main version",
|
||||||
|
version: "(devel)",
|
||||||
|
buildInfo: mockBuildInfo("v1.2.3", "(devel)"),
|
||||||
|
expectedVersion: "v1.2.3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "The version from LD_FLAGS is overridden by the version from dependencies",
|
||||||
|
version: "(devel)",
|
||||||
|
buildInfo: mockBuildInfo("v1.2.3", "v1.2.3-0.20210101010101-abcdefabcdef"),
|
||||||
|
expectedVersion: "v1.2.2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
version := provenance.FindVersion(tt.buildInfo, tt.version)
|
||||||
|
assert.Equal(t, tt.expectedVersion, version)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetMostRecentTag(t *testing.T) {
|
func TestGetMostRecentTag(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ function build_kustomize_binary {
|
|||||||
release_dir=$2
|
release_dir=$2
|
||||||
echo "build release artifacts to $release_dir"
|
echo "build release artifacts to $release_dir"
|
||||||
|
|
||||||
mkdir -p "output"
|
mkdir -p output
|
||||||
# build date in ISO8601 format
|
# build date in ISO8601 format
|
||||||
build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||||
for os in linux darwin windows; do
|
for os in linux darwin windows; do
|
||||||
|
|||||||
Reference in New Issue
Block a user