From 760ec4e58a6b6d80dd86cc1484121c0244739385 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Fri, 30 Jul 2021 09:38:43 +0200 Subject: [PATCH] virtcontainers: clh: Do not use the default HTTP client When enabling tracing with Cloud Hypervisor, we end up establishing 2 connections to 2 different HTTP servers: The Cloud Hypervisor API one that runs over a UNIX socket and the Jaeger endpoint running over UDP. Both connections use the default HTTP golang client instance, and thus share the same transport layer. As the Cloud Hypervisor implementation sets it up to be over a Unix socket, the jaeger uploader ends up going through that transport as well, and sending its spans to the Cloud Hypervisor API server. We fix that by giving the Cloud Hypervisor implementation its own HTTP client instance and we avoid sharing it with anything else in the shim. Fixes #2364 Signed-off-by: Samuel Ortiz --- src/runtime/virtcontainers/clh.go | 41 ++++++++++++------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index 190925303..3a9311254 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -281,6 +281,22 @@ func (clh *cloudHypervisor) createSandbox(ctx context.Context, id string, networ } clh.state.apiSocket = apiSocketPath + cfg := chclient.NewConfiguration() + cfg.HTTPClient = &http.Client{ + Transport: &http.Transport{ + DialContext: func(ctx context.Context, network, path string) (net.Conn, error) { + addr, err := net.ResolveUnixAddr("unix", clh.state.apiSocket) + if err != nil { + return nil, err + } + + return net.DialUnix("unix", nil, addr) + }, + }, + } + + clh.APIClient = chclient.NewAPIClient(cfg).DefaultApi + clh.virtiofsd = &virtiofsd{ path: clh.config.VirtioFSDaemon, sourcePath: filepath.Join(getSharePath(clh.id)), @@ -968,34 +984,9 @@ func (clh *cloudHypervisor) isClhRunning(timeout uint) (bool, error) { } func (clh *cloudHypervisor) client() clhClient { - if clh.APIClient == nil { - clh.APIClient = clh.newAPIClient() - } - return clh.APIClient } -func (clh *cloudHypervisor) newAPIClient() *chclient.DefaultApiService { - - cfg := chclient.NewConfiguration() - - socketTransport := &http.Transport{ - DialContext: func(ctx context.Context, network, path string) (net.Conn, error) { - addr, err := net.ResolveUnixAddr("unix", clh.state.apiSocket) - if err != nil { - return nil, err - } - - return net.DialUnix("unix", nil, addr) - }, - } - - cfg.HTTPClient = http.DefaultClient - cfg.HTTPClient.Transport = socketTransport - - return chclient.NewAPIClient(cfg).DefaultApi -} - func openAPIClientError(err error) error { if err == nil {