Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/kubeadm/app/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ go_library(
"//pkg/util/initsystem:go_default_library",
"//pkg/version:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/renstrom/dedent:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/version:go_default_library",
"//vendor/k8s.io/apiserver/pkg/util/flag:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/pkg/api:go_default_library",
Expand Down
40 changes: 38 additions & 2 deletions cmd/kubeadm/app/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,64 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"io"

"github.com/ghodss/yaml"
"github.com/spf13/cobra"

apimachineryversion "k8s.io/apimachinery/pkg/version"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/i18n"
"k8s.io/kubernetes/pkg/version"
)

// Version provides the version information of kubeadm.
type Version struct {
ClientVersion *apimachineryversion.Info `json:"clientVersion"`
}

func NewCmdVersion(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version of kubeadm",
Short: i18n.T("Print the version of kubeadm"),
Run: func(cmd *cobra.Command, args []string) {
err := RunVersion(out, cmd)
kubeadmutil.CheckErr(err)
},
}
cmd.Flags().StringP("output", "o", "", "output format, options available are yaml, json and short")
return cmd
}

func RunVersion(out io.Writer, cmd *cobra.Command) error {
fmt.Fprintf(out, "kubeadm version: %#v\n", version.Get())
clientVersion := version.Get()
v := Version{
ClientVersion: &clientVersion,
}

switch of := cmdutil.GetFlagString(cmd, "output"); of {
case "":
fmt.Fprintf(out, "kubeadm version: %#v\n", v.ClientVersion)
case "short":
fmt.Fprintf(out, "%s\n", v.ClientVersion.GitVersion)
case "yaml":
y, err := yaml.Marshal(&v)
if err != nil {
return err
}
fmt.Fprintln(out, string(y))
case "json":
y, err := json.Marshal(&v)
if err != nil {
return err
}
fmt.Fprintln(out, string(y))
default:
return fmt.Errorf("invalid output format: %s", of)
}

return nil
}
2 changes: 2 additions & 0 deletions cmd/kubeadm/test/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_test(
"init_test.go",
"join_test.go",
"token_test.go",
"version_test.go",
],
args = ["--kubeadm-path=../../kubeadm"],
data = ["//cmd/kubeadm"],
Expand All @@ -30,6 +31,7 @@ go_test(
"integration",
"skip",
],
deps = ["//vendor/github.com/ghodss/yaml:go_default_library"],
)

filegroup(
Expand Down
130 changes: 130 additions & 0 deletions cmd/kubeadm/test/cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubeadm

import (
"encoding/json"
"regexp"
"testing"

"github.com/ghodss/yaml"
)

const (
ShortExpectedRegex = "^v.+\n$"
NormalExpectedRegex = "^kubeadm version: &version\\.Info{Major:\".+\", Minor:\".+\", GitVersion:\".+\", GitCommit:\".+\", GitTreeState:\".+\", BuildDate:\".+\", GoVersion:\".+\", Compiler:\".+\", Platform:\".+\"}\n$"
)

var (
VersionInfo = []string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState", "buildDate", "goVersion", "compiler", "platform"}
)

func TestCmdVersion(t *testing.T) {
if *kubeadmCmdSkip {
t.Log("kubeadm cmd tests being skipped")
t.Skip()
}

var versionTest = []struct {
args string
regex string
expected bool
}{
{"--output=valid", "", false},
{"--output=short", ShortExpectedRegex, true},
{"", NormalExpectedRegex, true},
}

for _, rt := range versionTest {
stdout, _, actual := RunCmd(*kubeadmPath, "version", rt.args)
if (actual == nil) != rt.expected {
t.Errorf(
"failed CmdVersion running 'kubeadm version %s' with an error: %v\n\texpected: %t\n\t actual: %t",
rt.args,
actual,
rt.expected,
(actual == nil),
)
}

if rt.expected {
matched, err := regexp.MatchString(rt.regex, stdout)
if err != nil {
t.Fatalf("encountered an error while trying to match 'kubeadm version %s' stdout: %v", rt.args, err)
}
if !matched {
t.Errorf("'kubeadm version %s' stdout did not match expected regex; wanted: [%q], got: [%s]", rt.args, rt.regex, stdout)
}
}
}
}

func TestCmdVersionOutputJsonOrYaml(t *testing.T) {
if *kubeadmCmdSkip {
t.Log("kubeadm cmd tests being skipped")
t.Skip()
}

var versionTest = []struct {
args string
format string
expected bool
}{
{"--output=json", "json", true},
{"--output=yaml", "yaml", true},
}

for _, rt := range versionTest {
stdout, _, actual := RunCmd(*kubeadmPath, "version", rt.args)
if (actual == nil) != rt.expected {
t.Errorf(
"failed CmdVersion running 'kubeadm version %s' with an error: %v\n\texpected: %t\n\t actual: %t",
rt.args,
actual,
rt.expected,
(actual == nil),
)
}

if rt.expected {
var obj interface{}
switch rt.format {
case "json":
err := json.Unmarshal([]byte(stdout), &obj)
if err != nil {
t.Errorf("failed to parse json from 'kubeadm version %s': %s", rt.args, err)
}
case "yaml":
err := yaml.Unmarshal([]byte(stdout), &obj)
if err != nil {
t.Errorf("failed to parse yaml from 'kubeadm version %s': %s", rt.args, err)
}
}

m := obj.(map[string]interface{})
if m["clientVersion"] == nil {
t.Errorf("failed to get the infomation of clientVersion from 'kubeadm version %s'", rt.args)
}
info := m["clientVersion"].(map[string]interface{})
for _, key := range VersionInfo {
if len(info[key].(string)) == 0 {
t.Errorf("failed to get the infomation of %s from 'kubeadm version %s'", key, rt.args)
}
}
}
}
}