Skip to content

Commit

Permalink
Migrate client-go/rest to contextual logging
Browse files Browse the repository at this point in the history
Signed-off-by: WillardHu <wei.hu@daocloud.io>
  • Loading branch information
WillardHu committed Dec 27, 2023
1 parent f55d18a commit 85dae87
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 109 deletions.
20 changes: 10 additions & 10 deletions staging/src/k8s.io/client-go/rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,36 +335,36 @@ func TestHTTPProxy(t *testing.T) {
}

func TestCreateBackoffManager(t *testing.T) {

ctx := context.Background()
theUrl, _ := url.Parse("http://localhost")

// 1 second base backoff + duration of 2 seconds -> exponential backoff for requests.
t.Setenv(envBackoffBase, "1")
t.Setenv(envBackoffDuration, "2")
backoff := readExpBackoffConfig()
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
if backoff.CalculateBackoff(theUrl)/time.Second != 2 {
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 2 {
t.Errorf("Backoff env not working.")
}

// 0 duration -> no backoff.
t.Setenv(envBackoffBase, "1")
t.Setenv(envBackoffDuration, "0")
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff = readExpBackoffConfig()
if backoff.CalculateBackoff(theUrl)/time.Second != 0 {
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 0 {
t.Errorf("Zero backoff duration, but backoff still occurring.")
}

// No env -> No backoff.
t.Setenv(envBackoffBase, "")
t.Setenv(envBackoffDuration, "")
backoff = readExpBackoffConfig()
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
if backoff.CalculateBackoff(theUrl)/time.Second != 0 {
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 0 {
t.Errorf("Backoff should have been 0.")
}

Expand Down
7 changes: 6 additions & 1 deletion staging/src/k8s.io/client-go/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import (
const (
DefaultQPS float32 = 5.0
DefaultBurst int = 10

loggerNameConfig = "rest_config"
)

var ErrNotInCluster = errors.New("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined")
Expand Down Expand Up @@ -508,11 +510,14 @@ func DefaultKubernetesUserAgent() string {
// kubernetes gives to pods. It's intended for clients that expect to be
// running inside a pod running on kubernetes. It will return ErrNotInCluster
// if called from a process not running in a kubernetes environment.
// logcheck:context InClusterConfig should not be used in code which supports contextual logging.
func InClusterConfig() (*Config, error) {
const (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
)
logger := klog.Background().WithName(loggerNameConfig)

host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, ErrNotInCluster
Expand All @@ -526,7 +531,7 @@ func InClusterConfig() (*Config, error) {
tlsClientConfig := TLSClientConfig{}

if _, err := certutil.NewPool(rootCAFile); err != nil {
klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
logger.Error(err, "Expected to load root CA config", "rootCAFile", rootCAFile)
} else {
tlsClientConfig.CAFile = rootCAFile
}
Expand Down
4 changes: 3 additions & 1 deletion staging/src/k8s.io/client-go/rest/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"k8s.io/client-go/transport"
"k8s.io/client-go/util/flowcontrol"

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -264,7 +265,8 @@ var fakeWrapperFunc = func(http.RoundTripper) http.RoundTripper {

type fakeWarningHandler struct{}

func (f fakeWarningHandler) HandleWarningHeader(code int, agent string, message string) {}
func (f fakeWarningHandler) HandleWarningHeader(logger logr.Logger, code int, agent string, message string) {
}

type fakeNegotiatedSerializer struct{}

Expand Down
5 changes: 4 additions & 1 deletion staging/src/k8s.io/client-go/rest/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ func (n *noopPersister) Persist(_ map[string]string) error {
var pluginsLock sync.Mutex
var plugins = make(map[string]Factory)

// RegisterAuthProviderPlugin ...
// logcheck:context this function RegisterAuthProviderPlugin(..) is called only when auth provider plugins are init().
func RegisterAuthProviderPlugin(name string, plugin Factory) error {
logger := klog.Background()
pluginsLock.Lock()
defer pluginsLock.Unlock()
if _, found := plugins[name]; found {
return fmt.Errorf("auth Provider Plugin %q was registered twice", name)
}
klog.V(4).Infof("Registered Auth Provider Plugin %q", name)
logger.V(4).Info("registered auth provider plugin", "name", name)
plugins[name] = plugin
return nil
}
Expand Down

0 comments on commit 85dae87

Please sign in to comment.