-
Notifications
You must be signed in to change notification settings - Fork 186
/
version.go
83 lines (68 loc) 路 2.09 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Copyright 漏 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"fmt"
"github.com/odigos-io/odigos/cli/cmd/resources"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
OdigosVersion string
OdigosCommit string
OdigosDate string
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print odigos version.",
Run: func(cmd *cobra.Command, args []string) {
short_flag, _ := cmd.Flags().GetBool("short")
if short_flag {
fmt.Printf("%s\n", OdigosVersion)
return
}
fmt.Printf("Odigos Cli Version: version.Info{Version:'%s', GitCommit:'%s', BuildDate:'%s'}\n", OdigosVersion, OdigosCommit, OdigosDate)
printOdigosClusterVersion(cmd)
},
}
func GetOdigosVersionInCluster(ctx context.Context, client *kube.Client, ns string) (string, error) {
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, resources.OdigosDeploymentConfigMapName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("error detecting Odigos version in the current cluster")
}
odigosVersion, ok := cm.Data["ODIGOS_VERSION"]
if !ok || odigosVersion == "" {
return "", fmt.Errorf("error detecting Odigos version in the current cluster")
}
return odigosVersion, nil
}
func printOdigosClusterVersion(cmd *cobra.Command) {
client, err := kube.CreateClient(cmd)
if err != nil {
return
}
ctx := cmd.Context()
ns, err := resources.GetOdigosNamespace(client, ctx)
if err != nil {
if resources.IsErrNoOdigosNamespaceFound(err) {
fmt.Println("Odigos is NOT yet installed in the current cluster")
} else {
fmt.Println("Error detecting Odigos version in the current cluster")
}
return
}
clusterVersion, err := GetOdigosVersionInCluster(ctx, client, ns)
if err != nil {
fmt.Println("Error detecting Odigos version in the current cluster")
return
}
fmt.Printf("Odigos Version (in cluster): version.Info{Version:'%s'}\n", clusterVersion)
}
func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().Bool("short", false, "prints only the CLI version")
}