Skip to content

Commit c3e2889

Browse files
author
Cailyn Edwards
committed
Update Versioning to Improve Output
1 parent da5d572 commit c3e2889

File tree

9 files changed

+168
-36
lines changed

9 files changed

+168
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ $(MYGOBIN)/pluginator:
7575
# Build from local source.
7676
$(MYGOBIN)/kustomize: build-kustomize-api
7777
cd kustomize; \
78-
go install .
78+
go install -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")" .
7979

8080
kustomize: $(MYGOBIN)/kustomize
8181

api/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
include ../Makefile-modules.mk
55

66
test:
7-
go test -v -timeout 45m -cover ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222"
7+
go test -v -timeout 45m -cover ./...
88

99
build:
10-
go build -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222" ./...
10+
go build ./...
1111

1212
generate: $(MYGOBIN)/k8scopy $(MYGOBIN)/stringer
1313
go generate ./...

api/api

1.91 MB
Binary file not shown.

api/krusty/managedbylabel_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ const expected = `apiVersion: v1
1313
kind: Service
1414
metadata:
1515
labels:
16-
app.kubernetes.io/managed-by: kustomize-v444.333.222
16+
app.kubernetes.io/managed-by: kustomize-unknown
1717
name: myService
1818
spec:
1919
ports:
2020
- port: 7002
2121
`
2222

23-
// This test may failed when running on package tests using the go command because `v444.333.222` is set on makefile.
23+
// This test may fail when running on package tests using the go command because `unknown` is set on makefile.
2424
func TestAddManagedbyLabel(t *testing.T) {
2525
tests := []struct {
2626
kustFile string

api/provenance/provenance.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,75 @@ package provenance
66
import (
77
"fmt"
88
"runtime"
9+
"runtime/debug"
910
"strings"
1011
)
1112

1213
var (
1314
version = "unknown"
1415
// sha1 from git, output of $(git rev-parse HEAD)
15-
gitCommit = "$Format:%H$"
16+
gitCommit = "unknown"
1617
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
17-
buildDate = "1970-01-01T00:00:00Z"
18-
goos = runtime.GOOS
19-
goarch = runtime.GOARCH
18+
buildDate = "unknown"
2019
)
2120

2221
// Provenance holds information about the build of an executable.
2322
type Provenance struct {
2423
// Version of the kustomize binary.
25-
Version string `json:"version,omitempty"`
24+
Version string `json:"version,omitempty" yaml:"version,omitempty"`
2625
// GitCommit is a git commit
27-
GitCommit string `json:"gitCommit,omitempty"`
26+
GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"`
2827
// BuildDate is date of the build.
29-
BuildDate string `json:"buildDate,omitempty"`
28+
BuildDate string `json:"buildDate,omitempty" yaml:"buildDate,omitempty"`
3029
// GoOs holds OS name.
31-
GoOs string `json:"goOs,omitempty"`
30+
GoOs string `json:"goOs,omitempty" yaml:"goOs,omitempty"`
3231
// GoArch holds architecture name.
33-
GoArch string `json:"goArch,omitempty"`
32+
GoArch string `json:"goArch,omitempty" yaml:"goArch,omitempty"`
33+
// GoVersion holds Go version.
34+
GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
3435
}
3536

3637
// GetProvenance returns an instance of Provenance.
3738
func GetProvenance() Provenance {
38-
return Provenance{
39-
version,
40-
gitCommit,
41-
buildDate,
42-
goos,
43-
goarch,
39+
p := Provenance{
40+
BuildDate: buildDate,
41+
Version: version,
42+
GitCommit: gitCommit,
43+
GoOs: runtime.GOOS,
44+
GoArch: runtime.GOARCH,
45+
GoVersion: runtime.Version(),
4446
}
47+
info, ok := debug.ReadBuildInfo()
48+
if !ok {
49+
return p
50+
}
51+
52+
// override with values from BuildInfo
53+
if info.Main.Version != "" && version == "unknown" {
54+
p.Version = info.Main.Version
55+
}
56+
57+
for _, setting := range info.Settings {
58+
switch setting.Key {
59+
case "vcs.revision":
60+
if p.Version == "(devel)" {
61+
p.GitCommit = setting.Value
62+
}
63+
case "GOOS":
64+
p.GoOs = setting.Value
65+
}
66+
}
67+
return p
4568
}
4669

4770
// Full returns the full provenance stamp.
4871
func (v Provenance) Full() string {
4972
return fmt.Sprintf("%+v", v)
5073
}
5174

52-
// Short returns the shortened provenance stamp.
75+
// Short returns the semantic version.
5376
func (v Provenance) Short() string {
54-
return fmt.Sprintf(
55-
"%v",
56-
Provenance{
57-
Version: v.Version,
58-
BuildDate: v.BuildDate,
59-
})
77+
return v.Semver()
6078
}
6179

6280
// Semver returns the semantic version of kustomize.

api/provenance/provenance_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package provenance_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"sigs.k8s.io/kustomize/api/provenance"
8+
)
9+
10+
func TestGetProvenance(t *testing.T) {
11+
p := provenance.GetProvenance()
12+
// These are not set during go test: https://github.com/golang/go/issues/33976
13+
assert.Equal(t, "unknown", p.Version)
14+
assert.Equal(t, "unknown", p.BuildDate)
15+
assert.Equal(t, "unknown", p.GitCommit)
16+
17+
// These are set properly during go test
18+
assert.NotEmpty(t, p.GoArch)
19+
assert.NotEmpty(t, p.GoOs)
20+
assert.Contains(t, p.GoVersion, "go1.")
21+
}
22+
23+
func TestProvenance_Short(t *testing.T) {
24+
p := provenance.GetProvenance()
25+
// The version not set during go test, so this comes from an ldflag: https://github.com/golang/go/issues/33976
26+
assert.Equal(t, "unknown", p.Short())
27+
28+
p.Version = "kustomize/v4.11.12"
29+
assert.Equal(t, "v4.11.12", p.Short())
30+
}
31+
32+
func TestProvenance_Full(t *testing.T) {
33+
p := provenance.GetProvenance()
34+
// Most values are not set during go test: https://github.com/golang/go/issues/33976
35+
assert.Contains(t, p.Full(), "{Version:unknown GitCommit:unknown BuildDate:unknown")
36+
assert.Regexp(t, "GoOs:\\w+ GoArch:\\w+ GoVersion:go1", p.Full())
37+
}
38+
39+
func TestProvenance_Semver(t *testing.T) {
40+
p := provenance.GetProvenance()
41+
// The version not set during go test
42+
assert.Equal(t, "unknown", p.Semver())
43+
44+
p.Version = "kustomize/v4.11.12"
45+
assert.Equal(t, "v4.11.12", p.Semver())
46+
}

go.work.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,32 @@ github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PL
1010
github.com/googleapis/gnostic v0.1.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
1111
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
1212
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
13+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
14+
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
15+
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
1316
github.com/paulmach/orb v0.1.3/go.mod h1:VFlX/8C+IQ1p6FTRRKzKoOPJnvEtA5G0Veuqwbu//Vk=
1417
github.com/qri-io/starlib v0.4.2-0.20200213133954-ff2e8cd5ef8d/go.mod h1:7DPO4domFU579Ga6E61sB9VFNaniPVwJP5C4bBCu3wA=
1518
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
1619
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
1720
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1821
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
22+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1923
go.starlark.net v0.0.0-20190528202925-30ae18b8564f/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
24+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
2025
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
2126
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
27+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
28+
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
29+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
30+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2231
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2334
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
35+
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
36+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
37+
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
38+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
2439
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
2540
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2641
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

kustomize/commands/version/version.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,83 @@
44
package version
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"io"
10+
"os"
911

1012
"github.com/spf13/cobra"
1113
"sigs.k8s.io/kustomize/api/provenance"
14+
"sigs.k8s.io/kustomize/kyaml/errors"
15+
"sigs.k8s.io/kustomize/kyaml/yaml"
1216
)
1317

18+
type Options struct {
19+
Short bool
20+
Output string
21+
Writer io.Writer
22+
}
23+
1424
// NewCmdVersion makes a new version command.
1525
func NewCmdVersion(w io.Writer) *cobra.Command {
16-
var short bool
17-
26+
o := NewOptions(w)
1827
versionCmd := cobra.Command{
1928
Use: "version",
2029
Short: "Prints the kustomize version",
2130
Example: `kustomize version`,
22-
Run: func(cmd *cobra.Command, args []string) {
23-
if short {
24-
fmt.Fprintln(w, provenance.GetProvenance().Short())
25-
} else {
26-
fmt.Fprintln(w, provenance.GetProvenance().Full())
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
if err := o.Validate(args); err != nil {
33+
return err
34+
}
35+
if err := o.Run(); err != nil {
36+
return err
2737
}
38+
return nil
2839
},
2940
}
3041

31-
versionCmd.Flags().BoolVar(&short, "short", false, "short form")
42+
versionCmd.Flags().BoolVar(&o.Short, "short", false, "short form")
43+
_ = versionCmd.Flags().MarkDeprecated("short", "and will be removed in the future. The --short output will become the default.")
44+
versionCmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
3245
return &versionCmd
3346
}
47+
48+
func NewOptions(w io.Writer) *Options {
49+
if w == nil {
50+
w = io.Writer(os.Stdout)
51+
}
52+
return &Options{Writer: w}
53+
}
54+
55+
func (o *Options) Validate(_ []string) error {
56+
if o.Short {
57+
if o.Output != "" {
58+
return fmt.Errorf("--short and --output are mutually exclusive")
59+
}
60+
}
61+
return nil
62+
}
63+
64+
func (o *Options) Run() error {
65+
switch o.Output {
66+
case "":
67+
if o.Short {
68+
fmt.Fprintln(o.Writer, provenance.GetProvenance().Short())
69+
} else {
70+
fmt.Fprintln(o.Writer, provenance.GetProvenance().Full())
71+
}
72+
case "yaml":
73+
marshalled, err := yaml.Marshal(provenance.GetProvenance())
74+
if err != nil {
75+
return errors.WrapPrefixf(err, "marshalling provenance to yaml")
76+
}
77+
fmt.Fprintln(o.Writer, string(marshalled))
78+
case "json":
79+
marshalled, err := json.MarshalIndent(provenance.GetProvenance(), "", " ")
80+
if err != nil {
81+
return errors.WrapPrefixf(err, "marshalling provenance to json")
82+
}
83+
fmt.Fprintln(o.Writer, string(marshalled))
84+
}
85+
return nil
86+
}

releasing/run-goreleaser.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ builds:
105105
-X sigs.k8s.io/kustomize/api/provenance.buildDate={{.Date}}
106106
107107
goos:
108-
- linux
108+
- linuxlock
109109
- darwin
110110
- windows
111111

0 commit comments

Comments
 (0)