Skip to content

Commit

Permalink
Always get commit from build info, always get date and version from l…
Browse files Browse the repository at this point in the history
…dflag
  • Loading branch information
KnVerey committed Feb 1, 2023
1 parent c5cb90b commit 2950321
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ $(MYGOBIN)/pluginator:
# Build from local source.
$(MYGOBIN)/kustomize: build-kustomize-api
cd kustomize; \
go install -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
-X sigs.k8s.io/kustomize/api/provenance.version=(devel)" \
go install -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
.

kustomize: $(MYGOBIN)/kustomize
Expand Down
4 changes: 2 additions & 2 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
include ../Makefile-modules.mk

test:
go test -v -timeout 45m -cover ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=(test)"
go test -v -timeout 45m -cover ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=2023-01-31T23:38:41Z -X sigs.k8s.io/kustomize/api/provenance.version=(test)"

build:
go build ./...
go build -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")" ./...

generate: $(MYGOBIN)/k8scopy $(MYGOBIN)/stringer
go generate ./...
25 changes: 13 additions & 12 deletions api/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import (
"strings"
)

// These variables are set at build time using ldflags.
//
//nolint:gochecknoglobals
var (
version = "unknown"
// sha1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// 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"
)

// This default value, (devel), matches
// the value debug.BuildInfo uses for an unset main module version.
const developmentVersion = "(devel)"

// Provenance holds information about the build of an executable.
type Provenance struct {
// Version of the kustomize binary.
Expand All @@ -40,7 +45,7 @@ func GetProvenance() Provenance {
p := Provenance{
BuildDate: buildDate,
Version: version,
GitCommit: gitCommit,
GitCommit: "unknown",
GoOs: runtime.GOOS,
GoArch: runtime.GOARCH,
GoVersion: runtime.Version(),
Expand All @@ -50,15 +55,11 @@ func GetProvenance() Provenance {
return p
}

// override with values from BuildInfo
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if p.Version == "(devel)" {
p.GitCommit = setting.Value
}
case "GOOS":
p.GoOs = setting.Value
// For now, the git commit is the only information of interest.
// We could consider adding other info such as the commit date in the future.
if setting.Key == "vcs.revision" {
p.GitCommit = setting.Value
}
}
return p
Expand Down
11 changes: 8 additions & 3 deletions api/provenance/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
package provenance_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/api/provenance"
)

const expectedBuildDateFromLdFlag = "2023-01-31T23:38:41Z"
const expectedVersionFromLdFlag = "(test)"

func TestGetProvenance(t *testing.T) {
p := provenance.GetProvenance()
// These are not set during go test: https://github.com/golang/go/issues/33976
// These are set by ldflags in our Makefile
assert.Equal(t, "(test)", p.Version)
assert.Equal(t, "unknown", p.BuildDate)
assert.Equal(t, expectedBuildDateFromLdFlag, p.BuildDate)
// This comes from BuildInfo, which is not set during go test: https://github.com/golang/go/issues/33976
assert.Equal(t, "unknown", p.GitCommit)

// These are set properly during go test
Expand All @@ -35,7 +40,7 @@ func TestProvenance_Short(t *testing.T) {
func TestProvenance_Full(t *testing.T) {
p := provenance.GetProvenance()
// Most values are not set during go test: https://github.com/golang/go/issues/33976
assert.Contains(t, p.Full(), "{Version:(test) GitCommit:unknown BuildDate:unknown")
assert.Contains(t, p.Full(), fmt.Sprintf("{Version:%s GitCommit:unknown BuildDate:%s", expectedVersionFromLdFlag, expectedBuildDateFromLdFlag))
assert.Regexp(t, "GoOs:\\w+ GoArch:\\w+ GoVersion:go1", p.Full())
}

Expand Down
1 change: 0 additions & 1 deletion kustomize.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ADD . /build/
WORKDIR /build/kustomize
RUN CGO_ENABLED=0 GO111MODULE=on go build \
-ldflags="-s -X sigs.k8s.io/kustomize/api/provenance.version=${VERSION} \
-X sigs.k8s.io/kustomize/api/provenance.gitCommit=${COMMIT} \
-X sigs.k8s.io/kustomize/api/provenance.buildDate=${DATE}"

# only copy binary
Expand Down
3 changes: 3 additions & 0 deletions kustomize/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (o *Options) Run() error {
if o.Short {
fmt.Fprintln(o.Writer, provenance.GetProvenance().Short())
} else {
fmt.Fprintln(os.Stderr, "WARNING: This version information is deprecated and "+
"will be replaced with the output from kustomize version --short. "+
"Use --output=yaml|json to get the full version.")
fmt.Fprintln(o.Writer, provenance.GetProvenance().Full())
}
case "yaml":
Expand Down
1 change: 0 additions & 1 deletion releasing/run-goreleaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ builds:
ldflags: >
-s
-X sigs.k8s.io/kustomize/api/provenance.version={{.Version}}
-X sigs.k8s.io/kustomize/api/provenance.gitCommit={{.Commit}}
-X sigs.k8s.io/kustomize/api/provenance.buildDate={{.Date}}
goos:
Expand Down

0 comments on commit 2950321

Please sign in to comment.