Skip to content

Commit

Permalink
fix: print cli version even when there is no active cluster (#2362)
Browse files Browse the repository at this point in the history
Example of new output for the`odigos version` command:

```
Odigos CLI: 
  Version: v1.0.145
  GitCommit: ca13f98
  BuildDate: 2025-01-30T14:27:13Z

Odigos in Cluster:
  Status: No Kubernetes cluster found
```
  • Loading branch information
blumamir authored Feb 2, 2025
1 parent aa34496 commit 58d6c11
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
19 changes: 15 additions & 4 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ Get started with Odigos today to effortlessly improve the observability of your
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

client := kube.GetCLIClientOrExit(cmd)
ctx = cmdcontext.ContextWithKubeClient(ctx, client)
commandRequiresKubeClient := true
if cmd.Name() == "version" {
// version command can run without a kube client (prints only cli version)
commandRequiresKubeClient = false
}

details := autodetect.GetK8SClusterDetails(ctx, kubeConfig, kubeContext, client)
ctx = cmdcontext.ContextWithClusterDetails(ctx, details)
client, err := kube.CreateClient(cmd)
if err != nil && commandRequiresKubeClient {
kube.PrintClientErrorAndExit(err)
}

if client != nil {
ctx = cmdcontext.ContextWithKubeClient(ctx, client)
details := autodetect.GetK8SClusterDetails(ctx, kubeConfig, kubeContext, client)
ctx = cmdcontext.ContextWithClusterDetails(ctx, details)
}

cmd.SetContext(ctx)
},
Expand Down
69 changes: 38 additions & 31 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"errors"
"fmt"

"github.com/odigos-io/odigos/cli/cmd/resources"
cmdcontext "github.com/odigos-io/odigos/cli/pkg/cmd_context"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/k8sutils/pkg/getters"
"github.com/spf13/cobra"
)
Expand All @@ -31,61 +31,68 @@ var versionCmd = &cobra.Command{
Short: "Print odigos version.",
Long: `This command is used to print the Odigos version.
Both the CLI version and the Odigos components in your cluster will be printed.`,
Run: func(cmd *cobra.Command, args []string) {
cliFlag, _ := cmd.Flags().GetBool(cliFlag)
clusterFlag, _ := cmd.Flags().GetBool(clusterFlag)
RunE: func(cmd *cobra.Command, args []string) error {

if cliFlag {
fmt.Printf("%s\n", OdigosVersion)
}
odigosClusterVersion, clusterVersionErr := getOdigosVersionInCluster(cmd)

OdigosClusterVersion, err := getOdigosVersionInCluster(cmd)
cliFlag, _ := cmd.Flags().GetBool(cliFlag)
clusterFlag, _ := cmd.Flags().GetBool(clusterFlag)

if clusterFlag && err == nil {
fmt.Printf("%s\n", OdigosClusterVersion)
// check for errors - both flags cannot be used at the same time
if cliFlag && clusterFlag {
return errors.New("Only one of the flags --cli or --cluster can be used at a time")
}

if cliFlag || clusterFlag {
return
// handle case where only one of the flags is used, and print the version accordingly or return an error
if cliFlag {
fmt.Printf("%s\n", OdigosVersion)
return nil
} else if clusterFlag {
if clusterVersionErr != nil {
return clusterVersionErr
}
fmt.Printf("%s\n", odigosClusterVersion)
return nil
}

if err != nil {
fmt.Printf("%s\n", err)
fmt.Printf("Odigos CLI: \n Version: %s\n GitCommit: %s\n BuildDate: %s\n\n", OdigosVersion, OdigosCommit, OdigosDate)
var odigosClusterVersionText string
if clusterVersionErr != nil {
odigosClusterVersionText = fmt.Sprintf("Status: %s", clusterVersionErr.Error())
} else {
odigosClusterVersionText = fmt.Sprintf("Version: %s", odigosClusterVersion)
}

fmt.Printf("Odigos Cli Version: version.Info{Version:'%s', GitCommit:'%s', BuildDate:'%s'}\n", OdigosVersion, OdigosCommit, OdigosDate)
fmt.Printf("Odigos Version (in cluster): version.Info{Version:'%s'}\n", OdigosClusterVersion)

fmt.Printf("Odigos in Cluster:\n %s\n", odigosClusterVersionText)
return nil
},
Example: `
# Print the version of odigos CLI and Odigos deployment in your cluster
odigos version
`,
}

// returns the odigos version in the cluster (as v1.2.3) or err if the version cannot be determined
func getOdigosVersionInCluster(cmd *cobra.Command) (string, error) {
client, ns, err := getOdigosKubeClientAndNamespace(cmd)
if err != nil {
return "", err
}

return getters.GetOdigosVersionInClusterFromConfigMap(cmd.Context(), client.Clientset, ns)
}

func getOdigosKubeClientAndNamespace(cmd *cobra.Command) (*kube.Client, string, error) {
// get the client which is generated from the root command
ctx := cmd.Context()
client := cmdcontext.KubeClientFromContextOrExit(ctx)
client, err := cmdcontext.KubeClientFromContext(ctx)
if err != nil {
return "", errors.New("No Kubernetes cluster found")
}

ns, err := resources.GetOdigosNamespace(client, ctx)
odigosns, err := resources.GetOdigosNamespace(client, ctx)
if err != nil {
if resources.IsErrNoOdigosNamespaceFound(err) {
err = fmt.Errorf("Odigos is NOT yet installed in the current cluster")
return "", errors.New("Not Installed")
} else {
err = fmt.Errorf("Error detecting Odigos namespace in the current cluster")
return "", errors.New("Multiple Odigos installations found")
}
}

return client, ns, err
v, err := getters.GetOdigosVersionInClusterFromConfigMap(ctx, client.Clientset, odigosns)

return v, err
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions cli/pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ type Object interface {
// otherwise it creates a new client and returns it.
func GetCLIClientOrExit(cmd *cobra.Command) *Client {
// we can check the cmd context for client, but currently avoiding that due to circular dependencies
client, err := createClient(cmd)
client, err := CreateClient(cmd)
if err != nil {
PrintClientErrorAndExit(err)
}
return client
}

func createClient(cmd *cobra.Command) (*Client, error) {
func CreateClient(cmd *cobra.Command) (*Client, error) {
kc := cmd.Flag("kubeconfig").Value.String()
kContext := cmd.Flag("kube-context").Value.String()
config, err := k8sutils.GetClientConfigWithContext(kc, kContext)
Expand Down

0 comments on commit 58d6c11

Please sign in to comment.