Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Migrate client-go/rest to contextual logging #122445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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