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 Jan 18, 2024
1 parent 2624e93 commit 1bedf91
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 164 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
6 changes: 2 additions & 4 deletions staging/src/k8s.io/client-go/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"k8s.io/client-go/transport"
certutil "k8s.io/client-go/util/cert"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -526,10 +525,9 @@ 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)
} else {
tlsClientConfig.CAFile = rootCAFile
return nil, err
}
tlsClientConfig.CAFile = rootCAFile

return &Config{
// TODO: switch to using cluster DNS.
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
6 changes: 3 additions & 3 deletions staging/src/k8s.io/client-go/rest/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
"net/http"
"sync"

"k8s.io/klog/v2"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
)

type AuthProvider interface {
Expand Down Expand Up @@ -65,7 +64,8 @@ func RegisterAuthProviderPlugin(name string, plugin Factory) error {
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)
//nolint:logchck // For development and debugging only
klog.Background().V(4).Info("Registered Auth Provider Plugin", "plugin-name", name)
plugins[name] = plugin
return nil
}
Expand Down

0 comments on commit 1bedf91

Please sign in to comment.