...

Source file src/k8s.io/component-base/configz/configz.go

Documentation: k8s.io/component-base/configz

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

View as plain text