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 Mar 4, 2024
1 parent 8c80c07 commit 6834ea7
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 126 deletions.
15 changes: 14 additions & 1 deletion staging/src/k8s.io/client-go/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,24 @@ 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 // Use InClusterConfigWithContext instead in code which supports contextual logging.
func InClusterConfig() (*Config, error) {
return InClusterConfigWithContext(context.Background())
}

// InClusterConfig returns a config object which uses the service account
// 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.
// This function supports contextual logging.
func InClusterConfigWithContext(ctx context.Context) (*Config, error) {
const (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
)
logger := klog.FromContext(ctx)

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 +539,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, but got an err", "rootCAFile", rootCAFile)
} else {
tlsClientConfig.CAFile = rootCAFile
}
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 6834ea7

Please sign in to comment.