-
Notifications
You must be signed in to change notification settings - Fork 38.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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,17 +17,26 @@ limitations under the License. | |
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/spf13/cobra" | ||
|
||
apimachineryversion "k8s.io/apimachinery/pkg/version" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/util/i18n" | ||
"k8s.io/kubernetes/pkg/version" | ||
) | ||
|
||
type Version struct { | ||
ClientVersion *apimachineryversion.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` | ||
ServerVersion *apimachineryversion.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` | ||
} | ||
|
||
var ( | ||
version_example = templates.Examples(` | ||
# Print the client and server versions for the current context | ||
|
@@ -47,36 +56,75 @@ 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.") | ||
cmd.Flags().String("output", "", "output format, options available are yaml and json") | ||
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()) | ||
if cmdutil.GetFlagBool(cmd, "short") { | ||
v = version.Get().GitVersion | ||
var serverVersion *apimachineryversion.Info = nil | ||
var serverErr error = nil | ||
vo := Version{nil, nil} | ||
|
||
clientVersion := version.Get() | ||
vo.ClientVersion = &clientVersion | ||
|
||
if !cmdutil.GetFlagBool(cmd, "client") { | ||
serverVersion, serverErr = retrieveServerVersion(f) | ||
vo.ServerVersion = serverVersion | ||
} | ||
|
||
fmt.Fprintf(out, "Client Version: %s\n", v) | ||
if cmdutil.GetFlagBool(cmd, "client") { | ||
return nil | ||
switch of := cmdutil.GetFlagString(cmd, "output"); of { | ||
case "": | ||
if cmdutil.GetFlagBool(cmd, "short") { | ||
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. Maybe check outside of the switch if both 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. I dont think there would be much gained by adding an error if both flags are used. The suggestion goes back to my disagreement regarding expected/unexpected behavior using flags i had prior, please look at the history, regarding how it is implemented now. If you feel strongly about this, I think it would be best we get together and discuss it in detail and perhaps discuss some enforcement throughout the code, or what exists atm. |
||
fmt.Fprintf(out, "Client Version: %s\n", clientVersion.GitVersion) | ||
|
||
if serverVersion != nil { | ||
fmt.Fprintf(out, "Server Version: %s\n", serverVersion.GitVersion) | ||
} | ||
} else { | ||
fmt.Fprintf(out, "Client Version: %s\n", fmt.Sprintf("%#v", clientVersion)) | ||
|
||
if serverVersion != nil { | ||
fmt.Fprintf(out, "Server Version: %s\n", fmt.Sprintf("%#v", *serverVersion)) | ||
} | ||
} | ||
case "yaml": | ||
y, err := yaml.Marshal(&vo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintln(out, string(y)) | ||
case "json": | ||
y, err := json.Marshal(&vo) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(out, string(y)) | ||
default: | ||
return errors.New("invalid output format: " + of) | ||
|
||
} | ||
|
||
discoveryclient, err := f.DiscoveryClient() | ||
if err != nil { | ||
return err | ||
if serverErr != nil { | ||
return serverErr | ||
} | ||
|
||
serverVersion, err := discoveryclient.ServerVersion() | ||
return nil | ||
} | ||
|
||
func retrieveServerVersion(f cmdutil.Factory) (*apimachineryversion.Info, error) { | ||
discoveryClient, err := f.DiscoveryClient() | ||
|
||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
|
||
v = fmt.Sprintf("%#v", *serverVersion) | ||
if cmdutil.GetFlagBool(cmd, "short") { | ||
v = serverVersion.GitVersion | ||
serverVersion, err := discoveryClient.ServerVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fmt.Fprintf(out, "Server Version: %s\n", v) | ||
return nil | ||
return serverVersion, 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.
this file is for generic test helper funcs, not specific tests
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 function isnt a test, its a helper function. I suppose i can rename it to make that clear.