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

[VC-36032] Pass the context to Venafi clients and enable debug roundtripper #627

Merged
merged 6 commits into from
Nov 28, 2024

Conversation

wallrj
Copy link
Member

@wallrj wallrj commented Nov 21, 2024

  • Improve orderly shutdown by abandoning ongoing requests to Venafi when the context is cancelled
  • Make it easier to debug venafi data upload problems by allowing the request details to be logged with --log-level=6.
go run . agent --install-namespace venafi --one-shot --input-path out.json  --venafi-connection venafi-components -v6

I1127 09:58:16.779115 318776 metrics_transport.go:113] "outgoing http request succeeded" source="controller-runtime" method="POST" uri="https://api.venafi.eu/v1/oauth2/v2.0/9a0cab61-2b00-11ee-ba09-0733b0fe5adc/token" status="200 OK" duration="582.557289ms"
I1127 09:58:17.313520 318776 round_trippers.go:553] POST https://api.venafi.eu/v1/tlspk/upload/clusterdata/no?name=my_cluster 200 OK in 502 milliseconds
I1127 09:58:17.313624 318776 run.go:405] "Data sent successfully" logger="Run.gatherAndOutputData.postData"

W1127 09:58:17.313811 318776 reflector.go:484] pkg/mod/k8s.io/client-go@v0.31.1/tools/cache/reflector.go:243: watch of *v1alpha1.VenafiConnection ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding
I1127 09:58:17.313874 318776 reflector.go:311] Stopping reflector *v1alpha1.VenafiConnection (10h1m26.347417125s) from pkg/mod/k8s.io/client-go@v0.31.1/tools/cache/reflector.go:243
I1127 09:58:17.313899 318776 run.go:484] "Shutting down" logger="Run.APIServer.ListenAndServe" addr=":8081"
I1127 09:58:17.314047 318776 run.go:499] "Shutdown complete" logger="Run.APIServer.ListenAndServe" addr=":8081"

@wallrj wallrj force-pushed the VC-36032/context-to-client branch 3 times, most recently from 12618ad to f46d396 Compare November 27, 2024 09:51
Add the client-go debug round tripper to the venafi clients

Signed-off-by: Richard Wall <richard.wall@venafi.com>
@wallrj wallrj force-pushed the VC-36032/context-to-client branch from f46d396 to 68bbb8f Compare November 27, 2024 09:55
Signed-off-by: Richard Wall <richard.wall@venafi.com>
Signed-off-by: Richard Wall <richard.wall@venafi.com>
Signed-off-by: Richard Wall <richard.wall@venafi.com>
@wallrj wallrj force-pushed the VC-36032/context-to-client branch 2 times, most recently from 3b3b68a to ec8af5f Compare November 27, 2024 14:18
extraArgs:
- --logging-format=json
- --log-level=6 # To enable HTTP request logging
```
Copy link
Member Author

Choose a reason for hiding this comment

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

# Example:
# extraArgs:
# - --logging-format=json
# - --log-level=6 # To enable HTTP request logging
extraArgs: []
Copy link
Member Author

@wallrj wallrj Nov 27, 2024

Choose a reason for hiding this comment

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

@@ -10,4 +10,4 @@ authentication:

extraArgs:
- --logging-format=json
- --log-level=2
- --log-level=6
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's how it looks in Google Logs Explorer

image

@@ -648,7 +655,7 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
testutil.TrustCA(t, cl, cert)
assert.Equal(t, VenafiCloudKeypair, got.AuthMode)

err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"})
err = cl.PostDataReadingsWithOptions(ctx, nil, client.Options{ClusterName: "test cluster name"})
require.NoError(t, err)
Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing gets logged by the client in this test

$ go test ./pkg/agent/... -v -run "Test_ValidateAndCombineConfig_VenafiCloudKeyPair"
=== RUN   Test_ValidateAndCombineConfig_VenafiCloudKeyPair
=== RUN   Test_ValidateAndCombineConfig_VenafiCloudKeyPair/server,_uploader_id,_and_cluster_name_are_correctly_passed
    envtest.go:188: fake api.venafi.cloud received request: POST /v1/oauth/token/serviceaccount
    envtest.go:188: fake api.venafi.cloud received request: POST /v1/tlspk/upload/clusterdata/no
--- PASS: Test_ValidateAndCombineConfig_VenafiCloudKeyPair (0.01s)
    --- PASS: Test_ValidateAndCombineConfig_VenafiCloudKeyPair/server,_uploader_id,_and_cluster_name_are_correctly_passed (0.01s)
PASS
ok      github.com/jetstack/preflight/pkg/agent 0.044s

testutil.TrustCA(t, cl, cert)

// TODO(mael): the client should keep track of the cluster ID, we
// shouldn't need to pass it as an option to
// PostDataReadingsWithOptions.
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID})
err = cl.PostDataReadingsWithOptions(ctx, nil, client.Options{ClusterName: cfg.ClusterID})
require.NoError(t, err)
Copy link
Member Author

Choose a reason for hiding this comment

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

$ go test ./pkg/agent/... -v -run "Test_ValidateAndCombineConfig_VenafiConnection"
=== RUN   Test_ValidateAndCombineConfig_VenafiConnection
=== RUN   Test_ValidateAndCombineConfig_VenafiConnection/err_when_cluster_id_field_is_empty
=== RUN   Test_ValidateAndCombineConfig_VenafiConnection/the_server_field_is_ignored_when_VenafiConnection_is_used
=== NAME  Test_ValidateAndCombineConfig_VenafiConnection
    envtest.go:188: fake api.venafi.cloud received request: POST /v1/tlspk/upload/clusterdata/no
    envtest.go:51: Waiting for envtest to exit
--- PASS: Test_ValidateAndCombineConfig_VenafiConnection (7.01s)
    --- PASS: Test_ValidateAndCombineConfig_VenafiConnection/err_when_cluster_id_field_is_empty (0.00s)
    --- PASS: Test_ValidateAndCombineConfig_VenafiConnection/the_server_field_is_ignored_when_VenafiConnection_is_used (0.17s)
PASS
ok      github.com/jetstack/preflight/pkg/agent 7.041s

Copy link
Member Author

Choose a reason for hiding this comment

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

$ go test ./pkg/client/... -v -run "TestVenConnClient_PostDataReadin
gsWithOptions"
=== RUN   TestVenConnClient_PostDataReadingsWithOptions
=== PAUSE TestVenConnClient_PostDataReadingsWithOptions
=== CONT  TestVenConnClient_PostDataReadingsWithOptions
=== RUN   TestVenConnClient_PostDataReadingsWithOptions/valid_accessToken
    envtest.go:188: fake api.venafi.cloud received request: POST /v1/tlspk/upload/clusterdata/no
=== RUN   TestVenConnClient_PostDataReadingsWithOptions/error_when_the_apiKey_field_is_used
W1127 14:44:47.223096  366750 reflector.go:484] pkg/mod/k8s.io/client-go@v0.31.1/tools/cache/reflector.go:243: watch of *v1.PartialObjectMetadata ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding
    envtest.go:188: fake api.venafi.cloud received request: GET /v1/useraccounts
    envtest.go:188: fake api.venafi.cloud received request: GET /v1/useraccounts
    metrics_transport.go:113: I1127 14:44:47.488976] outgoing http request succeeded method="GET" uri="https://127.0.0.1:36713/v1/useraccounts" status="200 OK" duration="340.47µs"
W1127 14:44:47.491474  366750 reflector.go:484] pkg/mod/k8s.io/client-go@v0.31.1/tools/cache/reflector.go:243: watch of *v1alpha1.VenafiConnection ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding
=== RUN   TestVenConnClient_PostDataReadingsWithOptions/error_when_the_tpp_field_is_used
W1127 14:44:47.491530  366750 reflector.go:484] pkg/mod/k8s.io/client-go@v0.31.1/tools/cache/reflector.go:243: watch of *v1.PartialObjectMetadata ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding
    envtest.go:235: fake tpp.example.com received request: GET /vedsdk/Identity/Self
    envtest.go:235: fake tpp.example.com received request: GET /vedsdk/Identity/Self
    metrics_transport.go:113: I1127 14:44:47.756336] outgoing http request succeeded method="GET" uri="https://127.0.0.1:46053/vedsdk/Identity/Self" status="200 OK" duration="453.761µs"
=== NAME  TestVenConnClient_PostDataReadingsWithOptions
    envtest.go:51: Waiting for envtest to exit
--- PASS: TestVenConnClient_PostDataReadingsWithOptions (7.93s)
    --- PASS: TestVenConnClient_PostDataReadingsWithOptions/valid_accessToken (2.35s)
    --- PASS: TestVenConnClient_PostDataReadingsWithOptions/error_when_the_apiKey_field_is_used (0.27s)
    --- PASS: TestVenConnClient_PostDataReadingsWithOptions/error_when_the_tpp_field_is_used (0.27s)
PASS
ok      github.com/jetstack/preflight/pkg/client        7.958s

Signed-off-by: Richard Wall <richard.wall@venafi.com>
…rt values documentation.

Signed-off-by: Richard Wall <richard.wall@venafi.com>
@wallrj wallrj force-pushed the VC-36032/context-to-client branch from ec8af5f to fcca2f1 Compare November 27, 2024 14:48
@wallrj wallrj changed the title WIP: [VC-36032] Pass the context to the Venafi clients [VC-36032] Pass the context to Venafi clients and enable debug roundtripper Nov 27, 2024
Copy link
Member Author

Choose a reason for hiding this comment

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

client-go uses the global klog functions rather than the logger from the context. I didn't realise that when I started this PR and in hindsight the introduction of DebugWrappers could be split into a separate PR.

@wallrj wallrj requested a review from maelvls November 27, 2024 15:01
@maelvls
Copy link
Member

maelvls commented Nov 28, 2024

I'm OK with these having these two seemingly unrelated changes (debug roundtripper + pass context down to Venafi clients) since it aims to the same thing: logging HTTP requests for the Venafi client and the Kubernetes client.

outgoing http request succeeded method="GET" uri="https://127.0.0.1:46053/vedsdk/Identity/Self" status="200 OK" duration="453.761µs"

Excellent! We will have a much easier way to debug customer issues with -v=6. Your PR will allow us to close #220, too. @hawksight will be super happy to hear about your change.

I have looked at the changes and they look OK to me. I have not tried the feature locally since you showed everything I needed in the PR description.

@maelvls
Copy link
Member

maelvls commented Nov 28, 2024

Maybe one note: client-go seems to be forcing -v=6 on us. I don't particularly like -v=6 as it is super hidden, but it's such a helpful feature. Sometimes, I'd prefer that we had something like --log-http-requests, but -v=6 will do, especially since you have added some notes in the flag's help:

-v      0=Info, 1=Debug, 2=Trace. Use 6-9 for increasingly verbose HTTP request logging. (default: 0)

@wallrj wallrj merged commit 98afe3b into master Nov 28, 2024
2 checks passed
@wallrj wallrj deleted the VC-36032/context-to-client branch November 28, 2024 10:10
@wallrj wallrj restored the VC-36032/context-to-client branch November 28, 2024 10:10
@wallrj wallrj deleted the VC-36032/context-to-client branch November 28, 2024 10:10
@wallrj wallrj linked an issue Nov 28, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A flag for setting the log level and more verbose log output
2 participants