erasure-code-profile.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  19. "github.com/rook/rook/pkg/clusterd"
  20. )
  21. type CephErasureCodeProfile struct {
  22. DataChunkCount uint `json:"k,string"`
  23. CodingChunkCount uint `json:"m,string"`
  24. Plugin string `json:"plugin"`
  25. Technique string `json:"technique"`
  26. FailureDomain string `json:"crush-failure-domain"`
  27. CrushRoot string `json:"crush-root"`
  28. }
  29. func ListErasureCodeProfiles(context *clusterd.Context, clusterInfo *ClusterInfo) ([]string, error) {
  30. args := []string{"osd", "erasure-code-profile", "ls"}
  31. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  32. if err != nil {
  33. return nil, errors.Wrap(err, "failed to list erasure-code-profiles")
  34. }
  35. var ecProfiles []string
  36. err = json.Unmarshal(buf, &ecProfiles)
  37. if err != nil {
  38. return nil, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(buf))
  39. }
  40. return ecProfiles, nil
  41. }
  42. func GetErasureCodeProfileDetails(context *clusterd.Context, clusterInfo *ClusterInfo, name string) (CephErasureCodeProfile, error) {
  43. args := []string{"osd", "erasure-code-profile", "get", name}
  44. buf, err := NewCephCommand(context, clusterInfo, args).Run()
  45. if err != nil {
  46. return CephErasureCodeProfile{}, errors.Wrapf(err, "failed to get erasure-code-profile for %q", name)
  47. }
  48. var ecProfileDetails CephErasureCodeProfile
  49. err = json.Unmarshal(buf, &ecProfileDetails)
  50. if err != nil {
  51. return CephErasureCodeProfile{}, errors.Wrapf(err, "unmarshal failed raw buffer response %s", string(buf))
  52. }
  53. return ecProfileDetails, nil
  54. }
  55. func CreateErasureCodeProfile(context *clusterd.Context, clusterInfo *ClusterInfo, profileName string, pool cephv1.PoolSpec) error {
  56. // look up the default profile so we can use the default plugin/technique
  57. defaultProfile, err := GetErasureCodeProfileDetails(context, clusterInfo, "default")
  58. if err != nil {
  59. return errors.Wrap(err, "failed to look up default erasure code profile")
  60. }
  61. // define the profile with a set of key/value pairs
  62. profilePairs := []string{
  63. fmt.Sprintf("k=%d", pool.ErasureCoded.DataChunks),
  64. fmt.Sprintf("m=%d", pool.ErasureCoded.CodingChunks),
  65. fmt.Sprintf("plugin=%s", defaultProfile.Plugin),
  66. fmt.Sprintf("technique=%s", defaultProfile.Technique),
  67. }
  68. if pool.FailureDomain != "" {
  69. profilePairs = append(profilePairs, fmt.Sprintf("crush-failure-domain=%s", pool.FailureDomain))
  70. }
  71. if pool.CrushRoot != "" {
  72. profilePairs = append(profilePairs, fmt.Sprintf("crush-root=%s", pool.CrushRoot))
  73. }
  74. if pool.DeviceClass != "" {
  75. profilePairs = append(profilePairs, fmt.Sprintf("crush-device-class=%s", pool.DeviceClass))
  76. }
  77. args := []string{"osd", "erasure-code-profile", "set", profileName, "--force"}
  78. args = append(args, profilePairs...)
  79. _, err = NewCephCommand(context, clusterInfo, args).Run()
  80. if err != nil {
  81. return errors.Wrap(err, "failed to set ec-profile")
  82. }
  83. return nil
  84. }
  85. func DeleteErasureCodeProfile(context *clusterd.Context, clusterInfo *ClusterInfo, profileName string) error {
  86. args := []string{"osd", "erasure-code-profile", "rm", profileName}
  87. cmd := NewCephCommand(context, clusterInfo, args)
  88. cmd.JsonOutput = false
  89. buf, err := cmd.Run()
  90. if err != nil {
  91. return errors.Wrapf(err, "failed to delete erasure-code-profile %q. output: %q.", profileName, string(buf))
  92. }
  93. return nil
  94. }