github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/version/version.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package version 21 22 import ( 23 "fmt" 24 "runtime" 25 26 gv "github.com/hashicorp/go-version" 27 "github.com/spf13/cobra" 28 "k8s.io/klog/v2" 29 cmdutil "k8s.io/kubectl/pkg/cmd/util" 30 31 "github.com/1aal/kubeblocks/pkg/cli/util" 32 "github.com/1aal/kubeblocks/version" 33 ) 34 35 type versionOptions struct { 36 verbose bool 37 } 38 39 // NewVersionCmd the version command 40 func NewVersionCmd(f cmdutil.Factory) *cobra.Command { 41 o := &versionOptions{} 42 cmd := &cobra.Command{ 43 Use: "version", 44 Short: "Print the version information, include kubernetes, KubeBlocks and kbcli version.", 45 Run: func(cmd *cobra.Command, args []string) { 46 o.Run(f) 47 }, 48 } 49 cmd.Flags().BoolVar(&o.verbose, "verbose", false, "print detailed kbcli information") 50 return cmd 51 } 52 53 func (o *versionOptions) Run(f cmdutil.Factory) { 54 client, err := f.KubernetesClientSet() 55 if err != nil { 56 klog.V(1).Infof("failed to get clientset: %v", err) 57 } 58 59 v, _ := util.GetVersionInfo(client) 60 if v.Kubernetes != "" { 61 fmt.Printf("Kubernetes: %s\n", v.Kubernetes) 62 } 63 if v.KubeBlocks != "" { 64 fmt.Printf("KubeBlocks: %s\n", v.KubeBlocks) 65 } 66 fmt.Printf("kbcli: %s\n", v.Cli) 67 if o.verbose { 68 fmt.Printf(" BuildDate: %s\n", version.BuildDate) 69 fmt.Printf(" GitCommit: %s\n", version.GitCommit) 70 fmt.Printf(" GitTag: %s\n", version.GitVersion) 71 fmt.Printf(" GoVersion: %s\n", runtime.Version()) 72 fmt.Printf(" Compiler: %s\n", runtime.Compiler) 73 fmt.Printf(" Platform: %s/%s\n", runtime.GOOS, runtime.GOARCH) 74 } 75 76 kbVersion, err := gv.NewVersion(v.KubeBlocks) 77 if err != nil { 78 klog.V(1).Infof("failed to parse KubeBlocks version: %v", err) 79 return 80 } 81 cliVersion, err := gv.NewVersion(v.Cli) 82 if err != nil { 83 klog.V(1).Infof("failed to parse kbcli version: %v", err) 84 return 85 } 86 87 if !kbVersion.Equal(cliVersion) { 88 fmt.Printf("WARNING: version difference between kbcli (%s) and kubeblocks (%s) \n", v.Cli, v.KubeBlocks) 89 } 90 }