-
Notifications
You must be signed in to change notification settings - Fork 39.8k
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
addressing issue #39427 adding a flag --output to 'kubectl version' #39858
addressing issue #39427 adding a flag --output to 'kubectl version' #39858
Conversation
There seems to be some issue upstream, never had these tests fail, local shows things working properly. |
7e5e38a
to
5dccc5c
Compare
@kubernetes/api-reviewers could i get some available eyes here? |
pkg/version/version.go
Outdated
GoVersion string `json:"goVersion"` | ||
Compiler string `json:"compiler"` | ||
Platform string `json:"platform"` | ||
Major string `json:"major,omitempty" yaml:"major"` |
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.
don't change this, this is being returned from the /version
API endpoint
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.
you mean all these vars? do I make the changes there then?
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.
no, revert this change. it's ok to print empty strings for these fields
pkg/kubectl/cmd/version.go
Outdated
} | ||
|
||
type ClientAndServerVersionObj struct { | ||
ClientVersion version.Info `json:"Client Version" yaml:"Client 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.
english language keys is odd here... I'd expect something like client
and server
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 totally agree, was trying to keep it inline with the original keys. I dont want too much variation between what was expected before and what they will get now. What do you think?
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.
for programmatic output, I would go short and lowercase
pkg/kubectl/cmd/version.go
Outdated
@@ -46,36 +59,92 @@ func NewCmdVersion(f cmdutil.Factory, out io.Writer) *cobra.Command { | |||
} | |||
cmd.Flags().BoolP("client", "c", false, "Client version only (no server required).") | |||
cmd.Flags().BoolP("short", "", false, "Print just the version number.") | |||
// default behavior is std. | |||
cmd.Flags().String("output", "std", "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.
leave default empty (not "std") for normal human-readable output?
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 was going back and forth on this. I settled on std simply because i thought empty string would be a bit more difficult to understand? Not sure, but i'll change it to empty string if you are ok with that?
pkg/kubectl/cmd/version.go
Outdated
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
// We include both struct objects because json marshal does not handle the idea | ||
// of empty struct objects well and will include and empty server object in results |
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.
really? omitempty doesn't work with an empty server 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.
it doesnt. You can try it out, i wasnt happy with that either.
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.
you can make it a pointer to the struct and set it to nil when unused, then
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.
@liggitt ahhh, good point, will try this.
pkg/kubectl/cmd/version.go
Outdated
v = serverVersion.GitVersion | ||
func printRightFormat(out io.Writer, outputFormat string, vo interface{}) error { | ||
if outputFormat == "yaml" { | ||
fmt.Println(vo) |
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 doesn't seem right
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.
:face_palm: arggghhh sorry about this.
@liggitt left some questions there for you just to get some direction, thank you soooo much for taking your time to review, much appreciated! |
@liggitt one more question, have you taken a look at the issue ticket, just wanted your opinion on the output now vs older format. |
@liggitt made changes requested, please have another look, and thank you again! |
cmd.Flags().MarkShorthandDeprecated("client", "please use --client instead.") | ||
return cmd | ||
} | ||
|
||
func RunVersion(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { | ||
v := fmt.Sprintf("%#v", version.Get()) |
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 would order it like this:
- get the client and (optionally) server versions
- build the VersionObj struct with nil set appropriately
- switch on cmdutil.GetFlagString(cmd, "output") (don't normalize their arg)
- in the "" case, have all the existing output, including short output handling
- in the json/yaml cases, marshal and output
- in the default case, error
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.
That would be the ideal case. However, the "" case makes it a bit difficult because the output is actually label: version.Info{...}
which makes this approach difficult to do. I have to keep the old output because legacy tests etc.. still use this.
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.
Right, in the "" case, include all the old output code
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.
Ok, i think i understand what you are saying... let me try it out. it will most likely be submitted later tonight though, fyi.
pkg/kubectl/cmd/version.go
Outdated
return nil | ||
} | ||
case of == "yaml" || of == "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.
Please avoid duplicated code of generating version info.
ok @liggitt that was fun, take a look when you are ready and thank you again. |
pkg/kubectl/cmd/version.go
Outdated
v := fmt.Sprintf("%#v", version.Get()) | ||
vo := VersionObj{nil, nil} | ||
|
||
cvg := version.Get() | ||
if cmdutil.GetFlagBool(cmd, "short") { |
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 handle short output in the human readable case
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.
Can you clarify why this is the case?
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.
The only point of json/yaml output is to consume specific fields programmatically. Truncating to a single field doesn't make that any easier
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 disagree, regardless of the consumer we should respect the expectations of the flag.
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.
Then disallow short output combined with json/yaml output
pkg/kubectl/cmd/version.go
Outdated
} | ||
serverVersion, err := clientSet.Discovery().ServerVersion() | ||
if err != nil { | ||
return err |
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.
Store this, but don't return it yet. Otherwise a server error prevents us from seeing our client version and is a behavior change
pkg/kubectl/cmd/version.go
Outdated
return err | ||
sv := serverVersion | ||
if cmdutil.GetFlagBool(cmd, "short") { | ||
sv = &version.Info{GitVersion: serverVersion.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.
Same, don't truncate data here. Truncate in short mode only in the human readable case
pkg/kubectl/cmd/version.go
Outdated
switch of := cmdutil.GetFlagString(cmd, "output"); of { | ||
case "": | ||
fmt.Fprintf(out, "Client Version: %s\n", fmt.Sprintf("%#v", *vo.ClientVersion)) | ||
if cmdutil.GetFlagBool(cmd, "client") { |
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.
Can just check whether you have a non nil server version
pkg/kubectl/cmd/version.go
Outdated
} | ||
|
||
fmt.Fprintf(out, "Server Version: %s\n", fmt.Sprintf("%#v", *vo.ServerVersion)) | ||
return nil |
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.
Return server error here if you got 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.
Actually, return server error after the switch statement
Very good points, I'll try to make changes before the end of the day. |
facfbe4
to
72742dc
Compare
f7b3b98
to
c01a5a7
Compare
@adohe could I get the lgtm applied again? Sorry for the trouble. |
@ethernetdan should you remove that donotmerge label and add a 1.7 label instead? also the bot removed the lgtm because of a flake. |
pkg/kubectl/cmd/version.go
Outdated
"fmt" | ||
"io" | ||
|
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 did this get removed. Isn't the import standard
import (
std libs
[blank line]
other people's packages
[blank line]
pkg's from kubernetes
)
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.
@eparis yeah probably got deleted and re-added after some change, ill go ahead and make the format change.
c01a5a7
to
a8eb28c
Compare
pkg/kubectl/cmd/version.go
Outdated
"fmt" | ||
"io" | ||
|
||
"encoding/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.
encoding/json
is part of the standard lib.
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.
ha, i totally missed that during moving things around. thanks for the catch. Could you give this pr and lgtm? i got this dropped after an automatic retest.
… either json or yaml. updating with PR changes requested. latest changes to having short for human readable only, and error cases moved a bit to the end. rebase fixes latest pr. changes. small change moving return nil out of switch. updated the nil check for the error in the humanreadable case. more optimization in humanreadable code. pushed up current test changes, this is purely temporary finished writing tests updated test and function names. changed output extensions from .sh to output. updated version, version struct now just called Version and not VersionObj. made a few changes to testing. fixed testing issues, created better test and cleanup go format change.
a8eb28c
to
a5e6dcb
Compare
@alejandroEsc does this also address #43750? Can you post the output of |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: adohe, alejandroEsc, deads2k, kargakis
Needs approval from an approver in each of these OWNERS Files:
You can indicate your approval by writing |
Those are looking good but I am asking specifically about the output of the command w/o using -o (the default case) |
the default, without any additional output flag, has not changed, the intend here is to keep default behavior and only allow the option to make it human readable if we want it. |
That's what I thought. Thanks for clarifying. cc: @php-coder |
@k8s-bot bazel test this what is going on with bazel? |
Looks like the bazel build is just broken - the upstream busybox dependency no longer exists. cc @mikedanese @spxtr
|
Automatic merge from submit-queue |
Automatic merge from submit-queue add --output flag to `kubeadm version` ref to kubectl #39858
Automatic merge from submit-queue (batch tested with PRs 46210, 48607, 46874, 46598, 49240) Make "kubectl version" json format output more readable. **What this PR does / why we need it**: ##39858 adds a flag --output to `kubectl version`, but the json format output is displayed in one line. It's not so readable. This PR fixes it. and - adds a shorthand for `output` - ~~refactors that: if `--short` is specified, `--output` will be ignored~~ **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #43750 **Special notes for your reviewer**: /cc @php-coder @alejandroEsc **Release note**: ```release-note NONE ```
What this PR does / why we need it:
Addressing Issue #39427 we all
Which issue this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close that issue when PR gets merged): fixes #39427Release note: