Merge pull request #90375 from cici37/removeConfigz
move pkg/util/configz to k8s.io/component-base
This commit is contained in:
@@ -31,7 +31,6 @@ go_library(
|
||||
"//pkg/kubelet/server/streaming:go_default_library",
|
||||
"//pkg/kubelet/types:go_default_library",
|
||||
"//pkg/kubelet/util:go_default_library",
|
||||
"//pkg/util/configz:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
@@ -46,6 +45,7 @@ go_library(
|
||||
"//staging/src/k8s.io/apiserver/pkg/server/httplog:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/server/routes:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/util/flushwriter:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/configz:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/logs:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/metrics:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
|
||||
|
||||
@@ -53,6 +53,7 @@ import (
|
||||
"k8s.io/apiserver/pkg/server/httplog"
|
||||
"k8s.io/apiserver/pkg/server/routes"
|
||||
"k8s.io/apiserver/pkg/util/flushwriter"
|
||||
"k8s.io/component-base/configz"
|
||||
"k8s.io/component-base/logs"
|
||||
compbasemetrics "k8s.io/component-base/metrics"
|
||||
"k8s.io/component-base/metrics/legacyregistry"
|
||||
@@ -71,7 +72,6 @@ import (
|
||||
"k8s.io/kubernetes/pkg/kubelet/server/streaming"
|
||||
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
|
||||
"k8s.io/kubernetes/pkg/kubelet/util"
|
||||
"k8s.io/kubernetes/pkg/util/configz"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -14,7 +14,6 @@ filegroup(
|
||||
"//pkg/util/async:all-srcs",
|
||||
"//pkg/util/bandwidth:all-srcs",
|
||||
"//pkg/util/config:all-srcs",
|
||||
"//pkg/util/configz:all-srcs",
|
||||
"//pkg/util/conntrack:all-srcs",
|
||||
"//pkg/util/coverage:all-srcs",
|
||||
"//pkg/util/ebtables:all-srcs",
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["configz.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/util/configz",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["configz_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package configz serves ComponentConfig objects from running components.
|
||||
//
|
||||
// Each component that wants to serve its ComponentConfig creates a Config
|
||||
// object, and the program should call InstallHandler once. e.g.,
|
||||
// func main() {
|
||||
// boatConfig := getBoatConfig()
|
||||
// planeConfig := getPlaneConfig()
|
||||
//
|
||||
// bcz, err := configz.New("boat")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// bcz.Set(boatConfig)
|
||||
//
|
||||
// pcz, err := configz.New("plane")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// pcz.Set(planeConfig)
|
||||
//
|
||||
// configz.InstallHandler(http.DefaultServeMux)
|
||||
// http.ListenAndServe(":8080", http.DefaultServeMux)
|
||||
// }
|
||||
package configz
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
configsGuard sync.RWMutex
|
||||
configs = map[string]*Config{}
|
||||
)
|
||||
|
||||
// Config is a handle to a ComponentConfig object. Don't create these directly;
|
||||
// use New() instead.
|
||||
type Config struct {
|
||||
val interface{}
|
||||
}
|
||||
|
||||
// InstallHandler adds an HTTP handler on the given mux for the "/configz"
|
||||
// endpoint which serves all registered ComponentConfigs in JSON format.
|
||||
func InstallHandler(m mux) {
|
||||
m.Handle("/configz", http.HandlerFunc(handle))
|
||||
}
|
||||
|
||||
type mux interface {
|
||||
Handle(string, http.Handler)
|
||||
}
|
||||
|
||||
// New creates a Config object with the given name. Each Config is registered
|
||||
// with this package's "/configz" handler.
|
||||
func New(name string) (*Config, error) {
|
||||
configsGuard.Lock()
|
||||
defer configsGuard.Unlock()
|
||||
if _, found := configs[name]; found {
|
||||
return nil, fmt.Errorf("register config %q twice", name)
|
||||
}
|
||||
newConfig := Config{}
|
||||
configs[name] = &newConfig
|
||||
return &newConfig, nil
|
||||
}
|
||||
|
||||
// Delete removes the named ComponentConfig from this package's "/configz"
|
||||
// handler.
|
||||
func Delete(name string) {
|
||||
configsGuard.Lock()
|
||||
defer configsGuard.Unlock()
|
||||
delete(configs, name)
|
||||
}
|
||||
|
||||
// Set sets the ComponentConfig for this Config.
|
||||
func (v *Config) Set(val interface{}) {
|
||||
configsGuard.Lock()
|
||||
defer configsGuard.Unlock()
|
||||
v.val = val
|
||||
}
|
||||
|
||||
// MarshalJSON marshals the ComponentConfig as JSON data.
|
||||
func (v *Config) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(v.val)
|
||||
}
|
||||
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
if err := write(w); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func write(w http.ResponseWriter) error {
|
||||
var b []byte
|
||||
var err error
|
||||
func() {
|
||||
configsGuard.RLock()
|
||||
defer configsGuard.RUnlock()
|
||||
b, err = json.Marshal(configs)
|
||||
}()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshaling json: %v", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
_, err = w.Write(b)
|
||||
return err
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package configz
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigz(t *testing.T) {
|
||||
v, err := New("testing")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
v.Set("blah")
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(handle))
|
||||
defer s.Close()
|
||||
|
||||
resp, err := http.Get(s.URL + "/configz")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if string(body) != `{"testing":"blah"}` {
|
||||
t.Fatalf("unexpected output: %s", body)
|
||||
}
|
||||
|
||||
v.Set("bing")
|
||||
resp, err = http.Get(s.URL + "/configz")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if string(body) != `{"testing":"bing"}` {
|
||||
t.Fatalf("unexpected output: %s", body)
|
||||
}
|
||||
|
||||
Delete("testing")
|
||||
resp, err = http.Get(s.URL + "/configz")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if string(body) != `{}` {
|
||||
t.Fatalf("unexpected output: %s", body)
|
||||
}
|
||||
if resp.Header.Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("unexpected Content-Type: %s", resp.Header.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user