/
objectstore.go
311 lines (265 loc) · 9.55 KB
/
objectstore.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
/*
Copyright 2016 The Rook Authors. 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
http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
ceph "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/daemon/ceph/model"
"github.com/rook/rook/pkg/operator/ceph/config"
)
const (
rootPool = ".rgw.root"
// AppName is the name Rook uses for the object store's application
AppName = "rook-ceph-rgw"
)
var (
metadataPools = []string{
// .rgw.root (rootPool) is appended to this slice where needed
"rgw.control",
"rgw.meta",
"rgw.log",
"rgw.buckets.index",
"rgw.buckets.non-ec",
}
dataPools = []string{
"rgw.buckets.data",
}
)
type idType struct {
ID string `json:"id"`
}
type realmType struct {
Realms []string `json:"realms"`
}
func createObjectStore(context *Context, metadataSpec, dataSpec model.Pool, serviceIP string, port int32) error {
err := createPools(context, metadataSpec, dataSpec)
if err != nil {
return errors.Wrapf(err, "failed to create object pools")
}
err = createRealm(context, serviceIP, port)
if err != nil {
return errors.Wrapf(err, "failed to create object store realm")
}
return nil
}
func deleteRealmAndPools(context *Context, preservePoolsOnDelete bool) error {
stores, err := getObjectStores(context)
if err != nil {
return errors.Wrapf(err, "failed to detect object stores during deletion")
}
logger.Infof("Found stores %v when deleting store %s", stores, context.Name)
err = deleteRealm(context)
if err != nil {
return errors.Wrapf(err, "failed to delete realm")
}
lastStore := false
if len(stores) == 1 && stores[0] == context.Name {
lastStore = true
}
if !preservePoolsOnDelete {
err = deletePools(context, lastStore)
if err != nil {
return errors.Wrapf(err, "failed to delete object store pools")
}
} else {
logger.Infof("PreservePoolsOnDelete is set in object store %s. Pools not deleted", context.Name)
}
return nil
}
func createRealm(context *Context, serviceIP string, port int32) error {
zoneArg := fmt.Sprintf("--rgw-zone=%s", context.Name)
endpointArg := fmt.Sprintf("--endpoints=%s:%d", serviceIP, port)
updatePeriod := false
// The first realm must be marked as the default
defaultArg := ""
stores, err := getObjectStores(context)
if err != nil {
return errors.Wrapf(err, "failed to get object stores")
}
if len(stores) == 0 {
defaultArg = "--default"
}
// create the realm if it doesn't exist yet
output, err := runAdminCommand(context, "realm", "get")
if err != nil {
updatePeriod = true
output, err = runAdminCommand(context, "realm", "create", defaultArg)
if err != nil {
return errors.Wrapf(err, "failed to create rgw realm %s", context.Name)
}
}
realmID, err := decodeID(output)
if err != nil {
return errors.Wrapf(err, "failed to parse realm id")
}
// create the zonegroup if it doesn't exist yet
output, err = runAdminCommand(context, "zonegroup", "get")
if err != nil {
updatePeriod = true
output, err = runAdminCommand(context, "zonegroup", "create", "--master", endpointArg, defaultArg)
if err != nil {
return errors.Wrapf(err, "failed to create rgw zonegroup for %s", context.Name)
}
}
zoneGroupID, err := decodeID(output)
if err != nil {
return errors.Wrapf(err, "failed to parse zone group id")
}
// create the zone if it doesn't exist yet
output, err = runAdminCommand(context, "zone", "get", zoneArg)
if err != nil {
updatePeriod = true
output, err = runAdminCommand(context, "zone", "create", "--master", endpointArg, zoneArg, defaultArg)
if err != nil {
return errors.Wrapf(err, "failed to create rgw zonegroup for %s", context.Name)
}
}
zoneID, err := decodeID(output)
if err != nil {
return errors.Wrapf(err, "failed to parse zone id")
}
if updatePeriod {
// the period will help notify other zones of changes if there are multi-zones
_, err := runAdminCommandNoRealm(context, "period", "update", "--commit")
if err != nil {
return errors.Wrapf(err, "failed to update period")
}
}
logger.Infof("RGW: realm=%s, zonegroup=%s, zone=%s", realmID, zoneGroupID, zoneID)
return nil
}
func deleteRealm(context *Context) error {
// <name>
_, err := runAdminCommand(context, "realm", "delete", "--rgw-realm", context.Name)
if err != nil {
logger.Warningf("failed to delete rgw realm %q. %v", context.Name, err)
}
_, err = runAdminCommand(context, "zonegroup", "delete", "--rgw-zonegroup", context.Name)
if err != nil {
logger.Warningf("failed to delete rgw zonegroup %q. %v", context.Name, err)
}
_, err = runAdminCommand(context, "zone", "delete", "--rgw-zone", context.Name)
if err != nil {
logger.Warningf("failed to delete rgw zone %q. %v", context.Name, err)
}
return nil
}
func decodeID(data string) (string, error) {
var id idType
err := json.Unmarshal([]byte(data), &id)
if err != nil {
return "", errors.Wrapf(err, "Failed to unmarshal json")
}
return id.ID, err
}
func getObjectStores(context *Context) ([]string, error) {
output, err := runAdminCommandNoRealm(context, "realm", "list")
if err != nil {
if strings.Index(err.Error(), "exit status 2") != 0 {
return []string{}, nil
}
return nil, err
}
var r realmType
err = json.Unmarshal([]byte(output), &r)
if err != nil {
return nil, errors.Wrapf(err, "Failed to unmarshal realms")
}
return r.Realms, nil
}
func deletePools(context *Context, lastStore bool) error {
pools := append(metadataPools, dataPools...)
if lastStore {
pools = append(pools, rootPool)
}
for _, pool := range pools {
name := poolName(context.Name, pool)
if err := ceph.DeletePool(context.Context, context.ClusterName, name); err != nil {
logger.Warningf("failed to delete pool %q. %v", name, err)
}
}
// Delete erasure code profile if any
erasureCodes, err := ceph.ListErasureCodeProfiles(context.Context, context.ClusterName)
if err != nil {
return errors.Wrapf(err, "failed to list erasure code profiles for cluster %s", context.ClusterName)
}
// cleans up the EC profile for the data pool only. Metadata pools don't support EC (only replication is supported).
objectStoreErasureCode := ceph.GetErasureCodeProfileForPool(context.Name)
for i := range erasureCodes {
if erasureCodes[i] == objectStoreErasureCode {
if err := ceph.DeleteErasureCodeProfile(context.Context, context.ClusterName, objectStoreErasureCode); err != nil {
return errors.Wrapf(err, "failed to delete erasure code profile %s for object store %s", objectStoreErasureCode, context.Name)
}
break
}
}
return nil
}
func createPools(context *Context, metadataSpec, dataSpec model.Pool) error {
// get the default PG count for rgw metadata pools
metadataPoolPGs, err := config.GetMonStore(context.Context, context.ClusterName).Get("mon.", "rgw_rados_pool_pg_num_min")
if err != nil {
logger.Warningf("failed to adjust the PG count for rgw metadata pools. using the general default. %v", err)
metadataPoolPGs = ceph.DefaultPGCount
}
if err := createSimilarPools(context, append(metadataPools, rootPool), metadataSpec, metadataPoolPGs); err != nil {
return errors.Wrapf(err, "failed to create metadata pools")
}
if err := createSimilarPools(context, dataPools, dataSpec, ceph.DefaultPGCount); err != nil {
return errors.Wrapf(err, "failed to create data pool")
}
return nil
}
func createSimilarPools(context *Context, pools []string, poolSpec model.Pool, pgCount string) error {
poolSpec.Name = context.Name
cephConfig := ceph.ModelPoolToCephPool(poolSpec)
isECPool := cephConfig.ErasureCodeProfile != ""
if isECPool {
// create a new erasure code profile for the new pool
if err := ceph.CreateErasureCodeProfile(context.Context, context.ClusterName, poolSpec.ErasureCodedConfig, cephConfig.ErasureCodeProfile,
poolSpec.FailureDomain, poolSpec.CrushRoot, poolSpec.DeviceClass); err != nil {
return errors.Wrapf(err, "failed to create erasure code profile for object store %s", context.Name)
}
}
for _, pool := range pools {
// create the pool if it doesn't exist yet
name := poolName(context.Name, pool)
if _, err := ceph.GetPoolDetails(context.Context, context.ClusterName, name); err != nil {
cephConfig.Name = name
// If the ceph config has an EC profile, an EC pool must be created. Otherwise, it's necessary
// to create a replicated pool.
var err error
if isECPool {
// An EC pool backing an object store does not need to enable EC overwrites, so the pool is
// created with that property disabled to avoid unnecessary performance impact.
err = ceph.CreateECPoolForApp(context.Context, context.ClusterName, cephConfig, pgCount, AppName, false /* enableECOverwrite */, poolSpec.ErasureCodedConfig)
} else {
err = ceph.CreateReplicatedPoolForApp(context.Context, context.ClusterName, cephConfig, pgCount, AppName)
}
if err != nil {
return errors.Errorf("failed to create pool %s for object store %s", name, context.Name)
}
}
}
return nil
}
func poolName(storeName, poolName string) string {
if strings.HasPrefix(poolName, ".") {
return poolName
}
// the name of the pool is <instance>.<name>, except for the pool ".rgw.root" that spans object stores
return fmt.Sprintf("%s.%s", storeName, poolName)
}