-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Update Versioning to Improve Output #5000
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-sigs:master
from
cailynse:improve-version-output-4617
Feb 1, 2023
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
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 set by ldflags in our Makefile | ||
assert.Equal(t, "(test)", p.Version) | ||
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 | ||
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(), fmt.Sprintf("{Version:%s GitCommit:unknown BuildDate:%s", expectedVersionFromLdFlag, expectedBuildDateFromLdFlag)) | ||
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,30 +4,86 @@ | |
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(os.Stderr, "WARNING: This version information is deprecated and "+ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, we could be less conservative and replace it now with the major version bump. |
||
"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": | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically, this is already breaking. Alternatively, we could either leave it as is (and update the deprecation notice on the main format), or go ahead and remove it in v5 without deprecation. Wdyt @natasha41575 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will leave this to @natasha41575