From e90eb4c41348e655275c24c49c5cf77984d1b430 Mon Sep 17 00:00:00 2001 From: zhaojizhuang <571130360@qq.com> Date: Thu, 19 Aug 2021 15:23:13 +0800 Subject: [PATCH] add support display running version Signed-off-by: zhaojizhuang <571130360@qq.com> --- internal/cli/cmd/cmd.go | 2 +- internal/cli/cmd/version.go | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/internal/cli/cmd/cmd.go b/internal/cli/cmd/cmd.go index 6b7e5bb73f..50edec2c9b 100644 --- a/internal/cli/cmd/cmd.go +++ b/internal/cli/cmd/cmd.go @@ -36,7 +36,7 @@ func NewDefaultCiliumCommand() *cobra.Command { return nil } switch cmd.Name() { - case "completion", "help", "version": + case "completion", "help": return nil } diff --git a/internal/cli/cmd/version.go b/internal/cli/cmd/version.go index 643cdb946f..1b857737bd 100644 --- a/internal/cli/cmd/version.go +++ b/internal/cli/cmd/version.go @@ -15,6 +15,7 @@ package cmd import ( + "context" "fmt" "io" "net/http" @@ -48,16 +49,26 @@ func getLatestStableVersion() string { } func newCmdVersion() *cobra.Command { - return &cobra.Command{ + var namespace string + cmd := &cobra.Command{ Use: "version", Short: "Display detailed version information", Long: `Displays information about the version of this software.`, - Run: func(cmd *cobra.Command, _ []string) { - // TODO: add support for reporting the Cilium version running in - // the cluster, if any. See https://github.com/cilium/cilium-cli/issues/131 + RunE: func(cmd *cobra.Command, args []string) error { fmt.Printf("cilium-cli: %s compiled with %v on %v/%v\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) fmt.Printf("cilium image (default): %s\n", defaults.Version) fmt.Printf("cilium image (stable): %s\n", getLatestStableVersion()) + version, err := k8sClient.GetRunningCiliumVersion(context.Background(), namespace) + if version == "" || err != nil { + fmt.Printf("cilium image (running): unknown. Unable to obtain cilium version, no cilium pods found in namespace %q\n", namespace) + } else { + fmt.Printf("cilium image (running): %s\n", version) + } + return nil }, } + + cmd.Flags().StringVar(&contextName, "context", "", "Kubernetes configuration context") + cmd.Flags().StringVarP(&namespace, "namespace", "n", "kube-system", "Namespace Cilium is running in") + return cmd }