kubectl version should include bundled kustomize version

This commit is contained in:
Katrina Verey 2022-03-23 19:17:29 -04:00
parent af059a0731
commit 44e63e8ff8
No known key found for this signature in database
GPG Key ID: 836031A2C9F15C08
3 changed files with 60 additions and 17 deletions

View File

@ -55,10 +55,20 @@ fi
./hack/update-internal-modules.sh ./hack/update-internal-modules.sh
./hack/lint-dependencies.sh ./hack/lint-dependencies.sh
sed -i '' -e "s/const kustomizeVersion.*$/const kustomizeVersion = \"${LATEST_KUSTOMIZE}\"/" staging/src/k8s.io/kubectl/pkg/cmd/version/version.go
echo -e "\n${color_blue}Committing changes${color_norm}" echo -e "\n${color_blue}Committing changes${color_norm}"
git add . git add .
git commit -a -m "Update kubectl kustomize to kyaml/$LATEST_KYAML, cmd/config/$LATEST_CONFIG, api/$LATEST_API, kustomize/$LATEST_KUSTOMIZE" git commit -a -m "Update kubectl kustomize to kyaml/$LATEST_KYAML, cmd/config/$LATEST_CONFIG, api/$LATEST_API, kustomize/$LATEST_KUSTOMIZE"
echo -e "\n${color_blue:?}Verifying kubectl kustomize version${color_norm:?}"
make WHAT=cmd/kubectl
if [[ $(_output/bin/kubectl version --client -o json | jq -r '.kustomizeVersion') != "$LATEST_KUSTOMIZE" ]]; then
echo -e "${color_red:?}Unexpected kubectl kustomize version${color_norm:?}"
exit 1
fi
echo -e "\n${color_green:?}Update successful${color_norm:?}" echo -e "\n${color_green:?}Update successful${color_norm:?}"
echo "Note: If any of the integration points changed, you may need to update them manually." echo "Note: If any of the integration points changed, you may need to update them manually."
echo "See https://github.com/kubernetes-sigs/kustomize/tree/master/releasing#update-kustomize-in-kubectl for more information" echo "See https://github.com/kubernetes-sigs/kustomize/tree/master/releasing#update-kustomize-in-kubectl for more information"

View File

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"runtime/debug"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
@ -34,10 +35,14 @@ import (
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
) )
// TODO(knverey): remove this hardcoding once kubectl being built with module support makes BuildInfo available.
const kustomizeVersion = "v4.4.1"
// Version is a struct for version information // Version is a struct for version information
type Version struct { type Version struct {
ClientVersion *apimachineryversion.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` ClientVersion *apimachineryversion.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
ServerVersion *apimachineryversion.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` KustomizeVersion string `json:"kustomizeVersion,omitempty" yaml:"kustomizeVersion,omitempty"`
ServerVersion *apimachineryversion.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
} }
var ( var (
@ -116,32 +121,32 @@ func (o *Options) Validate(args []string) error {
// Run executes version command // Run executes version command
func (o *Options) Run() error { func (o *Options) Run() error {
var ( var (
serverVersion *apimachineryversion.Info serverErr error
serverErr error versionInfo Version
versionInfo Version
) )
clientVersion := version.Get() versionInfo.ClientVersion = func() *apimachineryversion.Info { v := version.Get(); return &v }()
versionInfo.ClientVersion = &clientVersion versionInfo.KustomizeVersion = getKustomizeVersion()
if !o.ClientOnly && o.discoveryClient != nil { if !o.ClientOnly && o.discoveryClient != nil {
// Always request fresh data from the server // Always request fresh data from the server
o.discoveryClient.Invalidate() o.discoveryClient.Invalidate()
serverVersion, serverErr = o.discoveryClient.ServerVersion() versionInfo.ServerVersion, serverErr = o.discoveryClient.ServerVersion()
versionInfo.ServerVersion = serverVersion
} }
switch o.Output { switch o.Output {
case "": case "":
if o.Short { if o.Short {
fmt.Fprintf(o.Out, "Client Version: %s\n", clientVersion.GitVersion) fmt.Fprintf(o.Out, "Client Version: %s\n", versionInfo.ClientVersion.GitVersion)
if serverVersion != nil { fmt.Fprintf(o.Out, "Kustomize Version: %s\n", versionInfo.KustomizeVersion)
fmt.Fprintf(o.Out, "Server Version: %s\n", serverVersion.GitVersion) if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %s\n", versionInfo.ServerVersion.GitVersion)
} }
} else { } else {
fmt.Fprintf(o.Out, "Client Version: %#v\n", clientVersion) fmt.Fprintf(o.Out, "Client Version: %#v\n", *versionInfo.ClientVersion)
if serverVersion != nil { fmt.Fprintf(o.Out, "Kustomize Version: %s\n", versionInfo.KustomizeVersion)
fmt.Fprintf(o.Out, "Server Version: %#v\n", *serverVersion) if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %#v\n", *versionInfo.ServerVersion)
} }
} }
case "yaml": case "yaml":
@ -162,11 +167,24 @@ func (o *Options) Run() error {
return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", o.Output) return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", o.Output)
} }
if serverVersion != nil { if versionInfo.ServerVersion != nil {
if err := printVersionSkewWarning(o.ErrOut, clientVersion, *serverVersion); err != nil { if err := printVersionSkewWarning(o.ErrOut, *versionInfo.ClientVersion, *versionInfo.ServerVersion); err != nil {
return err return err
} }
} }
return serverErr return serverErr
} }
func getKustomizeVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return kustomizeVersion
}
for _, dep := range info.Deps {
if dep.Path == "sigs.k8s.io/kustomize/kustomize/v4" {
return dep.Version
}
}
return kustomizeVersion
}

View File

@ -66,6 +66,21 @@ run_kubectl_version_tests() {
kube::test::version::yaml_object_to_file "" "${TEMP}/client_server_yaml_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" kube::test::version::diff_assert "${TEMP}/client_server_json_version_test" "eq" "${TEMP}/client_server_yaml_version_test" "--output json/yaml has identical information"
kube::log::status "Testing kubectl version: contains semantic version of embedded kustomize"
output_message=$(kubectl version)
kube::test::if_has_not_string "${output_message}" "Kustomize Version\: unknown" "kustomize version should not be unknown"
kube::test::if_has_string "${output_message}" "Kustomize Version\: v[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*" "kubectl kustomize version should have a reasonable value"
kube::log::status "Testing kubectl version: all output formats include kustomize version"
output_message=$(kubectl version --client)
kube::test::if_has_string "${output_message}" "Kustomize Version" "kustomize version should be printed when --client is specified"
output_message=$(kubectl version --short)
kube::test::if_has_string "${output_message}" "Kustomize Version" "kustomize version should be printed when --short is specified"
output_message=$(kubectl version -o yaml)
kube::test::if_has_string "${output_message}" "kustomizeVersion" "kustomize version should be printed when -o yaml is used"
output_message=$(kubectl version -o json)
kube::test::if_has_string "${output_message}" "kustomizeVersion" "kustomize version should be printed when -o json is used"
set +o nounset set +o nounset
set +o errexit set +o errexit
} }