forked from canonical/microcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroceph.go
328 lines (269 loc) · 8.31 KB
/
microceph.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package service
import (
"context"
"crypto/x509"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
cephTypes "github.com/canonical/microceph/microceph/api/types"
cephClient "github.com/canonical/microceph/microceph/client"
"github.com/canonical/microcluster/v2/client"
"github.com/canonical/microcluster/v2/microcluster"
"github.com/canonical/microcloud/microcloud/api/types"
cloudClient "github.com/canonical/microcloud/microcloud/client"
)
// CephService is a MicroCeph service.
type CephService struct {
m *microcluster.MicroCluster
name string
address string
port int64
config map[string]string
}
// NewCephService creates a new MicroCeph service with a client attached.
func NewCephService(name string, addr string, cloudDir string) (*CephService, error) {
proxy := func(r *http.Request) (*url.URL, error) {
if !strings.HasPrefix(r.URL.Path, "/1.0/services/microceph") {
r.URL.Path = "/1.0/services/microceph" + r.URL.Path
}
return shared.ProxyFromEnvironment(r)
}
client, err := microcluster.App(microcluster.Args{StateDir: cloudDir, Proxy: proxy})
if err != nil {
return nil, err
}
return &CephService{
m: client,
name: name,
address: addr,
port: CephPort,
config: make(map[string]string),
}, nil
}
// Client returns a client to the Ceph unix socket. If target is specified, it will be added to the query params.
func (s CephService) Client(target string) (*client.Client, error) {
c, err := s.m.LocalClient()
if err != nil {
return nil, err
}
if target != "" {
c = c.UseTarget(target)
}
c, err = cloudClient.UseAuthProxy(c, types.MicroCeph, cloudClient.AuthConfig{})
if err != nil {
return nil, err
}
return c, nil
}
// Bootstrap bootstraps the MicroCeph daemon on the default port.
func (s CephService) Bootstrap(ctx context.Context) error {
err := s.m.NewCluster(ctx, s.name, util.CanonicalNetworkAddress(s.address, s.port), s.config)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
for {
select {
case <-ctx.Done():
return fmt.Errorf("Timed out waiting for MicroCeph cluster to initialize")
default:
names, err := s.ClusterMembers(ctx)
if err != nil {
return err
}
if len(names) > 0 {
return nil
}
time.Sleep(time.Second)
}
}
}
// IssueToken issues a token for the given peer. Each token will last 5 minutes in case the system joins the cluster very slowly.
func (s CephService) IssueToken(ctx context.Context, peer string) (string, error) {
return s.m.NewJoinToken(ctx, peer, 5*time.Minute)
}
// DeleteToken deletes a token by its name.
func (s CephService) DeleteToken(ctx context.Context, tokenName string, address string) error {
var c *client.Client
var err error
if address != "" {
c, err = s.m.RemoteClient(util.CanonicalNetworkAddress(address, CloudPort))
if err != nil {
return err
}
c, err = cloudClient.UseAuthProxy(c, types.MicroCeph, cloudClient.AuthConfig{})
} else {
c, err = s.m.LocalClient()
}
if err != nil {
return err
}
return c.DeleteTokenRecord(ctx, tokenName)
}
// Join joins a cluster with the given token.
func (s CephService) Join(ctx context.Context, joinConfig JoinConfig) error {
err := s.m.JoinCluster(ctx, s.name, util.CanonicalNetworkAddress(s.address, s.port), joinConfig.Token, nil)
if err != nil {
return err
}
c, err := s.Client("")
if err != nil {
return err
}
for _, disk := range joinConfig.CephConfig {
_, err := cephClient.AddDisk(ctx, c, &disk)
if err != nil {
return err
}
}
return nil
}
// remoteClient returns an https client for the given address:port.
// It picks the cluster certificate if none is provided to verify the remote.
func (s CephService) remoteClient(cert *x509.Certificate, address string) (*client.Client, error) {
var err error
var client *client.Client
canonicalAddress := util.CanonicalNetworkAddress(address, CloudPort)
if cert != nil {
client, err = s.m.RemoteClientWithCert(canonicalAddress, cert)
} else {
client, err = s.m.RemoteClient(canonicalAddress)
}
if err != nil {
return nil, err
}
return client, nil
}
// RemoteClusterMembers returns a map of cluster member names and addresses from the MicroCloud at the given address.
// Provide the certificate of the remote server for mTLS.
func (s CephService) RemoteClusterMembers(ctx context.Context, cert *x509.Certificate, address string) (map[string]string, error) {
client, err := s.remoteClient(cert, address)
if err != nil {
return nil, err
}
client, err = cloudClient.UseAuthProxy(client, types.MicroCeph, cloudClient.AuthConfig{})
if err != nil {
return nil, err
}
return clusterMembers(ctx, client)
}
// ClusterMembers returns a map of cluster member names and addresses.
func (s CephService) ClusterMembers(ctx context.Context) (map[string]string, error) {
client, err := s.Client("")
if err != nil {
return nil, err
}
return clusterMembers(ctx, client)
}
// DeleteClusterMember removes the given cluster member from the service.
func (s CephService) DeleteClusterMember(ctx context.Context, name string, force bool) error {
c, err := s.m.LocalClient()
if err != nil {
return err
}
return c.DeleteClusterMember(ctx, name, force)
}
// ClusterConfig returns the Ceph cluster configuration.
func (s CephService) ClusterConfig(ctx context.Context, targetAddress string, cert *x509.Certificate) (map[string]string, error) {
var c *client.Client
var err error
if targetAddress == "" {
c, err = s.Client("")
if err != nil {
return nil, err
}
} else {
c, err = s.remoteClient(cert, targetAddress)
if err != nil {
return nil, err
}
c, err = cloudClient.UseAuthProxy(c, types.MicroCeph, cloudClient.AuthConfig{})
if err != nil {
return nil, err
}
}
data := cephTypes.Config{}
cs, err := cephClient.GetConfig(ctx, c, &data)
if err != nil {
return nil, err
}
configs := make(map[string]string, len(cs))
for _, c := range cs {
configs[c.Key] = c.Value
}
return configs, nil
}
// Type returns the type of Service.
func (s CephService) Type() types.ServiceType {
return types.MicroCeph
}
// Name returns the name of this Service instance.
func (s CephService) Name() string {
return s.name
}
// Address returns the address of this Service instance.
func (s CephService) Address() string {
return s.address
}
// Port returns the port of this Service instance.
func (s CephService) Port() int64 {
return s.port
}
// GetVersion gets the installed daemon version of the service, and returns an error if the version is not supported.
func (s CephService) GetVersion(ctx context.Context) (string, error) {
status, err := s.m.Status(ctx)
if err != nil && api.StatusErrorCheck(err, http.StatusNotFound) {
return "", fmt.Errorf("The installed version of %s is not supported", s.Type())
}
if err != nil {
return "", err
}
err = validateVersion(s.Type(), status.Version)
if err != nil {
return "", err
}
return status.Version, nil
}
// IsInitialized returns whether the service is initialized.
func (s CephService) IsInitialized(ctx context.Context) (bool, error) {
err := s.m.Ready(ctx)
if err != nil && api.StatusErrorCheck(err, http.StatusNotFound) {
return false, fmt.Errorf("Unix socket not found. Check if %s is installed", s.Type())
}
if err != nil {
return false, fmt.Errorf("Failed to wait for %s to get ready: %w", s.Type(), err)
}
status, err := s.m.Status(ctx)
if err != nil {
return false, fmt.Errorf("Failed to get %s status: %w", s.Type(), err)
}
return status.Ready, nil
}
// SetConfig sets the config of this Service instance.
func (s *CephService) SetConfig(config map[string]string) {
if s.config == nil {
s.config = make(map[string]string)
}
for key, value := range config {
s.config[key] = value
}
}
// SupportsFeature checks if the specified API feature of this Service instance if supported.
func (s *CephService) SupportsFeature(ctx context.Context, feature string) (bool, error) {
server, err := s.m.Status(ctx)
if err != nil {
return false, fmt.Errorf("Failed to get MicroCeph server status while checking for features: %v", err)
}
if server.Extensions == nil {
logger.Warnf("MicroCeph server does not expose API extensions")
return false, nil
}
return server.Extensions.HasExtension(feature), nil
}