Skip to content

Commit

Permalink
add support display running version
Browse files Browse the repository at this point in the history
Signed-off-by: zhaojizhuang <[email protected]>
  • Loading branch information
zhaojizhuang committed Aug 17, 2021
1 parent 6a1699d commit 2b01725
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewDefaultCiliumCommand() *cobra.Command {
return nil
}
switch cmd.Name() {
case "completion", "help", "version":
case "completion", "help":
return nil
}

Expand Down
58 changes: 52 additions & 6 deletions internal/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
package cmd

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strings"

"github.com/cilium/cilium-cli/defaults"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/internal/k8s"
)

// The following variables are set at compile time via LDFLAGS.
Expand All @@ -32,6 +37,38 @@ var (
Version string
)

type CiliumVersion struct {
client *k8s.Client
}

func NewCiliumVersion() *CiliumVersion {
return &CiliumVersion{client: k8sClient}
}

func (c *CiliumVersion) GetRunningCiliumVersionForCluster(ctx context.Context) (string, error) {
nss, err := c.client.ListNamespaces(ctx, metav1.ListOptions{})
if err != nil {
return "", fmt.Errorf("unable to list k8s namespace: %w", err)
}

// First look for ns kube-system
version, _ := c.client.GetRunningCiliumVersion(ctx, "kube-system")
if version != "" {
return version, nil
}

if len(nss.Items) > 0 {
for _, namespace := range nss.Items {
version, _ := c.client.GetRunningCiliumVersion(ctx, namespace.Name)
if version != "" {
return version, nil
}
}
}

return "", errors.New("unable to obtain cilium version: no cilium pods found")
}

func getLatestStableVersion() string {
resp, err := http.Get("https://raw.githubusercontent.com/cilium/cilium/master/stable.txt")
if err != nil {
Expand All @@ -48,16 +85,25 @@ func getLatestStableVersion() string {
}

func newCmdVersion() *cobra.Command {
return &cobra.Command{
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())
ciliumVersion := NewCiliumVersion()
version, err := ciliumVersion.GetRunningCiliumVersionForCluster(context.Background())
if err != nil {
fmt.Printf("cilium image (running): unknown. Error: %s \n", err.Error())
} else {
fmt.Printf("cilium image (running): %s\n", version)
}
return nil
},
}

cmd.Flags().StringVar(&contextName, "context", "", "Kubernetes configuration context")
return cmd
}

0 comments on commit 2b01725

Please sign in to comment.