/
sv.go
190 lines (160 loc) · 4.56 KB
/
sv.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package util
import (
"context"
"fmt"
"strings"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "tkestack.io/tke/api/client/clientset/versioned"
v1 "tkestack.io/tke/api/platform/v1"
"tkestack.io/tke/pkg/util/log"
)
const (
// TODO: Make this configurable?
storageConfigName = "storage-config"
// CephConfTemplate is the content template of /etc/ceph/ceph.conf.
CephConfTemplate = ` [global]
mon host = %s
auth_cluster_required = cephx
auth_service_required = cephx
auth_client_required = cephx
# Workaround for http://tracker.ceph.com/issues/23446
fuse_set_user_groups = false`
// CephAdminKeyringTemplate is the content template of ceph client keyring.
CephAdminKeyringTemplate = ` [client.admin]
key = %s
auid = 0
caps mds = "allow *"
caps mon = "allow *"
caps osd = "allow *"
caps mgr = "allow *"`
// CephKeyringFileNameTemplate is the template of ceph client keyring file name.
CephKeyringFileNameTemplate = "ceph.client.%s.keyring"
)
const (
// Ceph defines the storage type of ceph
Ceph = "Ceph"
// TencentCloud defines the storage type of tencent cloud
TencentCloud = "TencentCloud"
)
// GetSVInfo returns the information of storage vendor.
func GetSVInfo(ctx context.Context, client clientset.Interface) (*SVInfo, error) {
config, err := getConfig(ctx, client)
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
info := &SVInfo{
Version: config.ResourceVersion,
Type: config.Data["type"],
cephInfo: cephInfo{
Monitors: config.Data["monitors"],
AdminID: config.Data["adminID"],
AdminKey: config.Data["adminKey"],
},
tencentCloudInfo: tencentCloudInfo{
SecretID: config.Data["secretID"],
SecretKey: config.Data["secretKey"],
},
}
if info.AdminID == "" {
info.AdminID = "admin"
}
var infoErr error
switch info.Type {
case TencentCloud:
infoErr = validateTencentCloudInfo(info)
case Ceph:
default:
infoErr = validateCephInfo(info)
}
if infoErr != nil {
return nil, infoErr
}
log.Debugf("Found ceph config: %+v", info)
return info, nil
}
func validateCephInfo(info *SVInfo) error {
var missedConfigs []string
if info.Monitors == "" {
missedConfigs = append(missedConfigs, "monitors")
}
if info.AdminKey == "" {
missedConfigs = append(missedConfigs, "adminKey")
}
if len(missedConfigs) > 0 {
return fmt.Errorf("ceph config %s missed", strings.Join(missedConfigs, ","))
}
return nil
}
func validateTencentCloudInfo(info *SVInfo) error {
var missedConfigs []string
if info.SecretKey == "" {
missedConfigs = append(missedConfigs, "secretID")
}
if info.SecretID == "" {
missedConfigs = append(missedConfigs, "secretKey")
}
if len(missedConfigs) > 0 {
return fmt.Errorf("tecent cloud config %s missed", strings.Join(missedConfigs, ","))
}
return nil
}
// GetSVInfoVersion returns storage vendor info ConfigMap's ResourceVersion.
func GetSVInfoVersion(ctx context.Context, client clientset.Interface) (string, error) {
config, err := getConfig(ctx, client)
if err != nil {
return "", err
}
if config == nil {
return "", nil
}
return config.ResourceVersion, nil
}
func getConfig(ctx context.Context, client clientset.Interface) (*v1.ConfigMap, error) {
config, err := client.PlatformV1().ConfigMaps().Get(ctx, storageConfigName, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
// Ceph cluster not enabled.
return nil, nil
}
return nil, fmt.Errorf("get storage config failed: %v", err)
}
return config, nil
}
// SVInfo is a bunch of information of storage vendor.
type SVInfo struct {
Version string
Type string
cephInfo
// TODO: Add tencent cloud storage info.\
tencentCloudInfo
}
type cephInfo struct {
Monitors string
AdminID string
AdminKey string
}
type tencentCloudInfo struct {
SecretID string
SecretKey string
}