capability.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2015 CoreOS, Inc.
  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 v2http
  15. import (
  16. "fmt"
  17. "net/http"
  18. "sync"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver"
  21. "github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
  22. "github.com/coreos/go-semver/semver"
  23. )
  24. type capability string
  25. const (
  26. authCapability capability = "auth"
  27. )
  28. var (
  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. }
  36. enableMapMu sync.Mutex
  37. // enabledMap points to a map in capabilityMaps
  38. enabledMap map[capability]bool
  39. )
  40. // capabilityLoop checks the cluster version every 500ms and updates
  41. // the enabledMap when the cluster version increased.
  42. // capabilityLoop MUST be ran in a goroutine before checking capability
  43. // or using capabilityHandler.
  44. func capabilityLoop(s *etcdserver.EtcdServer) {
  45. stopped := s.StopNotify()
  46. var pv *semver.Version
  47. for {
  48. if v := s.ClusterVersion(); v != pv {
  49. if pv == nil {
  50. pv = v
  51. } else if v != nil && pv.LessThan(*v) {
  52. pv = v
  53. }
  54. enableMapMu.Lock()
  55. enabledMap = capabilityMaps[pv.String()]
  56. enableMapMu.Unlock()
  57. }
  58. select {
  59. case <-stopped:
  60. return
  61. case <-time.After(500 * time.Millisecond):
  62. }
  63. }
  64. }
  65. func isCapabilityEnabled(c capability) bool {
  66. enableMapMu.Lock()
  67. defer enableMapMu.Unlock()
  68. if enabledMap == nil {
  69. return false
  70. }
  71. return enabledMap[c]
  72. }
  73. func capabilityHandler(c capability, fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  74. return func(w http.ResponseWriter, r *http.Request) {
  75. if !isCapabilityEnabled(c) {
  76. notCapable(w, r, c)
  77. return
  78. }
  79. fn(w, r)
  80. }
  81. }
  82. func notCapable(w http.ResponseWriter, r *http.Request, c capability) {
  83. herr := httptypes.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Not capable of accessing %s feature during rolling upgrades.", c))
  84. if err := herr.WriteTo(w); err != nil {
  85. plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
  86. }
  87. }