config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Copyright 2018 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 provides methods for creating and formatting Ceph configuration files for daemons.
  14. package client
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "github.com/rook/rook/pkg/operator/k8sutil"
  24. kerrors "k8s.io/apimachinery/pkg/api/errors"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "github.com/coreos/pkg/capnslog"
  27. "github.com/go-ini/ini"
  28. "github.com/pkg/errors"
  29. "github.com/rook/rook/pkg/clusterd"
  30. cephutil "github.com/rook/rook/pkg/daemon/ceph/util"
  31. cephver "github.com/rook/rook/pkg/operator/ceph/version"
  32. )
  33. var logger = capnslog.NewPackageLogger("github.com/rook/rook", "cephclient")
  34. const (
  35. // DefaultKeyringFile is the default name of the file where Ceph stores its keyring info
  36. DefaultKeyringFile = "keyring"
  37. // Msgr2port is the listening port of the messenger v2 protocol
  38. Msgr2port = 3300
  39. // Msgr1port is the listening port of the messenger v1 protocol
  40. Msgr1port = 6789
  41. )
  42. var (
  43. // DefaultConfigDir is the default dir where Ceph stores its configs. Can be overridden for unit
  44. // tests.
  45. DefaultConfigDir = "/etc/ceph"
  46. // DefaultConfigFile is the default name of the file where Ceph stores its configs. Can be
  47. // overridden for unit tests.
  48. DefaultConfigFile = "ceph.conf"
  49. )
  50. // GlobalConfig represents the [global] sections of Ceph's config file.
  51. type GlobalConfig struct {
  52. FSID string `ini:"fsid,omitempty"`
  53. MonMembers string `ini:"mon initial members,omitempty"`
  54. MonHost string `ini:"mon host"`
  55. }
  56. // CephConfig represents an entire Ceph config including all sections.
  57. type CephConfig struct {
  58. *GlobalConfig `ini:"global,omitempty"`
  59. }
  60. // DefaultConfigFilePath returns the full path to Ceph's default config file
  61. func DefaultConfigFilePath() string {
  62. return path.Join(DefaultConfigDir, DefaultConfigFile)
  63. }
  64. // getConfFilePath gets the path of a given cluster's config file
  65. func getConfFilePath(root, clusterName string) string {
  66. return fmt.Sprintf("%s/%s.config", root, clusterName)
  67. }
  68. // GenerateConnectionConfig calls GenerateConnectionConfigWithSettings with no settings
  69. // overridden.
  70. func GenerateConnectionConfig(context *clusterd.Context, cluster *ClusterInfo) (string, error) {
  71. return GenerateConnectionConfigWithSettings(context, cluster, nil)
  72. }
  73. // GenerateConnectionConfigWithSettings generates a Ceph config and keyring which will allow
  74. // the daemon to connect. Default config file settings can be overridden by specifying
  75. // some subset of settings.
  76. func GenerateConnectionConfigWithSettings(context *clusterd.Context, clusterInfo *ClusterInfo, settings *CephConfig) (string, error) {
  77. root := path.Join(context.ConfigDir, clusterInfo.Namespace)
  78. keyringPath := path.Join(root, fmt.Sprintf("%s.keyring", clusterInfo.CephCred.Username))
  79. err := writeKeyring(CephKeyring(clusterInfo.CephCred), keyringPath)
  80. if err != nil {
  81. return "", errors.Wrapf(err, "failed to write keyring %q to %s", clusterInfo.CephCred.Username, root)
  82. }
  83. filePath, err := generateConfigFile(context, clusterInfo, root, keyringPath, settings, nil)
  84. if err != nil {
  85. return "", errors.Wrapf(err, "failed to write config to %s", root)
  86. }
  87. logger.Infof("generated admin config in %s", root)
  88. return filePath, nil
  89. }
  90. // generateConfigFile generates and writes a config file to disk.
  91. func generateConfigFile(context *clusterd.Context, clusterInfo *ClusterInfo, pathRoot, keyringPath string, globalConfig *CephConfig, clientSettings map[string]string) (string, error) {
  92. // create the config directory
  93. if err := os.MkdirAll(pathRoot, 0744); err != nil {
  94. return "", errors.Wrapf(err, "failed to create config directory at %q", pathRoot)
  95. }
  96. configFile, err := createGlobalConfigFileSection(context, clusterInfo, globalConfig)
  97. if err != nil {
  98. return "", errors.Wrap(err, "failed to create global config section")
  99. }
  100. if err := mergeDefaultConfigWithRookConfigOverride(context, clusterInfo, configFile); err != nil {
  101. return "", errors.Wrapf(err, "failed to merge global config with %q", k8sutil.ConfigOverrideName)
  102. }
  103. qualifiedUser := getQualifiedUser(clusterInfo.CephCred.Username)
  104. if err := addClientConfigFileSection(configFile, qualifiedUser, keyringPath, clientSettings); err != nil {
  105. return "", errors.Wrap(err, "failed to add admin client config section")
  106. }
  107. // write the entire config to disk
  108. filePath := getConfFilePath(pathRoot, clusterInfo.Namespace)
  109. logger.Infof("writing config file %s", filePath)
  110. if err := configFile.SaveTo(filePath); err != nil {
  111. return "", errors.Wrapf(err, "failed to save config file %s", filePath)
  112. }
  113. return filePath, nil
  114. }
  115. func mergeDefaultConfigWithRookConfigOverride(clusterdContext *clusterd.Context, clusterInfo *ClusterInfo, configFile *ini.File) error {
  116. cm, err := clusterdContext.Clientset.CoreV1().ConfigMaps(clusterInfo.Namespace).Get(clusterInfo.Context, k8sutil.ConfigOverrideName, metav1.GetOptions{})
  117. if err != nil {
  118. if !kerrors.IsNotFound(err) {
  119. return errors.Wrapf(err, "failed to read configmap %q", k8sutil.ConfigOverrideName)
  120. }
  121. return nil
  122. }
  123. config, ok := cm.Data["config"]
  124. if !ok || config == "" {
  125. logger.Debugf("No ceph configuration override to merge as %q configmap is empty", k8sutil.ConfigOverrideName)
  126. return nil
  127. }
  128. if err := configFile.Append([]byte(config)); err != nil {
  129. return errors.Wrapf(err, "failed to load config data from %q", k8sutil.ConfigOverrideName)
  130. }
  131. // Remove any debug message setting from the config file
  132. // Debug messages will be printed on stdout, rendering the output of each command unreadable, especially json output
  133. // This call is idempotent and will not fail if the debug message is not present
  134. configFile.Section("global").DeleteKey("debug_ms")
  135. configFile.Section("global").DeleteKey("debug ms")
  136. return nil
  137. }
  138. // prepends "client." if a user namespace is not already specified
  139. func getQualifiedUser(user string) string {
  140. if !strings.Contains(user, ".") {
  141. return fmt.Sprintf("client.%s", user)
  142. }
  143. return user
  144. }
  145. // CreateDefaultCephConfig creates a default ceph config file.
  146. func CreateDefaultCephConfig(context *clusterd.Context, clusterInfo *ClusterInfo) (*CephConfig, error) {
  147. cephVersionEnv := os.Getenv("ROOK_CEPH_VERSION")
  148. if cephVersionEnv != "" {
  149. v, err := cephver.ExtractCephVersion(cephVersionEnv)
  150. if err != nil {
  151. return nil, errors.Wrap(err, "failed to extract ceph version")
  152. }
  153. clusterInfo.CephVersion = *v
  154. }
  155. // extract a list of just the monitor names, which will populate the "mon initial members"
  156. // and "mon hosts" global config field
  157. monMembers, monHosts := PopulateMonHostMembers(clusterInfo)
  158. conf := &CephConfig{
  159. GlobalConfig: &GlobalConfig{
  160. FSID: clusterInfo.FSID,
  161. MonMembers: strings.Join(monMembers, " "),
  162. MonHost: strings.Join(monHosts, ","),
  163. },
  164. }
  165. return conf, nil
  166. }
  167. // create a config file with global settings configured, and return an ini file
  168. func createGlobalConfigFileSection(context *clusterd.Context, clusterInfo *ClusterInfo, userConfig *CephConfig) (*ini.File, error) {
  169. var ceph *CephConfig
  170. if userConfig != nil {
  171. // use the user config since it was provided
  172. ceph = userConfig
  173. } else {
  174. var err error
  175. ceph, err = CreateDefaultCephConfig(context, clusterInfo)
  176. if err != nil {
  177. return nil, errors.Wrap(err, "failed to create default ceph config")
  178. }
  179. }
  180. configFile := ini.Empty()
  181. err := ini.ReflectFrom(configFile, ceph)
  182. return configFile, err
  183. }
  184. // add client config to the ini file
  185. func addClientConfigFileSection(configFile *ini.File, clientName, keyringPath string, settings map[string]string) error {
  186. s, err := configFile.NewSection(clientName)
  187. if err != nil {
  188. return err
  189. }
  190. if _, err := s.NewKey("keyring", keyringPath); err != nil {
  191. return err
  192. }
  193. for key, val := range settings {
  194. if _, err := s.NewKey(key, val); err != nil {
  195. return errors.Wrapf(err, "failed to add key %s", key)
  196. }
  197. }
  198. return nil
  199. }
  200. // PopulateMonHostMembers extracts a list of just the monitor names, which will populate the "mon initial members"
  201. // and "mon hosts" global config field
  202. func PopulateMonHostMembers(clusterInfo *ClusterInfo) ([]string, []string) {
  203. var monMembers []string
  204. var monHosts []string
  205. for _, monitor := range clusterInfo.Monitors {
  206. if monitor.OutOfQuorum {
  207. logger.Warningf("skipping adding mon %q to config file, detected out of quorum", monitor.Name)
  208. continue
  209. }
  210. monMembers = append(monMembers, monitor.Name)
  211. monIP := cephutil.GetIPFromEndpoint(monitor.Endpoint)
  212. // Detect the current port if the mon already exists
  213. // so the same msgr1 port can be preserved if needed (6789 or 6790)
  214. currentMonPort := cephutil.GetPortFromEndpoint(monitor.Endpoint)
  215. if currentMonPort == Msgr2port {
  216. msgr2Endpoint := net.JoinHostPort(monIP, strconv.Itoa(int(Msgr2port)))
  217. monHosts = append(monHosts, "[v2:"+msgr2Endpoint+"]")
  218. } else {
  219. msgr2Endpoint := net.JoinHostPort(monIP, strconv.Itoa(int(Msgr2port)))
  220. msgr1Endpoint := net.JoinHostPort(monIP, strconv.Itoa(int(currentMonPort)))
  221. monHosts = append(monHosts, "[v2:"+msgr2Endpoint+",v1:"+msgr1Endpoint+"]")
  222. }
  223. }
  224. return monMembers, monHosts
  225. }
  226. // WriteCephConfig writes the ceph config so ceph commands can be executed
  227. func WriteCephConfig(context *clusterd.Context, clusterInfo *ClusterInfo) error {
  228. // create the ceph.conf with the default settings
  229. cephConfig, err := CreateDefaultCephConfig(context, clusterInfo)
  230. if err != nil {
  231. return errors.Wrap(err, "failed to create default ceph config")
  232. }
  233. // write the latest config to the config dir
  234. confFilePath, err := GenerateConnectionConfigWithSettings(context, clusterInfo, cephConfig)
  235. if err != nil {
  236. return errors.Wrap(err, "failed to write connection config")
  237. }
  238. src, err := os.ReadFile(filepath.Clean(confFilePath))
  239. if err != nil {
  240. return errors.Wrap(err, "failed to copy connection config to /etc/ceph. failed to read the connection config")
  241. }
  242. err = os.WriteFile(DefaultConfigFilePath(), src, 0600)
  243. if err != nil {
  244. return errors.Wrapf(err, "failed to copy connection config to /etc/ceph. failed to write %q", DefaultConfigFilePath())
  245. }
  246. dst, err := os.ReadFile(DefaultConfigFilePath())
  247. if err == nil {
  248. logger.Debugf("config file @ %s:\n%s", DefaultConfigFilePath(), dst)
  249. } else {
  250. logger.Warningf("wrote and copied config file but failed to read it back from %s for logging. %v", DefaultConfigFilePath(), err)
  251. }
  252. return nil
  253. }