kubectl version should include bundled kustomize version

Kubernetes-commit: 44e63e8ff8e7f98c75dc86d8c41553a5cce6a603
This commit is contained in:
Katrina Verey 2022-03-23 19:17:29 -04:00 committed by Kubernetes Publisher
parent dd13f90390
commit 5bb84013cd
1 changed files with 35 additions and 17 deletions

View File

@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"runtime/debug"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
@ -34,9 +35,13 @@ import (
"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
type Version struct {
ClientVersion *apimachineryversion.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
KustomizeVersion string `json:"kustomizeVersion,omitempty" yaml:"kustomizeVersion,omitempty"`
ServerVersion *apimachineryversion.Info `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
}
@ -116,32 +121,32 @@ func (o *Options) Validate(args []string) error {
// Run executes version command
func (o *Options) Run() error {
var (
serverVersion *apimachineryversion.Info
serverErr error
versionInfo Version
)
clientVersion := version.Get()
versionInfo.ClientVersion = &clientVersion
versionInfo.ClientVersion = func() *apimachineryversion.Info { v := version.Get(); return &v }()
versionInfo.KustomizeVersion = getKustomizeVersion()
if !o.ClientOnly && o.discoveryClient != nil {
// Always request fresh data from the server
o.discoveryClient.Invalidate()
serverVersion, serverErr = o.discoveryClient.ServerVersion()
versionInfo.ServerVersion = serverVersion
versionInfo.ServerVersion, serverErr = o.discoveryClient.ServerVersion()
}
switch o.Output {
case "":
if o.Short {
fmt.Fprintf(o.Out, "Client Version: %s\n", clientVersion.GitVersion)
if serverVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %s\n", serverVersion.GitVersion)
fmt.Fprintf(o.Out, "Client Version: %s\n", versionInfo.ClientVersion.GitVersion)
fmt.Fprintf(o.Out, "Kustomize Version: %s\n", versionInfo.KustomizeVersion)
if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %s\n", versionInfo.ServerVersion.GitVersion)
}
} else {
fmt.Fprintf(o.Out, "Client Version: %#v\n", clientVersion)
if serverVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %#v\n", *serverVersion)
fmt.Fprintf(o.Out, "Client Version: %#v\n", *versionInfo.ClientVersion)
fmt.Fprintf(o.Out, "Kustomize Version: %s\n", versionInfo.KustomizeVersion)
if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %#v\n", *versionInfo.ServerVersion)
}
}
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)
}
if serverVersion != nil {
if err := printVersionSkewWarning(o.ErrOut, clientVersion, *serverVersion); err != nil {
if versionInfo.ServerVersion != nil {
if err := printVersionSkewWarning(o.ErrOut, *versionInfo.ClientVersion, *versionInfo.ServerVersion); err != nil {
return err
}
}
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
}