capability.go 2.5 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. "time"
  18. "github.com/coreos/etcd/etcdserver"
  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("github.com/coreos/etcd/etcdserver", "api")
  29. // capabilityMaps is a static map of version to capability map.
  30. // the base capabilities is the set of capability 2.0 supports.
  31. capabilityMaps = map[string]map[Capability]bool{
  32. "2.1.0": {AuthCapability: true},
  33. "2.2.0": {AuthCapability: true},
  34. "2.3.0": {AuthCapability: true},
  35. "3.0.0": {AuthCapability: true, V3rpcCapability: true},
  36. }
  37. // capLoopOnce ensures we only create one capability monitor goroutine
  38. capLoopOnce sync.Once
  39. enableMapMu sync.RWMutex
  40. // enabledMap points to a map in capabilityMaps
  41. enabledMap map[Capability]bool
  42. )
  43. func init() {
  44. enabledMap = make(map[Capability]bool)
  45. }
  46. // RunCapabilityLoop checks the cluster version every 500ms and updates
  47. // the enabledMap when the cluster version increased.
  48. func RunCapabilityLoop(s *etcdserver.EtcdServer) {
  49. go capLoopOnce.Do(func() { runCapabilityLoop(s) })
  50. }
  51. func runCapabilityLoop(s *etcdserver.EtcdServer) {
  52. stopped := s.StopNotify()
  53. var pv *semver.Version
  54. for {
  55. if v := s.ClusterVersion(); v != pv {
  56. if pv == nil || (v != nil && pv.LessThan(*v)) {
  57. pv = v
  58. enableMapMu.Lock()
  59. enabledMap = capabilityMaps[pv.String()]
  60. enableMapMu.Unlock()
  61. plog.Infof("enabled capabilities for version %s", pv)
  62. }
  63. }
  64. select {
  65. case <-stopped:
  66. return
  67. case <-time.After(500 * time.Millisecond):
  68. }
  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. }