-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
add --output flag to kubeadm version
#43850
add --output flag to kubeadm version
#43850
Conversation
Hi @xilabao. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
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.
I'd like some changes to this PR thanks
cmd/kubeadm/app/cmd/version.go
Outdated
} | ||
fmt.Fprintln(out, string(y)) | ||
default: | ||
return errors.New("invalid output format: " + of) |
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.
nit, use fmt.Errorf
like all other errors
cmd/kubeadm/app/cmd/version.go
Outdated
if cmdutil.GetFlagBool(cmd, "short") { | ||
fmt.Fprintf(out, "Kubeadm Version: %s\n", clientVersion.GitVersion) | ||
} else { | ||
fmt.Fprintf(out, "Kubeadm Version: %s\n", fmt.Sprintf("%#v", clientVersion)) |
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.
nit: I prefer kubeadm version: foo
. We don't write Kubeadm anywhere else, I've only seen it lowercased
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.
also, why don't just: fmt.Fprintf(out, "kubeadm version: %#v\n", *v.ClientVersion)
?
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { | ||
ClientVersion *apimachineryversion.Info `json:"kubeadmVersion,omitempty" yaml:"kubeadmVersion,omitempty"` |
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.
Why not clientVersion
in json? I don't think the extra yaml field is required.
cmd/kubeadm/app/cmd/version.go
Outdated
@@ -35,10 +44,39 @@ func NewCmdVersion(out io.Writer) *cobra.Command { | |||
kubeadmutil.CheckErr(err) | |||
}, | |||
} | |||
cmd.Flags().BoolP("short", "", false, "Print just the version number.") |
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.
only print the version (tag|number)
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { |
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.
godoc comment
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.
ping, please add a godoc comment about this struct
cmd/kubeadm/app/cmd/version.go
Outdated
return cmd | ||
} | ||
|
||
func RunVersion(out io.Writer, cmd *cobra.Command) error { | ||
fmt.Fprintf(out, "kubeadm version: %#v\n", version.Get()) | ||
v := Version{} |
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.
v := Version{
ClientVersion: &version.Get(),
}
and then use v.ClientVersion
kubeadm version
e0aa692
to
78b017b
Compare
Done. @luxas |
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { |
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.
ping, please add a godoc comment about this struct
cmd/kubeadm/app/cmd/version.go
Outdated
switch of := cmdutil.GetFlagString(cmd, "output"); of { | ||
case "": | ||
if cmdutil.GetFlagBool(cmd, "short") { | ||
fmt.Fprintf(out, "kubeadm version: %s\n", v.clientVersion.GitVersion) |
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.
if the user really wants the short version, we should probably drop kubeadm version
here
cmd/kubeadm/app/cmd/version.go
Outdated
@@ -35,10 +44,39 @@ func NewCmdVersion(out io.Writer) *cobra.Command { | |||
kubeadmutil.CheckErr(err) | |||
}, | |||
} | |||
cmd.Flags().BoolP("short", "", false, "only print the version (tag|number)") | |||
cmd.Flags().StringP("output", "o", "", "output format, options available are yaml and json") |
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.
didn't check how kubectl does it, but to be -o short
would be better than two flags
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.
Should we keep the same format with kubectl? I think your solution also make sense.
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { | ||
// clientVersion contains the kubeadm version. | ||
clientVersion *apimachineryversion.Info `json:"kubeadmVersion,omitempty" yaml:"kubeadmVersion,omitempty"` |
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.
ping: why not clientVersion
here? Also the yaml statement is unneeded, we only do json everywhere else.
(the yaml parser will work anyway)
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.
someone else may want the yaml statement #43750
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.
But the yaml statement here defaults to the json value; the duplication here is unneded.
I think this should just be: clientVersion *apimachineryversion.Info
json:"clientVersion"`
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.
please address the three comments that are left
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { | ||
// clientVersion contains the kubeadm version. | ||
clientVersion *apimachineryversion.Info `json:"kubeadmVersion,omitempty" yaml:"kubeadmVersion,omitempty"` |
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.
But the yaml statement here defaults to the json value; the duplication here is unneded.
I think this should just be: clientVersion *apimachineryversion.Info
json:"clientVersion"`
78b017b
to
cc27c66
Compare
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.
(but hmm, I'd love some tests for this, please CLI tests for this in /approve |
@xilabao Can you add tests and I'll apply my lgtm. |
@k8s-bot ok to test |
434dec8
to
38e331e
Compare
@k8s-bot verify test this |
Derek: Preferably, yes. What's your take?
…
|
38e331e
to
93ab6fd
Compare
I prefer |
6101c6c
to
6039f4c
Compare
@xilabao I think this is shaping up nicely. Do you mind writing tests that capture the output of running the test cmd and running a regex to make sure the output is sane? Something like this: https://github.com/kubernetes/kubernetes/blob/master/cmd/kubeadm/test/cmd/token_test.go |
6039f4c
to
d26182f
Compare
@dmmcquay PTAL |
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.
This looks much better. Just small comment about the tests.
cmd/kubeadm/test/cmd/version_test.go
Outdated
const ( | ||
ShortExpectedRegex = "^v.+\n$" | ||
JsonExpectedRegex = "^{\"kubeadmVersion\":{\"major\":\".+\",\"minor\":\".+\",\"gitVersion\":\".+\",\"gitCommit\":\".+\",\"gitTreeState\":\".+\",\"buildDate\":\".+\",\"goVersion\":\".+\",\"compiler\":\".+\",\"platform\":\".+\"}}\n$" | ||
YamlExpectedRegex = "^kubeadmVersion:\n\\s+buildDate:.+\n\\s+compiler:.+\n\\s+gitCommit:.+\n\\s+gitTreeState:.+\n\\s+gitVersion:.+\n\\s+goVersion:.+\n\\s+major:.+\n\\s+minor:.+\n\\s+platform:.+\n\n$" |
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.
I think instead of having a regex for json and yaml, you could just marshal it and check it that way? The json and yaml regex seems a bit intense.
d26182f
to
bb15fec
Compare
@dmmcquay PTAL |
/lgtm thanks for going back and forth on this one. |
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.
LGTM other than kubeadmVersion, I still think that should be clientVersion
cc @kubernetes/sig-cli-pr-reviews
cmd/kubeadm/app/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
// Version provides the version information of kubeadm. | ||
type Version struct { | ||
ClientVersion *apimachineryversion.Info `json:"kubeadmVersion"` |
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.
still I don't understand why this is kubeadmVersion
instead of clientVersion
?
kubeadm is a client and I think it's easiest if it has the same schema as kubectl
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.
kubectl version -o json
use clientVersion
to keep consistent with kubectl version
. I saw the output of kubeadm version
is kubeadm version...
Should we change all of them?
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.
I mean that I think this line should be
ClientVersion *apimachineryversion.Info `json:"clientVersion"`
bb15fec
to
3719840
Compare
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.
LGTM, thanks!
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dmmcquay, luxas, xilabao
Needs approval from an approver in each of these OWNERS Files:
You can indicate your approval by writing |
Automatic merge from submit-queue |
ref to kubectl #39858