-
Notifications
You must be signed in to change notification settings - Fork 12
/
version.go
166 lines (142 loc) · 4.73 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
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 util
import (
"bytes"
"context"
"fmt"
"os/exec"
"reflect"
"strings"
gv "github.com/hashicorp/go-version"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/version"
)
const (
// kubeblocksAppComponent the value of app.kubernetes.io/component label for KubeBlocks deployment
kubeblocksAppComponent = "apps"
// dataprotectionAppComponent the value of app.kubernetes.io/component label for DataProtection deployment
dataprotectionAppComponent = "dataprotection"
)
type Version struct {
KubeBlocks string
Kubernetes string
Cli string
}
// GetVersionInfo gets version include KubeBlocks, CLI and kubernetes
func GetVersionInfo(client kubernetes.Interface) (Version, error) {
var err error
v := Version{
Cli: version.GetVersion(),
}
if client == nil || reflect.ValueOf(client).IsNil() {
return v, nil
}
if v.Kubernetes, err = GetK8sVersion(client.Discovery()); err != nil {
return v, err
}
if v.KubeBlocks, err = getKubeBlocksVersion(client); err != nil {
return v, err
}
return v, nil
}
// getKubeBlocksVersion gets KubeBlocks version
func getKubeBlocksVersion(client kubernetes.Interface) (string, error) {
deploy, err := GetKubeBlocksDeploy(client)
if err != nil || deploy == nil {
return "", err
}
labels := deploy.GetLabels()
if labels == nil {
return "", fmt.Errorf("KubeBlocks deployment has no labels")
}
v, ok := labels["app.kubernetes.io/version"]
if !ok {
return "", fmt.Errorf("KubeBlocks deployment has no version label")
}
return v, nil
}
// GetK8sVersion gets k8s server version
func GetK8sVersion(discoveryClient discovery.DiscoveryInterface) (string, error) {
if discoveryClient == nil {
return "", nil
}
serverVersion, err := discoveryClient.ServerVersion()
if err != nil {
return "", err
}
if serverVersion != nil {
return serverVersion.GitVersion, nil
}
return "", nil
}
// GetKubeBlocksDeploy gets KubeBlocks deployments, now one kubernetes cluster
// only support one KubeBlocks
func GetKubeBlocksDeploy(client kubernetes.Interface) (*appsv1.Deployment, error) {
deploys, err := client.AppsV1().Deployments(metav1.NamespaceAll).List(context.Background(),
metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s,app.kubernetes.io/component=%s",
types.KubeBlocksChartName, kubeblocksAppComponent),
})
if err != nil {
return nil, err
}
if deploys == nil || len(deploys.Items) == 0 {
return nil, nil
}
if len(deploys.Items) > 1 {
return nil, fmt.Errorf("found multiple KubeBlocks deployments, please check your cluster")
}
return &deploys.Items[0], nil
}
// GetDataProtectionDeploy gets DataProtection deployments, now one kubernetes cluster
// only support one DataProtection
func GetDataProtectionDeploy(client kubernetes.Interface) (*appsv1.Deployment, error) {
deploys, err := client.AppsV1().Deployments(metav1.NamespaceAll).List(context.Background(),
metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s,app.kubernetes.io/component=%s",
types.KubeBlocksChartName, dataprotectionAppComponent),
})
if err != nil {
return nil, err
}
if deploys == nil || len(deploys.Items) == 0 {
return nil, nil
}
if len(deploys.Items) > 1 {
return nil, fmt.Errorf("found multiple DataProtection deployments, please check your cluster")
}
return &deploys.Items[0], nil
}
// GetDockerVersion get Docker Version
func GetDockerVersion() (*gv.Version, error) {
// exec cmd to get output from docker info --format '{{.ServerVersion}}'
cmd := exec.Command("docker", "info", "--format", "{{.ServerVersion}}")
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil || stderr.String() != "" {
errMsg := stderr.String()
if errMsg == "" {
errMsg = err.Error()
}
return nil, fmt.Errorf("failed to get the docker version by executing \"docker info --format {{.ServerVersion}}\": %s", errMsg)
}
return gv.NewVersion(strings.TrimSpace(string(out)))
}