capability.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/etcd/version"
  20. "github.com/coreos/go-semver/semver"
  21. "github.com/coreos/pkg/capnslog"
  22. )
  23. type Capability string
  24. const (
  25. AuthCapability Capability = "auth"
  26. V3rpcCapability Capability = "v3rpc"
  27. )
  28. var (
  29. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/etcdserver", "api")
  30. // capabilityMaps is a static map of version to capability map.
  31. // the base capabilities is the set of capability 2.0 supports.
  32. capabilityMaps = map[string]map[Capability]bool{
  33. "2.1.0": {AuthCapability: true},
  34. "2.2.0": {AuthCapability: true},
  35. "2.3.0": {AuthCapability: true},
  36. "3.0.0": {AuthCapability: true, V3rpcCapability: true},
  37. }
  38. // capLoopOnce ensures we only create one capability monitor goroutine
  39. capLoopOnce sync.Once
  40. enableMapMu sync.RWMutex
  41. // enabledMap points to a map in capabilityMaps
  42. enabledMap map[Capability]bool
  43. )
  44. func init() {
  45. enabledMap = make(map[Capability]bool)
  46. }
  47. // RunCapabilityLoop checks the cluster version every 500ms and updates
  48. // the enabledMap when the cluster version increased.
  49. func RunCapabilityLoop(s *etcdserver.EtcdServer) {
  50. go capLoopOnce.Do(func() { runCapabilityLoop(s) })
  51. }
  52. func runCapabilityLoop(s *etcdserver.EtcdServer) {
  53. stopped := s.StopNotify()
  54. var pv *semver.Version
  55. for {
  56. if v := s.ClusterVersion(); v != pv {
  57. if pv == nil || (v != nil && pv.LessThan(*v)) {
  58. pv = v
  59. enableMapMu.Lock()
  60. enabledMap = capabilityMaps[pv.String()]
  61. enableMapMu.Unlock()
  62. plog.Infof("enabled capabilities for version %s", version.Cluster(pv.String()))
  63. }
  64. }
  65. select {
  66. case <-stopped:
  67. return
  68. case <-time.After(500 * time.Millisecond):
  69. }
  70. }
  71. }
  72. func IsCapabilityEnabled(c Capability) bool {
  73. enableMapMu.RLock()
  74. defer enableMapMu.RUnlock()
  75. if enabledMap == nil {
  76. return false
  77. }
  78. return enabledMap[c]
  79. }
  80. func EnableCapability(c Capability) {
  81. enableMapMu.Lock()
  82. defer enableMapMu.Unlock()
  83. enabledMap[c] = true
  84. }