diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 498828ffe3..c27c10eabd 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -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) }, diff --git a/cli/cmd/version.go b/cli/cmd/version.go index 57f7a78366..25e3f6d37e 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -4,11 +4,11 @@ Copyright © 2022 NAME HERE 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" ) @@ -31,31 +31,39 @@ 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 @@ -63,29 +71,28 @@ 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() { diff --git a/cli/pkg/kube/client.go b/cli/pkg/kube/client.go index 13dfed3c89..9ec37af516 100644 --- a/cli/pkg/kube/client.go +++ b/cli/pkg/kube/client.go @@ -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)