capability.go 2.2 KB

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