Skip to content

Commit

Permalink
Always get commit from build info, always get date from ldflag, use l…
Browse files Browse the repository at this point in the history
…dflag for version only on release
  • Loading branch information
KnVerey committed Jan 31, 2023
1 parent 5230f8f commit 1e1187f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 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
2 changes: 1 addition & 1 deletion api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test:
go test -v -timeout 45m -cover ./... -ldflags "-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 ./...
22 changes: 12 additions & 10 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,12 @@ func GetProvenance() Provenance {
return p
}

// override with values from BuildInfo
for _, setting := range info.Settings {
// 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.
switch setting.Key {
case "vcs.revision":
if p.Version == "(devel)" {
p.GitCommit = setting.Value
}
case "GOOS":
p.GoOs = setting.Value
p.GitCommit = setting.Value
}
}
return p
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
1 change: 1 addition & 0 deletions kustomize/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ 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 1e1187f

Please sign in to comment.