forked from linkerd/linkerd2
/
version.go
86 lines (72 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
84
85
86
package cmd
import (
"context"
"fmt"
"os"
"github.com/linkerd/linkerd2/controller/api/public"
pb "github.com/linkerd/linkerd2/controller/gen/public"
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/linkerd/linkerd2/pkg/version"
"github.com/spf13/cobra"
)
const DefaultVersionString = "unavailable"
type versionOptions struct {
shortVersion bool
onlyClientVersion bool
}
func newVersionOptions() *versionOptions {
return &versionOptions{
shortVersion: false,
onlyClientVersion: false,
}
}
func newCmdVersion() *cobra.Command {
options := newVersionOptions()
cmd := &cobra.Command{
Use: "version",
Short: "Print the client and server version information",
Run: func(cmd *cobra.Command, args []string) {
clientVersion := version.Version
if options.shortVersion {
fmt.Println(clientVersion)
} else {
fmt.Printf("Client version: %s\n", clientVersion)
}
client, err := newVersionClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to server: %s\n", err)
os.Exit(1)
}
if !options.onlyClientVersion {
serverVersion := getServerVersion(client)
if options.shortVersion {
fmt.Println(serverVersion)
} else {
fmt.Printf("Server version: %s\n", serverVersion)
}
}
},
}
cmd.Args = cobra.NoArgs
cmd.PersistentFlags().BoolVar(&options.shortVersion, "short", options.shortVersion, "Print the version number(s) only, with no additional output")
cmd.PersistentFlags().BoolVar(&options.onlyClientVersion, "client", options.onlyClientVersion, "Print the client version only")
return cmd
}
func getServerVersion(client pb.ApiClient) string {
resp, err := client.Version(context.Background(), &pb.Empty{})
if err != nil {
return DefaultVersionString
}
return resp.GetReleaseVersion()
}
// This client does not do any validation
func newVersionClient() (pb.ApiClient, error) {
if apiAddr != "" {
return public.NewInternalClient(controlPlaneNamespace, apiAddr)
}
kubeAPI, err := k8s.NewAPI(kubeconfigPath)
if err != nil {
return nil, err
}
return public.NewExternalClient(controlPlaneNamespace, kubeAPI)
}