fix(docker): add TLS check; correct dial handling and reconnection for custom docker provider; modernize pointer arithemetic with unsafe.Add

This commit is contained in:
yusing
2026-01-07 15:28:53 +08:00
parent 7bfb57ea30
commit 1ebba20216
2 changed files with 21 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import (
"maps"
"net"
"net/http"
"net/url"
"reflect"
"sync"
"sync/atomic"
@@ -169,9 +170,26 @@ func NewClient(cfg types.DockerProviderConfig, unique ...bool) (*SharedClient, e
client.WithDialContext(helper.Dialer),
}
} else {
// connhelper.GetConnectionHelper already parsed the host without error
url, _ := url.Parse(host)
opt = []client.Opt{
client.WithHost(host),
}
switch url.Scheme {
case "", "tls", "http", "https":
if (url.Scheme == "https" || url.Scheme == "tls") && cfg.TLS == nil {
return nil, fmt.Errorf("TLS config is not set when using %s:// host", url.Scheme)
}
dial = func(ctx context.Context) (net.Conn, error) {
var dialer net.Dialer
return dialer.DialContext(ctx, "tcp", url.Host)
}
opt = append(opt, client.WithDialContext(func(ctx context.Context, _, _ string) (net.Conn, error) {
return dial(ctx)
}))
}
}
}
@@ -212,7 +230,7 @@ func NewClient(cfg types.DockerProviderConfig, unique ...bool) (*SharedClient, e
}
func (c *SharedClient) GetHTTPClient() **http.Client {
return (**http.Client)(unsafe.Pointer(uintptr(unsafe.Pointer(c.Client)) + clientClientOffset))
return (**http.Client)(unsafe.Add(unsafe.Pointer(c.Client), clientClientOffset))
}
func (c *SharedClient) InterceptHTTPClient(intercept httputils.InterceptFunc) {
@@ -279,6 +297,6 @@ func (c *SharedClient) unotel() {
log.Debug().Str("host", c.DaemonHost()).Msgf("docker client transport is not an otelhttp.Transport: %T", httpClient.Transport)
return
}
transport := *(*http.RoundTripper)(unsafe.Pointer(uintptr(unsafe.Pointer(otelTransport)) + otelRtOffset))
transport := *(*http.RoundTripper)(unsafe.Add(unsafe.Pointer(otelTransport), otelRtOffset))
httpClient.Transport = transport
}

View File

@@ -177,7 +177,7 @@ func checkConnection(ctx context.Context, client *docker.SharedClient) bool {
defer cancel()
err := client.CheckConnection(ctx)
if err != nil {
log.Debug().Err(err).Msg("docker watcher: connection failed")
log.Debug().Err(err).Str("host", client.Address()).Msg("docker watcher: connection failed")
return false
}
return true