Skip to content

Commit

Permalink
Update Versioning to Improve Output
Browse files Browse the repository at this point in the history
  • Loading branch information
Cailyn Edwards committed Jan 27, 2023
1 parent af3e8ee commit 48bacc7
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ $(MYGOBIN)/pluginator:
# Build from local source.
$(MYGOBIN)/kustomize: build-kustomize-api
cd kustomize; \
go install .
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)" \
.

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=v444.333.222"
go test -v -timeout 45m -cover ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=(test)"

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

generate: $(MYGOBIN)/k8scopy $(MYGOBIN)/stringer
go generate ./...
4 changes: 2 additions & 2 deletions api/krusty/managedbylabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const expected = `apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/managed-by: kustomize-v444.333.222
app.kubernetes.io/managed-by: kustomize-(test)
name: myService
spec:
ports:
- port: 7002
`

// This test may failed when running on package tests using the go command because `v444.333.222` is set on makefile.
// This test may fail when running on package tests using the go command because `(test)` is set on makefile.
func TestAddManagedbyLabel(t *testing.T) {
tests := []struct {
kustFile string
Expand Down
59 changes: 37 additions & 22 deletions api/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,72 @@ package provenance
import (
"fmt"
"runtime"
"runtime/debug"
"strings"
)

//nolint:gochecknoglobals
var (
version = "unknown"
// sha1 from git, output of $(git rev-parse HEAD)
gitCommit = "$Format:%H$"
gitCommit = "unknown"
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = "1970-01-01T00:00:00Z"
goos = runtime.GOOS
goarch = runtime.GOARCH
buildDate = "unknown"
)

// Provenance holds information about the build of an executable.
type Provenance struct {
// Version of the kustomize binary.
Version string `json:"version,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
// GitCommit is a git commit
GitCommit string `json:"gitCommit,omitempty"`
GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"`
// BuildDate is date of the build.
BuildDate string `json:"buildDate,omitempty"`
BuildDate string `json:"buildDate,omitempty" yaml:"buildDate,omitempty"`
// GoOs holds OS name.
GoOs string `json:"goOs,omitempty"`
GoOs string `json:"goOs,omitempty" yaml:"goOs,omitempty"`
// GoArch holds architecture name.
GoArch string `json:"goArch,omitempty"`
GoArch string `json:"goArch,omitempty" yaml:"goArch,omitempty"`
// GoVersion holds Go version.
GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
}

// GetProvenance returns an instance of Provenance.
func GetProvenance() Provenance {
return Provenance{
version,
gitCommit,
buildDate,
goos,
goarch,
p := Provenance{
BuildDate: buildDate,
Version: version,
GitCommit: gitCommit,
GoOs: runtime.GOOS,
GoArch: runtime.GOARCH,
GoVersion: runtime.Version(),
}
info, ok := debug.ReadBuildInfo()
if !ok {
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
}
}
return p
}

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

// Short returns the shortened provenance stamp.
// Short returns the semantic version.
func (v Provenance) Short() string {
return fmt.Sprintf(
"%v",
Provenance{
Version: v.Version,
BuildDate: v.BuildDate,
})
return v.Semver()
}

// Semver returns the semantic version of kustomize.
Expand Down
46 changes: 46 additions & 0 deletions api/provenance/provenance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package provenance_test

import (
"testing"

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

func TestGetProvenance(t *testing.T) {
p := provenance.GetProvenance()
// These are not set during go test: https://github.com/golang/go/issues/33976
assert.Equal(t, "(test)", p.Version)
assert.Equal(t, "unknown", p.BuildDate)
assert.Equal(t, "unknown", p.GitCommit)

// These are set properly during go test
assert.NotEmpty(t, p.GoArch)
assert.NotEmpty(t, p.GoOs)
assert.Contains(t, p.GoVersion, "go1.")
}

func TestProvenance_Short(t *testing.T) {
p := provenance.GetProvenance()
// The version not set during go test, so this comes from an ldflag: https://github.com/golang/go/issues/33976
assert.Equal(t, "(test)", p.Short())

p.Version = "kustomize/v4.11.12"
assert.Equal(t, "v4.11.12", p.Short())
}

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.Regexp(t, "GoOs:\\w+ GoArch:\\w+ GoVersion:go1", p.Full())
}

func TestProvenance_Semver(t *testing.T) {
p := provenance.GetProvenance()
// The version not set during go test
assert.Equal(t, "(test)", p.Semver())

p.Version = "kustomize/v4.11.12"
assert.Equal(t, "v4.11.12", p.Semver())
}
69 changes: 61 additions & 8 deletions kustomize/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,83 @@
package version

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/provenance"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

type Options struct {
Short bool
Output string
Writer io.Writer
}

// NewCmdVersion makes a new version command.
func NewCmdVersion(w io.Writer) *cobra.Command {
var short bool

o := NewOptions(w)
versionCmd := cobra.Command{
Use: "version",
Short: "Prints the kustomize version",
Example: `kustomize version`,
Run: func(cmd *cobra.Command, args []string) {
if short {
fmt.Fprintln(w, provenance.GetProvenance().Short())
} else {
fmt.Fprintln(w, provenance.GetProvenance().Full())
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Validate(args); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}
return nil
},
}

versionCmd.Flags().BoolVar(&short, "short", false, "short form")
versionCmd.Flags().BoolVar(&o.Short, "short", false, "short form")
_ = versionCmd.Flags().MarkDeprecated("short", "and will be removed in the future. The --short output will become the default.")
versionCmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
return &versionCmd
}

func NewOptions(w io.Writer) *Options {
if w == nil {
w = io.Writer(os.Stdout)
}
return &Options{Writer: w}
}

func (o *Options) Validate(_ []string) error {
if o.Short {
if o.Output != "" {
return fmt.Errorf("--short and --output are mutually exclusive")
}
}
return nil
}

func (o *Options) Run() error {
switch o.Output {
case "":
if o.Short {
fmt.Fprintln(o.Writer, provenance.GetProvenance().Short())
} else {
fmt.Fprintln(o.Writer, provenance.GetProvenance().Full())
}
case "yaml":
marshalled, err := yaml.Marshal(provenance.GetProvenance())
if err != nil {
return errors.WrapPrefixf(err, "marshalling provenance to yaml")
}
fmt.Fprintln(o.Writer, string(marshalled))
case "json":
marshalled, err := json.MarshalIndent(provenance.GetProvenance(), "", " ")
if err != nil {
return errors.WrapPrefixf(err, "marshalling provenance to json")
}
fmt.Fprintln(o.Writer, string(marshalled))
}
return nil
}

0 comments on commit 48bacc7

Please sign in to comment.