capability.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // the base capabilities is the set of capability 2.0 supports.
  30. capabilityMaps = map[string]map[Capability]bool{
  31. "2.1.0": {AuthCapability: true},
  32. "2.2.0": {AuthCapability: true},
  33. "2.3.0": {AuthCapability: true},
  34. "3.0.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 = make(map[Capability]bool)
  43. }
  44. // UpdateCapability updates the enabledMap when the cluster version increases.
  45. func UpdateCapability(v *semver.Version) {
  46. if v == nil {
  47. // if recovered but version was never set by cluster
  48. return
  49. }
  50. enableMapMu.Lock()
  51. if curVersion != nil && !curVersion.LessThan(*v) {
  52. enableMapMu.Unlock()
  53. return
  54. }
  55. curVersion = v
  56. enabledMap = capabilityMaps[curVersion.String()]
  57. enableMapMu.Unlock()
  58. plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
  59. }
  60. func IsCapabilityEnabled(c Capability) bool {
  61. enableMapMu.RLock()
  62. defer enableMapMu.RUnlock()
  63. if enabledMap == nil {
  64. return false
  65. }
  66. return enabledMap[c]
  67. }
  68. func EnableCapability(c Capability) {
  69. enableMapMu.Lock()
  70. defer enableMapMu.Unlock()
  71. enabledMap[c] = true
  72. }