capability.go 2.6 KB

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