capability.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "github.com/coreos/go-semver/semver"
  19. "github.com/coreos/pkg/capnslog"
  20. )
  21. type Capability string
  22. const (
  23. AuthCapability Capability = "auth"
  24. V3rpcCapability Capability = "v3rpc"
  25. )
  26. var (
  27. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver/api")
  28. // capabilityMaps is a static map of version to capability map.
  29. capabilityMaps = map[string]map[Capability]bool{
  30. "3.0.0": {AuthCapability: true, V3rpcCapability: true},
  31. "3.1.0": {AuthCapability: true, V3rpcCapability: true},
  32. "3.2.0": {AuthCapability: true, V3rpcCapability: true},
  33. "3.3.0": {AuthCapability: true, V3rpcCapability: true},
  34. }
  35. enableMapMu sync.RWMutex
  36. // enabledMap points to a map in capabilityMaps
  37. enabledMap map[Capability]bool
  38. curVersion *semver.Version
  39. )
  40. func init() {
  41. enabledMap = map[Capability]bool{
  42. AuthCapability: true,
  43. V3rpcCapability: true,
  44. }
  45. }
  46. // UpdateCapability updates the enabledMap when the cluster version increases.
  47. func UpdateCapability(v *semver.Version) {
  48. if v == nil {
  49. // if recovered but version was never set by cluster
  50. return
  51. }
  52. enableMapMu.Lock()
  53. if curVersion != nil && !curVersion.LessThan(*v) {
  54. enableMapMu.Unlock()
  55. return
  56. }
  57. curVersion = v
  58. enabledMap = capabilityMaps[curVersion.String()]
  59. enableMapMu.Unlock()
  60. plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
  61. }
  62. func IsCapabilityEnabled(c Capability) bool {
  63. enableMapMu.RLock()
  64. defer enableMapMu.RUnlock()
  65. if enabledMap == nil {
  66. return false
  67. }
  68. return enabledMap[c]
  69. }
  70. func EnableCapability(c Capability) {
  71. enableMapMu.Lock()
  72. defer enableMapMu.Unlock()
  73. enabledMap[c] = true
  74. }