-
Notifications
You must be signed in to change notification settings - Fork 14
/
version.go
90 lines (76 loc) · 2.53 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
87
88
89
90
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package version
import (
"fmt"
"runtime"
gv "github.com/hashicorp/go-version"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/version"
)
type versionOptions struct {
verbose bool
}
// NewVersionCmd the version command
func NewVersionCmd(f cmdutil.Factory) *cobra.Command {
o := &versionOptions{}
cmd := &cobra.Command{
Use: "version",
Short: "Print the version information, include kubernetes, KubeBlocks and kbcli version.",
Run: func(cmd *cobra.Command, args []string) {
o.Run(f)
},
}
cmd.Flags().BoolVar(&o.verbose, "verbose", false, "print detailed kbcli information")
return cmd
}
func (o *versionOptions) Run(f cmdutil.Factory) {
client, err := f.KubernetesClientSet()
if err != nil {
klog.V(1).Infof("failed to get clientset: %v", err)
}
v, _ := util.GetVersionInfo(client)
if v.Kubernetes != "" {
fmt.Printf("Kubernetes: %s\n", v.Kubernetes)
}
if v.KubeBlocks != "" {
fmt.Printf("KubeBlocks: %s\n", v.KubeBlocks)
}
fmt.Printf("kbcli: %s\n", v.Cli)
if o.verbose {
fmt.Printf(" BuildDate: %s\n", version.BuildDate)
fmt.Printf(" GitCommit: %s\n", version.GitCommit)
fmt.Printf(" GitTag: %s\n", version.GitVersion)
fmt.Printf(" GoVersion: %s\n", runtime.Version())
fmt.Printf(" Compiler: %s\n", runtime.Compiler)
fmt.Printf(" Platform: %s/%s\n", runtime.GOOS, runtime.GOARCH)
}
kbVersion, err := gv.NewVersion(v.KubeBlocks)
if err != nil {
klog.V(1).Infof("failed to parse KubeBlocks version: %v", err)
return
}
cliVersion, err := gv.NewVersion(v.Cli)
if err != nil {
klog.V(1).Infof("failed to parse kbcli version: %v", err)
return
}
if !kbVersion.Equal(cliVersion) {
fmt.Printf("WARNING: version difference between kbcli (%s) and kubeblocks (%s) \n", v.Cli, v.KubeBlocks)
}
}