capability.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "go.etcd.io/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("go.etcd.io/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. "3.4.0": {AuthCapability: true, V3rpcCapability: true},
  36. }
  37. enableMapMu sync.RWMutex
  38. // enabledMap points to a map in capabilityMaps
  39. enabledMap map[Capability]bool
  40. curVersion *semver.Version
  41. )
  42. func init() {
  43. enabledMap = map[Capability]bool{
  44. AuthCapability: true,
  45. V3rpcCapability: true,
  46. }
  47. }
  48. // UpdateCapability updates the enabledMap when the cluster version increases.
  49. func UpdateCapability(lg *zap.Logger, v *semver.Version) {
  50. if v == nil {
  51. // if recovered but version was never set by cluster
  52. return
  53. }
  54. enableMapMu.Lock()
  55. if curVersion != nil && !curVersion.LessThan(*v) {
  56. enableMapMu.Unlock()
  57. return
  58. }
  59. curVersion = v
  60. enabledMap = capabilityMaps[curVersion.String()]
  61. enableMapMu.Unlock()
  62. if lg != nil {
  63. lg.Info(
  64. "enabled capabilities for version",
  65. zap.String("cluster-version", version.Cluster(v.String())),
  66. )
  67. } else {
  68. plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
  69. }
  70. }
  71. func IsCapabilityEnabled(c Capability) bool {
  72. enableMapMu.RLock()
  73. defer enableMapMu.RUnlock()
  74. if enabledMap == nil {
  75. return false
  76. }
  77. return enabledMap[c]
  78. }
  79. func EnableCapability(c Capability) {
  80. enableMapMu.Lock()
  81. defer enableMapMu.Unlock()
  82. enabledMap[c] = true
  83. }