From f5a31ecd27dfcec7d7e0d1694ba7ca0e79f65d3f Mon Sep 17 00:00:00 2001 From: Mayank Shah Date: Wed, 25 Dec 2024 13:49:52 +0530 Subject: [PATCH] EVEREST-1612 | print installed version info (server version) in the output of `everestctl version` (#932) Signed-off-by: Mayank Shah --- commands/version.go | 56 +++++++++++++++++++++++++++++++++++++----- pkg/version/version.go | 35 ++++++++++++++++---------- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/commands/version.go b/commands/version.go index 5f351338d..f486d2476 100644 --- a/commands/version.go +++ b/commands/version.go @@ -2,15 +2,18 @@ package commands import ( "fmt" + "os" "github.com/spf13/cobra" "go.uber.org/zap" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/percona/everest/pkg/cli/utils" "github.com/percona/everest/pkg/version" ) func newVersionCmd(l *zap.SugaredLogger) *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "version", Long: "Print version info", Short: "Print version info", @@ -18,18 +21,59 @@ func newVersionCmd(l *zap.SugaredLogger) *cobra.Command { outputJSON, err := cmd.Flags().GetBool("json") if err != nil { l.Errorf("could not parse json global flag. Error: %s", err) + os.Exit(1) return } - if !outputJSON { - fmt.Println(version.FullVersionInfo()) //nolint:forbidigo + kubeConfigPath, err := cmd.Flags().GetString("kubeconfig") + if err != nil { + l.Errorf("could not parse kubeconfig global flag. Error: %s", err) + os.Exit(1) return } - version, err := version.FullVersionJSON() + + clientOnly, err := cmd.Flags().GetBool("client-only") if err != nil { - l.Errorf("could not print JSON. Error: %s", err) + l.Errorf("could not parse client-only flag. Error: %s", err) + os.Exit(1) return } - fmt.Println(version) //nolint:forbidigo + + v := version.Info{ + ProjectName: version.ProjectName, + Version: version.Version, + FullCommit: version.FullCommit, + } + + if !clientOnly { + var err error + k, err := utils.NewKubeclient(l, kubeConfigPath) + if err != nil { + os.Exit(1) + return + } + ev, err := version.EverestVersionFromDeployment(cmd.Context(), k) + if client.IgnoreNotFound(err) != nil { + l.Error(err) + os.Exit(1) + } + sv := "[NOT INSTALLED]" + if ev != nil { + sv = fmt.Sprintf("v%s", ev.String()) + } + v.ServerVersion = &sv + } + + if !outputJSON { + fmt.Fprintln(os.Stdout, v) + return + } + fmt.Fprintln(os.Stdout, v.JSONString()) }, } + initVersionFlags(cmd) + return cmd +} + +func initVersionFlags(cmd *cobra.Command) { + cmd.Flags().Bool("client-only", false, "Print client version only") } diff --git a/pkg/version/version.go b/pkg/version/version.go index eb7298296..dc63f0e9b 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -53,23 +53,32 @@ func IsDev(v string) bool { return ver.Core().Equal(devLatestVer) } -// FullVersionInfo returns full version report. -func FullVersionInfo() string { +// Info represents the version information. +type Info struct { + ProjectName string `json:"projectName"` + Version string `json:"version"` + FullCommit string `json:"fullCommit"` + ServerVersion *string `json:"serverVersion,omitempty"` +} + +// String returns the string representation of the version information. +func (v Info) String() string { out := []string{ - "ProjectName: " + ProjectName, - "Version: " + Version, - "FullCommit: " + FullCommit, + "ProjectName: " + v.ProjectName, + "Version: " + v.Version, + "FullCommit: " + v.FullCommit, + } + if v.ServerVersion != nil { + out = append(out, "ServerVersion: "+*v.ServerVersion) } return strings.Join(out, "\n") } -// FullVersionJSON returns version info as JSON. -func FullVersionJSON() (string, error) { - res := map[string]string{ - "projectName": ProjectName, - "version": Version, - "fullCommit": FullCommit, +// JSONString returns the JSON representation of the version information. +func (v Info) JSONString() string { + data, err := json.Marshal(v) + if err != nil { + panic(err) } - data, err := json.Marshal(res) - return string(data), err + return string(data) }