capability.go 2.2 KB

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