Change:
<!-- Thanks for filing an issue! Before hitting the button, please answer these questions.--Is this a request for help? (If yes, you should use our troubleshooting guide and community support channels, see ): no What keywords did you search in Kubernetes issues before filing this one? (If you have found any duplicates, you should instead reply there.): "kubectl version", "kubectl version" json Is this a BUG REPORT or FEATURE REQUEST? (choose one): Feature request Kubernetes version (use ): $ kubectl version --client Client Version: version.Info{Major:"1", Minor:"4", GitVersion:"v1.4.6", GitCommit:"", GitTreeState:"clean", BuildDate:"2016-11-12T05:22:15Z", GoVersion:"go1.7.1", Compiler:"gc", Platform:"darwin/amd64"} Environment: Cloud provider or hardware configuration: GKE OS (e.g. from /etc/os-release): MacOS 10.12.2 I find it difficult to parse the output of programmatically. I have to use a YAML parser to do it. I'd prefer to use JSON, or to be given just the string and asked to parse it with a regex. This is obviously a relatively low-priority request.
I wouldnt mind helping out on this, if there are no objections?
are you working on this? If not, I would like to work on it.
I plan to, but if i'll keep you posted if i get too delayed.
How can I direct message you?
didnt see this till now, felt bad i had not kept up on this so i decided to just push through it. If my email is not available in my profile, please let me know, otherwise AT gmail would be the best way to contact me.
Thanks!
Included below are some examples. Let me know what you think, my only concern is the the original output of included the key and then the object type which is a little strange. Note the output flag is optional so we don't break previous behavior and contract, which is now baked and part of e2e testing. here are the couple of samples:

Discussion:
done return 1 } <ins> kube::test::version::object_to_file() { name=$1 flags=${2:-""} file=$3 kubectl version $flags | grep "$name Version:" | sed -e s/"$name Version: version.Info{"/'/' -e s/'}'/'/' -e s/', '/','/g -e s/':'/'=/g' -e s/'"'/""/g | tr , 'n' > "${file}" } kube::test::version::json_object_to_file() { flags=$1 file=$2 kubectl version $flags --output json | sed -e s/'"'/''/g -e s/'}'/''/g -e s/'{'/''/g -e s/'clientVersion:'/'clientVersion:,'/ -e s/'serverVersion:'/'serverVersion:,'/ | tr , 'n' > "${file}" } kube::test::version::json_client_server_object_to_file() { flags=$1 name=$2 file=$3 kubectl version $flags --output json | jq -r ".${name}" | sed -e s/'"'/''/g -e s/'}'/''/g -e s/'{'/''/g -e /^$/d -e s/','/''/g  -e s/':'/'='/g > "${file}" } kube::test::version::yaml_object_to_file() { flags=$1 file=$2 kubectl version $flags --output yaml | sed -e s/' '/''/g -e s/'"'/''/g -e /^$/d > "${file}" } kube::test::version::diff_assert() { local original=$1 local comparator=${2:-"eq"} local latest=$3 local diff_msg=${4:-""} local res="" if [ ! -f $original ]; then echo ${bold}${red} echo "FAIL! ${diff_msg}" echo "the file '${original}' does not exit" echo ${reset}${red} caller echo ${reset} return 1 fi if [ ! -f $latest ]; then echo ${bold}${red} echo "FAIL! ${diff_msg}" echo "the file '${latest}' does not exit" echo ${reset}${red} caller echo ${reset} return 1 fi sort ${original} > "${original}.sorted" sort ${latest} > "${latest}.sorted" if [ "$comparator" == "eq" ]; then if [ "$(diff -iwB ${original}.sorted ${latest}.sorted)" == "" ] ; then echo -n ${green} echo "Successful: ${diff_msg}" echo -n ${reset} return 0 else echo ${bold}${red} echo "FAIL! ${diff_msg}" echo "  Expected: " echo "$(cat ${original})" echo "  Got: " echo "$(cat ${latest})" echo ${reset}${red} caller echo ${reset} return 1 fi else if [ ! -z "$(diff -iwB ${original}.sorted ${latest}.sorted)" ] ; then echo -n ${green} echo "Successful: ${diff_msg}" echo -n ${reset} return 0 else echo ${bold}${red} echo "FAIL! ${diff_msg}" echo "  Expected: " echo "$(cat ${original})" echo "  Got: " echo "$(cat ${latest})" echo ${reset}${red} caller echo ${reset} return 1 fi fi } </ins>

kube::log::status "Setup complete" } <ins> ######################################################## # Kubectl version (--short, --client, --output) # ######################################################## run_kubectl_version_tests() { kube::log::status "Testing kubectl version" TEMP="${KUBE_TEMP}" # create version files, one for the client, one for the server. # these are the files we will use to ensure that the remainder output is correct kube::test::version::object_to_file "Client" "" "${TEMP}/client_version_test" kube::test::version::object_to_file "Server" "" "${TEMP}/server_version_test" kube::log::status "Testing kubectl version: check client only output matches expected output" kube::test::version::object_to_file "Client" "--client" "${TEMP}/client_only_version_test" kube::test::version::object_to_file "Client" "--client" "${TEMP}/server_client_only_version_test" kube::test::version::diff_assert "${TEMP}/client_version_test" "eq" "${TEMP}/client_only_version_test" "the flag '--client' shows correct client info" kube::test::version::diff_assert "${TEMP}/server_version_test" "ne" "${TEMP}/server_client_only_version_test" "the flag '--client' correctly has no server version info" kube::log::status "Testing kubectl version: verify json output" kube::test::version::json_client_server_object_to_file "" "clientVersion" "${TEMP}/client_json_version_test" kube::test::version::json_client_server_object_to_file "" "serverVersion" "${TEMP}/server_json_version_test" kube::test::version::diff_assert "${TEMP}/client_version_test" "eq" "${TEMP}/client_json_version_test" "--output json has correct client info" kube::test::version::diff_assert "${TEMP}/server_version_test" "eq" "${TEMP}/server_json_version_test" "--output json has correct server info" kube::log::status "Testing kubectl version: verify json output using additional --client flag does not contain serverVersion" kube::test::version::json_client_server_object_to_file "--client" "clientVersion" "${TEMP}/client_only_json_version_test" kube::test::version::json_client_server_object_to_file "--client" "serverVersion" "${TEMP}/server_client_only_json_version_test" kube::test::version::diff_assert "${TEMP}/client_version_test" "eq" "${TEMP}/client_only_json_version_test" "--client --output json has correct client info" kube::test::version::diff_assert "${TEMP}/server_version_test" "ne" "${TEMP}/server_client_only_json_version_test" "--client --output json has no server info" kube::log::status "Testing kubectl version: compare json output using additional --short flag" kube::test::version::json_client_server_object_to_file "--short" "clientVersion" "${TEMP}/client_short_json_version_test" kube::test::version::json_client_server_object_to_file "--short" "serverVersion" "${TEMP}/server_short_json_version_test" kube::test::version::diff_assert "${TEMP}/client_version_test" "eq" "${TEMP}/client_short_json_version_test" "--short --output client json info is equal to non short result" kube::test::version::diff_assert "${TEMP}/server_version_test" "eq" "${TEMP}/server_short_json_version_test" "--short --output server json info is equal to non short result" kube::log::status "Testing kubectl version: compare json output with yaml output" kube::test::version::json_object_to_file "" "${TEMP}/client_server_json_version_test" kube::test::version::yaml_object_to_file "" "${TEMP}/client_server_yaml_version_test" kube::test::version::diff_assert "${TEMP}/client_server_json_version_test" "eq" "${TEMP}/client_server_yaml_version_test" "--output json/yaml has identical information" } </ins> # Runs all pod related tests. run_pod_tests() { kube::log::status "Testing kubectl(v1:pods)"

kubectl get "${kube_flags[@]}" -f hack/testdata/kubernetes-service.yaml fi <ins> ######################### # Kubectl version # ######################### run_kubectl_version_tests </ins> # Passing no arguments to create is an error ! kubectl create

"//vendor:k8s.io/apimachinery/pkg/util/validation/field", "//vendor:k8s.io/apimachinery/pkg/util/wait", "//vendor:k8s.io/apimachinery/pkg/util/yaml", <ins> "//vendor:k8s.io/apimachinery/pkg/version", </ins> "//vendor:k8s.io/apimachinery/pkg/watch", "//vendor:k8s.io/apiserver/pkg/util/flag", "//vendor:k8s.io/client-go/discovery",

package cmd import ( <ins> "encoding/json" "errors" </ins> "fmt" "io" <ins> "github.com/ghodss/yaml" </ins> "github.com/spf13/cobra" <ins> apimachineryversion "k8s.io/apimachinery/pkg/version" </ins> "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" ) <ins> type Version struct { ClientVersion *apimachineryversion.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` ServerVersion *apimachineryversion.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` } </ins> var ( version_example = templates.Examples(` # Print the client and server versions for the current context

} cmd.Flags().BoolP("client", "c", false, "Client version only (no server required).") cmd.Flags().BoolP("short", "", false, "Print just the version number.") <ins> cmd.Flags().String("output", "", "output format, options available are yaml and json") </ins> cmd.Flags().MarkShorthandDeprecated("client", "please use --client instead.") return cmd } func RunVersion(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { <del> v := fmt.Sprintf("%#v", version.Get()) if cmdutil.GetFlagBool(cmd, "short") { v = version.Get().GitVersion </del> <ins> 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 </ins> } <del> fmt.Fprintf(out, "Client Version: %sn", v) if cmdutil.GetFlagBool(cmd, "client") { return nil </del> <ins> switch of := cmdutil.GetFlagString(cmd, "output"); of { case "": if cmdutil.GetFlagBool(cmd, "short") { fmt.Fprintf(out, "Client Version: %sn", clientVersion.GitVersion) if serverVersion != nil { fmt.Fprintf(out, "Server Version: %sn", serverVersion.GitVersion) } } else { fmt.Fprintf(out, "Client Version: %sn", fmt.Sprintf("%#v", clientVersion)) if serverVersion != nil { fmt.Fprintf(out, "Server Version: %sn", 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) </ins> } <del> discoveryclient, err := f.DiscoveryClient() if err != nil { return err </del> <ins> if serverErr != nil { return serverErr </ins> } <del> serverVersion, err := discoveryclient.ServerVersion() </del> <ins> return nil } func retrieveServerVersion(f cmdutil.Factory) (*apimachineryversion.Info, error) { discoveryClient, err := f.DiscoveryClient() </ins> if err != nil { <del> return err </del> <ins> return nil, err </ins> } <del> v = fmt.Sprintf("%#v", *serverVersion) if cmdutil.GetFlagBool(cmd, "short") { v = serverVersion.GitVersion </del> <ins> serverVersion, err := discoveryClient.ServerVersion() if err != nil { return nil, err </ins> } <del> fmt.Fprintf(out, "Server Version: %sn", v) return nil </del> <ins> return serverVersion, nil </ins> }