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. "go.etcd.io/etcd/version"
  18. "go.uber.org/zap"
  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("go.etcd.io/etcd", "etcdserver/api")
  29. // capabilityMaps is a static map of version to capability map.
  30. capabilityMaps = map[string]map[Capability]bool{
  31. "3.0.0": {AuthCapability: true, V3rpcCapability: true},
  32. "3.1.0": {AuthCapability: true, V3rpcCapability: true},
  33. "3.2.0": {AuthCapability: true, V3rpcCapability: true},
  34. "3.3.0": {AuthCapability: true, V3rpcCapability: true},
  35. "3.4.0": {AuthCapability: true, V3rpcCapability: true},
  36. "3.5.0": {AuthCapability: true, V3rpcCapability: true},
  37. }
  38. enableMapMu sync.RWMutex
  39. // enabledMap points to a map in capabilityMaps
  40. enabledMap map[Capability]bool
  41. curVersion *semver.Version
  42. )
  43. func init() {
  44. enabledMap = map[Capability]bool{
  45. AuthCapability: true,
  46. V3rpcCapability: true,
  47. }
  48. }
  49. // UpdateCapability updates the enabledMap when the cluster version increases.
  50. func UpdateCapability(lg *zap.Logger, v *semver.Version) {
  51. if v == nil {
  52. // if recovered but version was never set by cluster
  53. return
  54. }
  55. enableMapMu.Lock()
  56. if curVersion != nil && !curVersion.LessThan(*v) {
  57. enableMapMu.Unlock()
  58. return
  59. }
  60. curVersion = v
  61. enabledMap = capabilityMaps[curVersion.String()]
  62. enableMapMu.Unlock()
  63. if lg != nil {
  64. lg.Info(
  65. "enabled capabilities for version",
  66. zap.String("cluster-version", version.Cluster(v.String())),
  67. )
  68. } else {
  69. plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
  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. }