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

client-go: export NewDebuggingRoundTripper function and DebugLevel #98324

Merged
merged 7 commits into from Feb 2, 2021

Conversation

atosatto
Copy link
Contributor

@atosatto atosatto commented Jan 22, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

debuggingRoundTripper is a useful tool to debug Kubernetes API requests and their timing.

Unfortunately it can only be used via the DebugWrappers function, which adjusts the level of debugging information logged by the roundTripper based on the configured klog verbosity.

While DebugWrappers definitely works for clients using klog (like kubectl), this is currently hard to be used from controllers using controller-runtime, as by default the library uses github.com/go-logr/logr for logging.

In this PR we change the visibility of newDebuggingRoundTripper and debugLevel in order to be able to use directly the debugRoundTripper without the DebugWrappers function.

In particular, the changes proposed in this PR allow to use the debuggingRoundTripper to intercept Kubernetes API requests performed by controller-runtime using the following sample code

import (
        ctrl "sigs.k8s.io/controller-runtime"
)

func init() {
	ctrl.SetLogger(zap.New())
}

func main() {
        // wrap the http transport used by the Kubernetes client
	restConfig, err := ctrl.GetConfig()
	checkError(setupLog, err, "unable to get kubernetes client config")

        restConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
		return transport.NewDebuggingRoundTripper(rt, transport.DebugJustURL)
	})

        ...
}

Does this PR introduce a user-facing change?:

Export NewDebuggingRoundTripper function and DebugLevel options in the k8s.io/client-go/transport package.

`debuggingRoundTripper` is a useful throbleshooting tool to debug of Kubernetes API requests and their timing.

Unfortunately, as of today, it can only be used via the `DebugWrappers` function, which automatically adjust the amount of debug information exposed by the roundTripper based on the configured `klog` verbosity.

While `DebugWrappers` definitely fits the purpose for clients using `klog`, this is currently hard to be used for controllers using `controller-runtime`, which uses `github.com/go-logr/logr` for logging.

In this PR we change the visibility of `newDebuggingRoundTripper` and `debugLevel` in order to be directly accessible from users of the `k8s.io/client-go/transport` package.

In particular, the changes proposed in this PR allow users of `controller-runtime` to use the `debuggingRoundTripper` to intercept Kubernetes API requests as follows

```go
import (
        ctrl "sigs.k8s.io/controller-runtime"
)

func init() {
	ctrl.SetLogger(zap.New())
}

func main() {
        // wrap the http transport used by the Kubernetes client
	restConfig, err := ctrl.GetConfig()
	checkError(setupLog, err, "unable to get kubernetes client config")

        restConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
		return transport.NewDebuggingRoundTripper(rt, transport.DebugJustURL)
	})

        ...
}
```
@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 22, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @atosatto. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jan 22, 2021
@leilajal
Copy link
Contributor

cc @lavalamp

@atosatto
Copy link
Contributor Author

atosatto commented Jan 26, 2021

@lavalamp adding here some more information on my PR.

Currently, the one way we can build a debuggingRoundTripper is using the DebugWrappers function:

// DebugWrappers wraps a round tripper and logs based on the current log level.
func DebugWrappers(rt http.RoundTripper) http.RoundTripper {
	switch {
	case bool(klog.V(9).Enabled()):
		rt = newDebuggingRoundTripper(rt, debugCurlCommand, debugURLTiming, debugResponseHeaders)
	case bool(klog.V(8).Enabled()):
		rt = newDebuggingRoundTripper(rt, debugJustURL, debugRequestHeaders, debugResponseStatus, debugResponseHeaders)
	case bool(klog.V(7).Enabled()):
		rt = newDebuggingRoundTripper(rt, debugJustURL, debugRequestHeaders, debugResponseStatus)
	case bool(klog.V(6).Enabled()):
		rt = newDebuggingRoundTripper(rt, debugURLTiming)
	}

	return rt
}

Unfortunately this is relying on klog's verbosity to customise the level of information exposed by the debuggingRoundTripper and there is no way for consumers of the client-go API to customise the output of the debuggingRoundTripper.

In this PR I am proposing to change the visibility of newDebuggingRoundTripper (and debugLevels) to allow client-go's API consumers (in my case a controller built with controller-runtime) to create debuggingRoundTripper with custom debugLevels.

Copy link
Member

@lavalamp lavalamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm pretty OK with this since it is just making something public. But did we consider letting the user also pass a log alternative in? So it doesn't have to use the global one? I guess that could be a separate change if we wanted to do that.

staging/src/k8s.io/client-go/transport/round_trippers.go Outdated Show resolved Hide resolved
@atosatto
Copy link
Contributor Author

I think I'm pretty OK with this since it is just making something public. But did we consider letting the user also pass a log alternative in? So it doesn't have to use the global one? I guess that could be a separate change if we wanted to do that.

I did but I am afraid this would be a bigger change (perhaps with more contention) as it would require us to switch to the logr.Logger interface and default to klogr to guarantee backward compatibility with the current behaviour.

I am happy to give it a try in a separated PR as this would definitely help in the scenario where the API is used in controller-runtime's controllers.

@lavalamp
Copy link
Member

OK. Let's just take this and wait for the structured logging folks to work their way through the system.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 26, 2021
@lavalamp
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 26, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: atosatto, lavalamp

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 26, 2021
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 26, 2021
@k8s-ci-robot k8s-ci-robot added the area/dependency Issues or PRs related to dependency changes label Jan 26, 2021
@atosatto
Copy link
Contributor Author

/test pull-kubernetes-e2e-kind-ipv6

@atosatto
Copy link
Contributor Author

@lavalamp it looks like the PR is now passing all the tests. Would you mind checking again whether it is good to be merged? Thanks!

@fedebongio
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 2, 2021
@lavalamp
Copy link
Member

lavalamp commented Feb 2, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 2, 2021
@k8s-ci-robot k8s-ci-robot merged commit 27104f1 into kubernetes:master Feb 2, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/dependency Issues or PRs related to dependency changes cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants