Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Get version from the BuildInfo.Main.Version if not found in deps and build flag #5846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ site/.hugo_build.lock

# goreleaser artifacts
**/dist/
/output/
40 changes: 31 additions & 9 deletions api/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ var (
// During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
version = developmentVersion
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = "unknown"
buildDate = unknown
)

// This default value, (devel), matches
// the value debug.BuildInfo uses for an unset main module version.
const developmentVersion = "(devel)"
const (
// This default value, (devel), matches
// 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.
type Provenance struct {
Expand All @@ -47,7 +56,7 @@ func GetProvenance() Provenance {
p := Provenance{
BuildDate: buildDate,
Version: version,
GitCommit: "unknown",
GitCommit: unknown,
GoOs: runtime.GOOS,
GoArch: runtime.GOARCH,
GoVersion: runtime.Version(),
Expand All @@ -62,24 +71,37 @@ func GetProvenance() Provenance {
// We could consider adding other info such as the commit date in the future.
if setting.Key == "vcs.revision" {
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 {
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
if dep.Version != "devel" {
if dep != nil && dep.Path == ModulePath {
if dep.Version == developmentVersion {
continue
}
v, err := GetMostRecentTag(*dep)
if err != nil {
fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
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) {
Expand Down
50 changes: 49 additions & 1 deletion api/provenance/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,60 @@ func TestProvenance_Semver(t *testing.T) {

func mockModule(version string) debug.Module {
return debug.Module{
Path: "sigs.k8s.io/kustomize/kustomize/v5",
Path: provenance.ModulePath,
Version: version,
Replace: nil,
}
}

func mockBuildInfo(mainVersion, depsVersion string) *debug.BuildInfo {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @koba1t
Could you please review it.
Thanks in advance

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) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion releasing/create-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function build_kustomize_binary {
release_dir=$2
echo "build release artifacts to $release_dir"

mkdir -p "output"
mkdir -p output
# build date in ISO8601 format
build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
for os in linux darwin windows; do
Expand Down
Loading