capability.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package api
  15. import (
  16. "sync"
  17. "github.com/coreos/etcd/version"
  18. "go.uber.org/zap"
  19. "github.com/coreos/go-semver/semver"
  20. "github.com/coreos/pkg/capnslog"
  21. )
  22. type Capability string
  23. const (
  24. AuthCapability Capability = "auth"
  25. V3rpcCapability Capability = "v3rpc"
  26. )
  27. var (
  28. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver/api")
  29. // capabilityMaps is a static map of version to capability map.
  30. capabilityMaps = map[string]map[Capability]bool{
  31. "3.0.0": {AuthCapability: true, V3rpcCapability: true},
  32. "3.1.0": {AuthCapability: true, V3rpcCapability: true},
  33. "3.2.0": {AuthCapability: true, V3rpcCapability: true},
  34. "3.3.0": {AuthCapability: true, V3rpcCapability: true},
  35. }
  36. enableMapMu sync.RWMutex
  37. // enabledMap points to a map in capabilityMaps
  38. enabledMap map[Capability]bool
  39. curVersion *semver.Version
  40. )
  41. func init() {
  42. enabledMap = map[Capability]bool{
  43. AuthCapability: true,
  44. V3rpcCapability: true,
  45. }
  46. }
  47. // UpdateCapability updates the enabledMap when the cluster version increases.
  48. func UpdateCapability(lg *zap.Logger, v *semver.Version) {
  49. if v == nil {
  50. // if recovered but version was never set by cluster
  51. return
  52. }
  53. enableMapMu.Lock()
  54. if curVersion != nil && !curVersion.LessThan(*v) {
  55. enableMapMu.Unlock()
  56. return
  57. }
  58. curVersion = v
  59. enabledMap = capabilityMaps[curVersion.String()]
  60. enableMapMu.Unlock()
  61. if lg != nil {
  62. lg.Info(
  63. "enabled capabilities for version",
  64. zap.String("cluster-version", version.Cluster(v.String())),
  65. )
  66. } else {
  67. plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
  68. }
  69. }
  70. func IsCapabilityEnabled(c Capability) bool {
  71. enableMapMu.RLock()
  72. defer enableMapMu.RUnlock()
  73. if enabledMap == nil {
  74. return false
  75. }
  76. return enabledMap[c]
  77. }
  78. func EnableCapability(c Capability) {
  79. enableMapMu.Lock()
  80. defer enableMapMu.Unlock()
  81. enabledMap[c] = true
  82. }