configz.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package configz serves ComponentConfig objects from running components.
  14. //
  15. // Each component that wants to serve its ComponentConfig creates a Config
  16. // object, and the program should call InstallHandler once. e.g.,
  17. // func main() {
  18. // boatConfig := getBoatConfig()
  19. // planeConfig := getPlaneConfig()
  20. //
  21. // bcz, err := configz.New("boat")
  22. // if err != nil {
  23. // panic(err)
  24. // }
  25. // bcz.Set(boatConfig)
  26. //
  27. // pcz, err := configz.New("plane")
  28. // if err != nil {
  29. // panic(err)
  30. // }
  31. // pcz.Set(planeConfig)
  32. //
  33. // configz.InstallHandler(http.DefaultServeMux)
  34. // http.ListenAndServe(":8080", http.DefaultServeMux)
  35. // }
  36. package configz
  37. import (
  38. "encoding/json"
  39. "fmt"
  40. "net/http"
  41. "sync"
  42. )
  43. var (
  44. configsGuard sync.RWMutex
  45. configs = map[string]*Config{}
  46. )
  47. // Config is a handle to a ComponentConfig object. Don't create these directly;
  48. // use New() instead.
  49. type Config struct {
  50. val interface{}
  51. }
  52. // InstallHandler adds an HTTP handler on the given mux for the "/configz"
  53. // endpoint which serves all registered ComponentConfigs in JSON format.
  54. func InstallHandler(m mux) {
  55. m.Handle("/configz", http.HandlerFunc(handle))
  56. }
  57. type mux interface {
  58. Handle(string, http.Handler)
  59. }
  60. // New creates a Config object with the given name. Each Config is registered
  61. // with this package's "/configz" handler.
  62. func New(name string) (*Config, error) {
  63. configsGuard.Lock()
  64. defer configsGuard.Unlock()
  65. if _, found := configs[name]; found {
  66. return nil, fmt.Errorf("register config %q twice", name)
  67. }
  68. newConfig := Config{}
  69. configs[name] = &newConfig
  70. return &newConfig, nil
  71. }
  72. // Delete removes the named ComponentConfig from this package's "/configz"
  73. // handler.
  74. func Delete(name string) {
  75. configsGuard.Lock()
  76. defer configsGuard.Unlock()
  77. delete(configs, name)
  78. }
  79. // Set sets the ComponentConfig for this Config.
  80. func (v *Config) Set(val interface{}) {
  81. configsGuard.Lock()
  82. defer configsGuard.Unlock()
  83. v.val = val
  84. }
  85. // MarshalJSON marshals the ComponentConfig as JSON data.
  86. func (v *Config) MarshalJSON() ([]byte, error) {
  87. return json.Marshal(v.val)
  88. }
  89. func handle(w http.ResponseWriter, r *http.Request) {
  90. if err := write(w); err != nil {
  91. http.Error(w, err.Error(), http.StatusInternalServerError)
  92. }
  93. }
  94. func write(w http.ResponseWriter) error {
  95. var b []byte
  96. var err error
  97. func() {
  98. configsGuard.RLock()
  99. defer configsGuard.RUnlock()
  100. b, err = json.Marshal(configs)
  101. }()
  102. if err != nil {
  103. return fmt.Errorf("error marshaling json: %v", err)
  104. }
  105. w.Header().Set("Content-Type", "application/json")
  106. _, err = w.Write(b)
  107. return err
  108. }